簡體   English   中英

在 android 中用 16 個字符的初始化向量生成 32 個字符的 AES 字符串

[英]Generate 32 character AES string with 16 character initialization vector in android

我正在使用下面的 class 在 android 中生成 AES 256 密鑰、初始化向量、加密和解密。我面臨的問題很少

1) 當我調用 getInitializationVector() 時,它返回 24 個字符而不是 16 個字符。(例如: WoiUFsQpizjG705OXja1Jw==
2) 當我調用 generateKey() 時,它返回 44 個字符而不是 32 個字符。(例如: tEf+dcrzI4x+kSMS8UZxjwziGySMMkDxO0aVgsj0oBs=
3)下面的class是對稱的方法,如何讓它不對稱。

您可以看到默認的 textKey 和 testIV。 但我無法創建它。 我在堆棧溢出中搜索了解決方案,但是,所有的解決方案都返回相同長度的文本。 有誰知道如何做到這一點? 我做錯了嗎? 提前感謝您的寶貴時間。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM