java - AESCipher(安卓)与 CCCrypt(ios)

标签 java ios aes

我使用 2 个库来加密密码,但使用相同的密码,我得到不同的值:。

这是我在android中使用的代码:

String dataEncrypted = new String();
try {
    Cipher aesCipher = Cipher.getInstance("AES");
    byte[] raw = hexToBytes(key);
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] byteDataToEncrypt = data.getBytes();
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
    dataEncrypted = new BASE64Encoder().encode(byteCipherText);
    return dataEncrypted;
} catch (Exception ex) {
    //log.d(ex.getMessage());
}

我在 ios 中使用的这段代码:

const void *vplainText;
size_t plainTextBufferSize;
NSData *plainTextData = [data dataUsingEncoding: NSUTF8StringEncoding]; 
plainTextBufferSize = [plainTextData length]; 
vplainText = [plainTextData bytes];
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize =(plainTextBufferSize + kCCBlockSizeAES128);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0001, bufferPtrSize);

const void *vkey = (const void *)[key UTF8String];


CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                      kCCAlgorithmAES128,
                                      kCCOptionPKCS7Padding,
                                      vkey,
                                      kCCKeySizeAES128,
                                      NULL,
                                      vplainText,
                                      plainTextBufferSize, /* input */
                                      bufferPtr,
                                      kCCKeySizeAES128,       /* output */
                                      &movedBytes);

NSString result;
if (cryptStatus== kCCSuccess)
{
    result=[Base64 encode:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    free(bufferPtr);

}else{
    result =@"False";
    free(bufferPtr);
}

如何匹配ios和android的2个版本。请帮帮我!

最佳答案

Android 代码使用 AES256 加密字符串,然后对结果进行 base64 编码。 iOS 代码看起来只是加密,因此要使它们匹配,您必须对其进行 base64 编码。尝试使用以下内容:

- (NSString *)AES256EncryptWithKey:(NSString *)key
{
    NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptedData = [plainData AES256EncryptWithKey:key];

    //Use any decent base64 category support
    NSString *encryptedString = [encryptedData base64Encoding];

    return encryptedString;
}

. .对于 base64 编码,请在 NSString 上使用任何合适的类别,或者如果您还没有,请尝试 here .

加密方式(AES256是Android默认的AES):

   //based on: AES Encrypt/Decrypt, Created by Jim Dovey and 'Jean'
   //See http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
- (NSData*)AES256EncryptWithKey:(NSString*)key
{
    uint8_t iv[kCCBlockSizeAES128];
    SecRandomCopyBytes(0, sizeof(iv), iv);

    // '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 = [self 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, kCCKeySizeAES128,
            iv /* initialization vector (optional) */, [self bytes], dataLength, /* input */
            buffer, bufferSize, /* output */
            &numBytesEncrypted);
    if (cryptStatus == kCCSuccess)
    {
        NSMutableData* result = [NSMutableData dataWithBytes:iv length:kCCBlockSizeAES128];
        [result appendBytes:buffer length:numBytesEncrypted];

        free(buffer); //free the buffer

        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return result;
    }

    free(buffer); //free the buffer
    return nil;
}

我在 CocoaSecurity 上也取得了很好的成功框架,但是为了匹配Android的AES加密,我不得不退回到上面的方法。 (不再记得为什么)。

关于java - AESCipher(安卓)与 CCCrypt(ios),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23861877/

相关文章:

Java:解决命名空间冲突

ios - GIF 图像到 UIImageView 在 Swift 中使用 AlamofireImage

iphone - 将项目加载到设备时出错..关于 libsqlite3.0dylib 文件丢失

java - 更改加密文件的 AES key 而不解密/重新加密它

java - java中的128位 key AES加密应用程序

java - 通过引用复制对象并替换 ArrayList 中的所有项目

java - 如何处理 *.txt 文件中缺少键的异常?属性文件?

ios - 如何在解析中从对象中获取 ObjectId 并在 swift 中将其显示为字符串

linux - Polarssl AES 计数器模式示例

java - 以编程方式在 Spring 中创建对象?