iOS和Android中的AES / CBC / PKCS5填充和MD5

时间:2016-11-24 05:57:27

标签: android ios encryption aes md5

我在Android应用程序中使用AES/CBC/PKCS5Padding。代码就像 -

private static String TRANSFORMATION = "AES/CBC/PKCS5Padding";

private static String ALGORITHM = "AES";
private static String DIGEST = "MD5";

private static Cipher cipher;
private static SecretKey password;
private static IvParameterSpec IVParamSpec;
private final static String pvtkey="GDNBCGDRFSC$%#%=";

//16-byte private key
private static byte[] IV = pvtkey.getBytes();

public PassWordEncryptor() {
    try {

        //Encode digest
        MessageDigest digest;           
        digest = MessageDigest.getInstance(DIGEST);            
        password = new SecretKeySpec(digest.digest(pvtkey.getBytes()), ALGORITHM);

        //Initialize objects
        cipher = Cipher.getInstance(TRANSFORMATION);
        IVParamSpec = new IvParameterSpec(IV);

    } catch (NoSuchAlgorithmException e) {
        Log.i(Lams4gApp.TAG, "No such algorithm " + ALGORITHM);
    } catch (NoSuchPaddingException e) {
        System.out.println( "No such padding PKCS7"+ e);
    }
}
/**
Encryptor.

@text String to be encrypted
@return Base64 encrypted text

*/
public String encrypt(byte[] text) {

    byte[] encryptedData;

    try {

        cipher.init(Cipher.ENCRYPT_MODE, password, IVParamSpec);
        encryptedData = cipher.doFinal(text);

    } catch (InvalidKeyException e) {
        System.out.println( "Invalid key  (invalid encoding, wrong length, uninitialized, etc)."+ e);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        System.out.println( "Invalid or inappropriate algorithm parameters for " + ALGORITHM+ e);
        return null;
    } catch (IllegalBlockSizeException e) {
        System.out.println( "The length of data provided to a block cipher is incorrect"+ e);
        return null;
    } catch (BadPaddingException e) {
        System.out.println( "The input data but the data is not padded properly."+ e);
        return null;
    }               
    return Base64.encodeToString(encryptedData,Base64.DEFAULT);

}

和iOS代码一样 -

- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
    char keyPtr[kCCKeySizeAES128 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    //keyPtr = [58,-43,46,33,-105,83,-80,-5,99,59,2,109,63,89,-59,-91];
    key = [key MD5];
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    char ivPtr[kCCBlockSizeAES128 + 1];
    bzero(ivPtr, sizeof(ivPtr));
    if (iv) {
        [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    }

    NSUInteger dataLength = [self length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          ivPtr,
                                          [self bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    return nil;
}

如果我在两个代码中都不使用MD5 digest,结果会相同。但是,如果我在Android和iOS中使用MD5,则结果会有所不同。如果我使用128 bit,结果完全相同,这只是256中的问题。

请帮忙。

1 个答案:

答案 0 :(得分:0)

这有很多问题。

  1. 在您的iOS代码中,您似乎正在调用MD5方法 key,这只是NSString。你需要实现MD5 该课程的摘要。 Example implementation
  2. 在您的Java代码中,您对密钥和密钥使用相同的值 四。这非常危险,可能导致密钥泄露 值。为IV使用唯一的,随机的,不可预测的值 用给定密钥加密的每条消息。
  3. MD5是一个非常弱的(事实上,已弃用且不被认为是“加密安全”)摘要功能。在这种情况下,您将其用作密钥派生函数(KDF),以从某些“人类可读且可用的”输入(通常是密码)派生 密钥。在商用硬件上,MD5派生很容易在几秒钟内被强制推出。使用bcryptscryptPBKDF2 with HMAC/SHA-256以及高工作系数来获取这些密钥。