当我尝试使用相同的密钥再次加密密文时,它会产生原始明文。
使用的算法是带有COUNTER MODE的AES。Key和IV保持不变。
这是算法应该表现的方式吗?如果,作为Cipher.init () 的第一个参数给出的Cipher.ENCRYTMODE有什么用?
这是我测试的示例程序,
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptionTest {
public static void main(String[] args) throws Exception {
SecretKeySpec key = null;
IvParameterSpec ivSpec = null;
byte[] keyBytes = "usethiskeyusethiusethiskeyusethi".getBytes();
byte[] ivBytes = "usethisIusethisI".getBytes();
key = new SecretKeySpec(keyBytes, "AES"); //No I18N
ivSpec = new IvParameterSpec(ivBytes);
Cipher AesCipher = Cipher.getInstance("AES/CTR/NoPadding");
byte[] byteText = "Your Plain Text Here".getBytes();
AesCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
byte[] byteCipherText = AesCipher.doFinal(byteText);
System.out.println("Encrypted : " + new String(byteCipherText));
AesCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
byte[] bytePlainText = AesCipher.doFinal(byteCipherText);
System.out.println("Double Encrypted : " + new String(bytePlainText));
}
}