繁体   English   中英

AESCipher(android)vs CCCrypt(ios)

[英]AESCipher (android) vs CCCrypt(ios)

我使用2 lib加密密码,但使用相同的密码,我得到不同的价值:

这是我在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);
}

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

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上任何体面的类别,或者如果你还没有,请尝试这里

加密方法(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加密,我不得不回到上面的方法。 (不再回忆起原因)。

CCCrypt默认为CBC模式。 Java通常默认为不安全的ECB。 请在Android中使用"AES/CBC/PKCS5Padding"并使用随机IV进行加密(在Android或iOS上),包括带有密文的IV。 请注意,Java中的PKCS5Padding 与PKCS#7填充相同 不要依赖加密中的默认值!

暂无
暂无

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

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