从IOS中的AES加密到JAVA中的解密

从IOS中的AES加密到JAVA中的解密,java,ios,encryption,aes,Java,Ios,Encryption,Aes,我在用JAVA解密我用IOS加密的mp3文件时遇到问题。以下是用于在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 (un

我在用JAVA解密我用IOS加密的mp3文件时遇到问题。以下是用于在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;
}

下面是我用JAVA解密文件的代码,我调用这个函数来解密:

    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();
}

此外,以下是我在这两个方面使用的关键点:

“3STI5F2F41608581SO3D8UN346D2E810097E220RAD9F9C29BPY738956BBE”

当我运行java应用程序时,出现以下错误:

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)....
任何帮助都将不胜感激

谢谢
迈克尔

简短回答

您的Java代码正在使用PBKDF2转换密钥,而您的Obj-C代码似乎不是(尽管您没有提供足够的代码让我们确定)。所以密钥不同,这就是解密无法工作的原因

主动提供的免费建议


看起来你只是从不同的地方剪切和粘贴代码,而没有真正理解它。如果你想使用加密,你需要了解你在做什么,否则你可能会把事情搞砸(即使它们似乎在工作)。如果您不知道密码和密钥之间的区别,或者不知道什么是PBKDF2以及何时使用它,那么您需要做一些基础研究。

感谢您的回复,没问题,我会做更多的研究,您需要查看哪些额外的代码才能确定?我可以将此粘贴到中供您查看。您需要显示Obj-C代码中的密钥。正如我所说,您的Java代码在使用PBKDF2之前将密钥转换为PBKDF2。看起来Obj-C没有。因此,要么在Obj-C代码中使用PBKDF2,要么将其从Java代码中删除。或者更简洁地说,确保
AES256EncryptWithKey
Obj-C函数中的
key
参数与
encryptOrDecrypt
Java函数中的
secret
变量的字节序列相同。如果您关心代码的安全性,至少要让加密域专家对其进行检查,这就是我所做的,我已经做加密多年了。否则,甚至不用费心尝试使用加密。