簡體   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