Android IOS Java通用的AES128加密解密Demo下载
2016-10-31 10:36:41  By: dwtedx

出于安全考虑、很多App在与服务器接口的请求和响应过程中都会涉及到加密和解密的问题、如果不加的话就会是明文的、即使加了GZIP也可以被直接解压成明文、在安全性上不是很好、所以很多涉及到金额的相关的App都会加密的、但是如果同时有Android和IOS的App的话、必须要保证加密和解密的算法一致、要不然对后台没法处理

之前就写过一种AES算法只能Android和后台的加密解密一样、IOS始终调试不过、后来花了很长时间研究、发现是OC和Java对Aes的Key的处理方法不一样、很久很久之后、终于解决了问题、今天在此给大家分享一下AES的加密和解密、Android和ios通用的AES加密算法、大家可以直接集成到自己的项目、服务器接口如果是用Java写的话、整个框架都完美了、如果是.NET编写的后台接口的话、得改造一下哦


IOS加密

/*加密方法*/
  (NSString *)AES256EncryptWithPlainText:(NSString *)plain {
    NSData *plainText = [plain dataUsingEncoding:NSUTF8StringEncoding];
    // ´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)
        
    NSUInteger dataLength = [plainText length];

    size_t bufferSize = dataLength   kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    bzero(buffer, sizeof(buffer));
    
    size_t numBytesEncrypted = 0;
    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding,
                                          [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256,
                                          ivBuff /* initialization vector (optional) */,
                                          [plainText bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        return [encryptData base64Encoding];
    }
    
    free(buffer); //free the buffer;
    return nil;
}


IOS解密

/*解密方法*/
  (NSString *)AES256DecryptWithCiphertext:(NSString *)ciphertexts{
    
    NSData *cipherData = [NSData dataWithBase64EncodedString:ciphertexts];
    // ´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)
        
    NSUInteger dataLength = [cipherData length];
    
    size_t bufferSize = dataLength   kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
        
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          [[NSData AESKeyForPassword:PASSWORD] bytes], kCCKeySizeAES256, 
                                          ivBuff ,/* initialization vector (optional) */
                                          [cipherData bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);
    
    if (cryptStatus == kCCSuccess) {
        NSData *encryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        return [[[NSString alloc] initWithData:encryptData encoding:NSUTF8StringEncoding] init];
    }
    
    free(buffer); //free the buffer;
    return nil;
}


Android加密

private byte[] encrypt(String cmp, SecretKey sk, IvParameterSpec IV,
        byte[] msg) {
    try {
        Cipher c = Cipher.getInstance(cmp);
        c.init(Cipher.ENCRYPT_MODE, sk, IV);
        return c.doFinal(msg);
    } catch (NoSuchAlgorithmException nsae) {
        Log.e("AESdemo", "no cipher getinstance support for "   cmp);
    } catch (NoSuchPaddingException nspe) {
        Log.e("AESdemo", "no cipher getinstance support for padding "   cmp);
    } catch (InvalidKeyException e) {
        Log.e("AESdemo", "invalid key exception");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e("AESdemo", "invalid algorithm parameter exception");
    } catch (IllegalBlockSizeException e) {
        Log.e("AESdemo", "illegal block size exception");
    } catch (BadPaddingException e) {
        Log.e("AESdemo", "bad padding exception");
    }
    return null;
}


Android解密

private byte[] decrypt(String cmp, SecretKey sk, IvParameterSpec IV,
            byte[] ciphertext) {
    try {
        Cipher c = Cipher.getInstance(cmp);
        c.init(Cipher.DECRYPT_MODE, sk, IV);
        return c.doFinal(ciphertext);
    } catch (NoSuchAlgorithmException nsae) {
        Log.e("AESdemo", "no cipher getinstance support for "   cmp);
    } catch (NoSuchPaddingException nspe) {
        Log.e("AESdemo", "no cipher getinstance support for padding "   cmp);
    } catch (InvalidKeyException e) {
        Log.e("AESdemo", "invalid key exception");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e("AESdemo", "invalid algorithm parameter exception");
    } catch (IllegalBlockSizeException e) {
        Log.e("AESdemo", "illegal block size exception");
    } catch (BadPaddingException e) {
        Log.e("AESdemo", "bad padding exception");
        e.printStackTrace();
    }
    return null;
}


以上是部分代码、为了让大家在使用的时候更为方法、给大家做了两个Demo、大家可以直接下载使用

ios android 加密解密源代码下载链接: android ios aes加密 密码: ih4b

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+