0

I am having trouble decrypting an mp3 file in JAVA which I encrypted using IOS. Below is the code used for encrypting the file in IOS:

- (NSData *)AES256EncryptWithKey:(NSData *) audioData: (NSString *)key{ // 'key' should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused) bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding) // fetch key data [key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding]; NSUInteger dataLength = [audioData length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That's why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc( bufferSize ); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [audioData bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted ); if( cryptStatus == kCCSuccess ) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free( buffer ); //free the buffer return nil; 

}

And below is the code I am using to decrypt the file in JAVA, I call this function to decrypt:

 private static final byte[] SALT = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 }; private static final int ITERATION_COUNT = 65536; private static final int KEY_LENGTH = 256; public static void encryptOrDecrypt(String key, byte[] is, OutputStream os) throws Throwable { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(key.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; IvParameterSpec ivspec = new IvParameterSpec(iv); ecipher.init(Cipher.DECRYPT_MODE, secret, ivspec); byte[] encrypted = ecipher.doFinal(is); os.write(encrypted); os.close(); 

}

Also here is the key I am using across both:

"3STI5F2F41608581SO3D8UN346D2E81009THEC7E220RAD9F9C29BPY738956BBE"

When I run the java application I get this error:

javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..).... 

Any help would be greatly appreciated.

Thanks Michael

1
  • At a minimum if you care about the security of your code have it reviewed by a crypto domain expert, that is what I do and I have been doing crypto for years. Otherwise, don't even bother trying to use crypto. Commented Sep 28, 2013 at 2:57

1 Answer 1

1

Short Answer

Your Java code is using PBKDF2 to transform your key, and it looks like your Obj-C code is not (though you haven't given enough code for us to be sure). So the key is different, which is why your decryption is not working.

Unsolicited Free Advice

It looks like you're just cutting and pasting code from different places without really understanding it. If you want to use crypto, you need to understand what you're doing or you can really mess things up (even if they seem to be working). If you don't know the difference between a password and a key, or what PBKDF2 is and when to use it, then you need to do some basic research.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the response and No problem I will do some more research, what extra code do you need to see to be sure? I can paste this in for you to view.
You need to show what your key is in the Obj-C code. As I said, your Java code transforms the key with PBKDF2 before using it. It looks like the Obj-C doesn't. So either use PBKDF2 in the Obj-C code, or take it out of the Java code.
Or more succinctly, make sure the key parameter in your AES256EncryptWithKey Obj-C function is the same sequence of bytes as your secret variable in your encryptOrDecrypt Java function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.