如何将参数发送到AES加密器?

时间:2019-08-14 20:20:13

标签: java encryption aes cbc-mode

我必须为Web门户加密一些参数,这些参数通过AES / CBC / PKCS5Padding通过POST接收加密的参数,加密方法是由Web门户创建者(另一个团队)提供的,但是当我使用他们的方法时,结果是网站门户不接受。我假设参数没有很好地提供给该方法。我想念东西吗?

我要求一些固定的IV值,但其他团队告诉我他们使用了随机IV。

public static byte[] Encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(IV);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);                
    byte[] cipherText = cipher.doFinal(plaintext);               
    return cipherText;
}

//这是我构建参数的方式

        String input_text = txtPlainText.getText();
        byte[] plaintext = input_text.getBytes("UTF-8");

        String encodedKey = "6qp4Y?kLmaY8+Fsd";
        byte[] decodedKey =  encodedKey.getBytes();
        SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");

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

        byte[] cipherText = Encrypt(plaintext,key,IV);
        String Encrypted_text = java.util.Base64.getEncoder().encodeToString(cipherText);

        txtEncryptedText.setText(Encrypted_text );

每次使用这种加密方法时,我都会得到不同的值,对于纯文本“系统管理员”,我会得到类似“ U + za3baTIvMpiFgCPCfqoKe + ljDutraUrUTJhroqHPg =”,“ lHT0Kq6GlyzYx2TEyQmqdPp4K4”的信息

0 个答案:

没有答案