Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMAC mismatch/Unknown header error. #148

Closed
jazeelkt opened this issue Mar 6, 2015 · 7 comments
Closed

HMAC mismatch/Unknown header error. #148

jazeelkt opened this issue Mar 6, 2015 · 7 comments

Comments

@jazeelkt
Copy link

jazeelkt commented Mar 6, 2015

Hi

I was using the RNCryptor for ios for encrypting and decrypting a set of HTML,CSS and JS files. I successfully encrypted the files using:
[RNEncryptor encryptData:fileData withSettings:kRNCryptorAES256Settings password:@"12345_123" error:&error]

While trying to decrypt the files using:
[RNDecryptor decryptData:data withPassword:@"12345_123" error:&error]

I am getting the following errors for some files:
Error: Error Domain=net.robnapier.RNCryptManager Code=1 "HMAC Mismatch" UserInfo=0x7fdbb157b120 {NSLocalizedDescription=HMAC Mismatch}

Error: Error Domain=net.robnapier.RNCryptManager Code=2 "Unknown header" UserInfo=0x7fdbb157ce00 {NSLocalizedDescription=Unknown header}

Any ideas how to fix this?

@rnapier
Copy link
Member

rnapier commented Mar 6, 2015 via email

@jazeelkt
Copy link
Author

jazeelkt commented Mar 9, 2015

Thanks for your reply.

Actually I was using encryption and decryption in two different projects. I encrypted a set of files in one xcode project and saved them to a location. Then I copied those encrypted files to another xcode project and tried to decrypt. The password used for decryption is same as that of encryption.

Also the decryption was successful when I ran the same project multiple times, but this occurs in a random fashion.

@aperechnev
Copy link

I have the similar problem in my iOS application. I had a solution for this problem a few month ago, but I forgot what I did to solve it :(.

Here is my code which encrypts and decrypts data, but it can't decrypt data just in a week after encryption:

- (NSString *)encryptString:(NSString *)source {
  NSString * password = [AccountManager sharedInstance].password;

  NSData * sourceData = [source dataUsingEncoding:NSUTF8StringEncoding];

  NSData * encryptedData = [RNEncryptor encryptData:sourceData
                                       withSettings:kRNCryptorAES256Settings
                                           password:password
                                              error:nil];

  NSString * encryptedString = [encryptedData base64EncodedStringWithOptions:0];

  return encryptedString;
}

- (NSString *)decryptString:(NSString *)source {
  NSString * password = [AccountManager sharedInstance].password;

  NSData * sourceData = [[NSData alloc] initWithBase64EncodedString:source options:0];

  NSData * decryptedData = [RNDecryptor decryptData:sourceData
                                       withSettings:kRNCryptorAES256Settings
                                           password:password
                                              error:nil];

  NSString * decryptedString = [[NSString alloc] initWithData:decryptedData
                                                     encoding:NSUTF8StringEncoding];
  return decryptedString;
}

@rnapier
Copy link
Member

rnapier commented Sep 6, 2015

In all these cases, if you're getting HMAC errors, you either have the wrong password or you've corrupted the data. The more common cause is data corruption, usually due to writing it or reading it in a format you didn't expect. If you're getting the results randomly, you may have race conditions. But in all the cases, you should inspect the data your receive from RNCryptor and the data you send to RNCryptor and confirm they are identical. They are almost certainly not (or the password has changed).

@rnapier rnapier closed this as completed Oct 20, 2015
@javalnanda
Copy link

@rnapier I have situation where I am using async encrypt for encrypting video file while its downloaded and writing it to file on device. Now i have to decrypt it while its being played using AVPlayer and I am using shouldWaitForLoadingOfRequestedResource for decrypting the requested data length and returning that data. Now I can't figure out way to properly decrypt chunks of data when it was encrypted using async encryption.

@rnapier
Copy link
Member

rnapier commented Oct 20, 2015

@javalnanda See the FAQ. See also #161 for a much longer discussion.

@lemarian
Copy link

lemarian commented Nov 3, 2015

This is my code . I am getting rncryptor error 2 which means unknown header passed

class func decrypt(message: String) -> String {
print("the message to decrypt is (message)")

  let encodedData : NSData = NSData(base64EncodedString: message, options: NSDataBase64DecodingOptions(rawValue: 0))!
    let data = message.dataUsingEncoding(NSUTF8StringEncoding)


    let data1: NSData = (encodedData as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
    print("data 1 is \(data1)")


    do {
        print("encoded data :\(encodedData)")

        let decData : NSData = try RNCryptor.decryptData(data1, password: password)
        print("The decrypted data is now now \(decData)")

        let tokenString: String  =   NSString(data: decData, encoding: NSUTF8StringEncoding)! as String
        return tokenString

    }
    catch let error as NSError {
        print("The error description is \(error.localizedDescription)")
        print("The main error is \(error.description)")
        debugPrint("The main main main eror :\(error)")

        return ""
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants