【问题标题】:AES encryption, InvalidKeyException: Unsupported key size: 6 bytes?AES 加密,InvalidKeyException:不支持的密钥大小:6 字节?
【发布时间】:2017-08-28 05:09:18
【问题描述】:

我正在尝试如下加密字符串

public class AES256Cipher {
static byte[] ivBytes = new byte[]{0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
static String EncryptionKey = "abc123";

public static byte[] encrypt(String plainText)
        throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException,
        NoSuchPaddingException,
        InvalidKeyException,
        InvalidAlgorithmParameterException,
        IllegalBlockSizeException,
        BadPaddingException {
    byte[] keyBytes = EncryptionKey.getBytes("UTF-8");

    AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
    byte[] cipherData = cipher.doFinal(plainText.getBytes("UTF-8"));
    Log.e("cipher", Base64.encodeToString(cipherData, Base64.DEFAULT));
    return cipher.doFinal(plainText.getBytes("UTF-8"));
}
}

我遇到了这个异常

java.security.InvalidKeyException: Unsupported key size: 6 bytes
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER$AES.checkSupportedKeySize(OpenSSLCipher.java:686)
at com.android.org.conscrypt.OpenSSLCipher.checkAndSetEncodedKey(OpenSSLCipher.java:442)
at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:272)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:608)
at javax.crypto.Cipher.tryCombinations(Cipher.java:532)
at javax.crypto.Cipher.getSpi(Cipher.java:437)
at javax.crypto.Cipher.init(Cipher.java:909)
at javax.crypto.Cipher.init(Cipher.java:859)
at com.vfirst.util.netwrok.AES256Cipher.encrypt(AES256Cipher.java:36)
at com.vfirst.LoginActivity.onCreate(LoginActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6321)

要加密的字符串

AES256Cipher.encrypt("12345");

【问题讨论】:

  • 错误“invalid key size 6 bytes”如何更清楚?我不明白你为什么要问这个?
  • 将 EncryptionKey "abc123" 的大小增加到 16 个字符。像“abc123abc123abc1”

标签: java encryption aes


【解决方案1】:

AES 允许 128192256 bit 的密钥长度。 换句话说,162432 byte

【讨论】:

  • 感谢上帝,有人知道密码学,不像我
  • 正确!类似“GWS3eDKYYoaZISBxbUINjvhreiiYHSAg”的东西。 32 的长度
【解决方案2】:

AES 仅支持 16、24 或 32 字节的密钥大小...所以您必须更改您的 EncryptionKey。

SecureRandom random = new SecureRandom();
byte[] EncryptionKey = new byte[16];
random.nextBytes(EncryptionKey);

您可以使用上面的代码示例。

【讨论】:

  • 您也应该谈谈密钥处理。如果没有正确的密钥处理,这不是很有用。
  • 您应该将该代码放在要使用加密的位置。
【解决方案3】:

如果您想使用密码,您应该从密码中派生一个 AES 密钥,而不是尝试将密码直接用作密钥。

最简单的方法是使用 SHA-256 对密码进行哈希处理,并将哈希密码用作 AES 密钥。

常用方法(首选)是使用 PBKDF2,例如使用 HMAC-SHA1 从密码生成 AES 密钥(128/192 或 256 位):

byte[] salt = new byte[8];
random.nextBytes(salt);
KeySpec spec = new PBEKeySpec(EncryptionKey.toCharArray(), salt, 65536, 128);
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] keyBytes = f.generateSecret(spec).getEncoded();

请注意,如果您想稍后从密码生成相同的密钥,则必须存储随机盐。

【讨论】:

    猜你喜欢
    • 2011-04-21
    • 2011-05-23
    • 1970-01-01
    • 2017-04-26
    • 2015-03-13
    • 2015-05-04
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多