AES/ CBC/ PKCS5Padding 加密解密算法和 SHA-1 哈希算法是否适用于所有类型的 Android 设备

Che*_*eng 5 encryption android cryptography

我曾尝试在 Nexus 5 中运行以下AES/CBC/PKCS5Padding加密和解密代码,使用SHA-1作为密钥生成。到目前为止,它运行良好。

但是,我唯一担心的是,AES/ CBC/ PKCS5Padding 加密解密算法和 SHA-1 哈希算法是否适用于所有类型的 Android 设备?

以下代码是否有可能无法在某些 Android 设备上运行?如果是这样,是否有任何回退计划?

AES/ CBC/ PKCS5Padding

// http://stackoverflow.com/questions/3451670/java-aes-and-using-my-own-key
public static byte[] generateKey(String key) throws GeneralSecurityException, UnsupportedEncodingException {
    byte[] binary = key.getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    binary = sha.digest(binary);
    // Use only first 128 bit.
    binary = Arrays.copyOf(binary, 16);
    return binary;
}

// http://stackoverflow.com/questions/17322002/what-causes-the-error-java-security-invalidkeyexception-parameters-missing
public static String encrypt(byte[] key, String value) throws GeneralSecurityException {
    // Argument validation.
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }

    // Setup AES tool.
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));

    // Do the job with AES tool.
    byte[] original = value.getBytes(Charset.forName("UTF-8"));
    byte[] binary = cipher.doFinal(original);
    return Base64.encodeToString(binary, Base64.DEFAULT);
}

// // http://stackoverflow.com/questions/17322002/what-causes-the-error-java-security-invalidkeyexception-parameters-missing
public static String decrypt(byte[] key, String encrypted) throws GeneralSecurityException {
    // Argument validation.
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }

    // Setup AES tool.
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));

    // Do the job with AES tool.
    byte[] binary = Base64.decode(encrypted, Base64.DEFAULT);
    byte[] original = cipher.doFinal(binary);
    return new String(original, Charset.forName("UTF-8"));
}
Run Code Online (Sandbox Code Playgroud)

用法

byte[] key = generateKey("my secret key");
String ciphertext = encrypt(key, "my plain content");
String plainContent = decrypt(key, ciphertext);
Run Code Online (Sandbox Code Playgroud)

Maa*_*wes 5

不,它不太可能会失败。Android API 是从 Java API 派生而来的。Java API 包含了"AES/CBC/PKCS5Padding"自 1.4 版以来的版本。

至于"SHA-1",那是一种更古老的算法,从一开始就得到支持。

注意不要使用"PKCS7Padding"。Java"PKCS5Padding"用作替代品,"PKCS7Padding"即使它意味着相同的东西,支持也可能是粗略的。


请注意,您应该使用基于密码的加密 (PBE) 而不是 AES/CBC 和 SHA-1。特别是使用 SHA-1 作为密钥派生方法特别危险,因为您不使用盐或工作因子作为一个好的基于密码的密钥派生函数,如 PBKDF2 应该。基本上只有在你知道你的密码包含足够的熵时才这样做。

不过,对同一个键使用全零 IV 会更糟(如评论中所述)。它可以让攻击者找到重复(起始块)纯文本输入。始终建议使用经过身份验证的加密(例如使用 HMAC-SHA-1),并且或多或少需要传输模式加密(与无法进行明文/填充预言机攻击的就地加密相反)。