繁体   English   中英

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

[英]AES Encryption in IOS to Decryption in JAVA

我在使用IOS加密的JAVA中解密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();

}

这也是我在两种情况下都使用的密钥:

“ 3STI5F2F41608581SO3D8UN346D2E81009THEC7E220RAD9F9C29BPY738956BBE”

当我运行Java应用程序时,出现此错误:

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

任何帮助将不胜感激。

谢谢迈克尔

简短答案

您的Java代码正在使用PBKDF2来转换密钥,并且看来您的Obj-C代码没有(尽管您没有给我们确定足够的代码)。 因此密钥是不同的,这就是为什么解密无法正常工作的原因。

不请自来的免费建议

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM