在 android 中生成具有 16 个字符初始化向量的 32 个字符的 AES 字符串

Generate 32 character AES string with 16 character initialization vector in android

我在下面使用 class 在 android 中生成 AES 256 密钥、初始化向量、加密和解密。我面临的问题很少

1) 当我调用 getInitializationVector() 时,它 return 24 个字符而不是 16 个字符。(例如:WoiUFsQpizjG705OXja1Jw==
2) 当我调用 generateKey() 时,它 returns 44 个字符而不是 32 个。(例如:tEf+dcrzI4x+kSMS8UZxjwziGySMMkDxO0aVgsj0oBs=
3)下面的class是对称的方法,如何让它不对称。

你可以看到默认的textKey和testIV。但我无法创建它。我在堆栈溢出中搜索了解决方案,但是,所有的都是 return 给我相同长度的文本。有谁知道如何做到这一点?我做错了吗? 提前感谢您的宝贵时间。

public class CryptoDataHandler {

    //32 characters
    String testKey = "82a645babc5cd41c9a2cb4d0d3ba17ad";
    //16 characters
    String testIV = "acf30ad32b693849";

    String edType = "AES";
    String chiperInstanceType = "AES/CBC/PKCS5PADDING";

    private static CryptoDataHandler instance = null;

    public static CryptoDataHandler getInstance() {

        if (instance == null) {
            Security.setProperty("crypto.policy", "unlimited");
            instance = new CryptoDataHandler();
        }
        return instance;
    }

    public String encrypt(String message) throws NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException,
            BadPaddingException, InvalidKeyException,
            UnsupportedEncodingException, InvalidAlgorithmParameterException {

        byte[] srcBuff = message.getBytes(StandardCharsets.UTF_8);
        //here using substring because AES takes only 16 or 24 or 32 byte of key
        SecretKeySpec skeySpec = new
                SecretKeySpec(testKey.substring(0, 32).getBytes(), edType);
        IvParameterSpec ivSpec = new
                IvParameterSpec(testIV.substring(0, 16).getBytes());
        Cipher ecipher = Cipher.getInstance(chiperInstanceType);
        ecipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
        byte[] dstBuff = ecipher.doFinal(srcBuff);
        return Base64.encodeToString(dstBuff, Base64.DEFAULT);
    }

    public String decrypt(String encrypted) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException, UnsupportedEncodingException {

        SecretKeySpec skeySpec = new
                SecretKeySpec(testKey.substring(0, 32).getBytes(), edType);
        IvParameterSpec ivSpec = new
                IvParameterSpec(testIV.substring(0, 16).getBytes());
        Cipher ecipher = Cipher.getInstance(chiperInstanceType);
        ecipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
        byte[] raw = Base64.decode(encrypted, Base64.DEFAULT);
        byte[] originalBytes = ecipher.doFinal(raw);
        return new String(originalBytes, StandardCharsets.UTF_8);
    }

    public String generateKey() {

        try {
            Key key;
            SecureRandom rand = new SecureRandom();
            KeyGenerator generator = KeyGenerator.getInstance(edType);
            generator.init(256, rand);
            key = generator.generateKey();
            return Base64.encodeToString(key.getEncoded(), Base64.DEFAULT);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }


    public String getInitializationVector() {
        byte[] IV = new byte[16];
        SecureRandom random = new SecureRandom();
        random.nextBytes(IV);
        return Base64.encodeToString(IV, Base64.DEFAULT);
    }
}

好吧,我找到了解决办法。在这里张贴。如果对其他人有帮助。

我刚刚修改了我的 KEY 和 IV 生成方法,精确的 32 和 16 个字符。我已经测试了加密和解密,它工作正常。

public String generateKey() {

    try {
        Key key;
        SecureRandom rand = new SecureRandom();
        KeyGenerator generator = KeyGenerator.getInstance(edType);
        generator.init(256, rand);
        key = generator.generateKey();
        return Base64.encodeToString(key.getEncoded(), Base64.DEFAULT).subString(0,32);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return null;
}


public String getInitializationVector() {
    byte[] IV = new byte[16];
    SecureRandom random = new SecureRandom();
    random.nextBytes(IV);
    return Base64.encodeToString(IV, Base64.DEFAULT).subString(0,16);
}