The example in this article describes the AES encryption method in JAVA. Share it with everyone for your reference. The details are as follows:
java code:
KeyGenerator kg = KeyGenerator.getInstance("AES"); //获取密匙生成器 kg.init(256); //初始化 //DES算法必须是56位 //DESede算法可以是112位或168位 //AES算法可以是128、192、256位 SecretKey key = kg.generateKey(); //生成密匙,可用多种方法来保存密匙
Encryption:
Cipher cp = Cipher.getInstance("AES"); //创建密码器 cp.init(Cipher.ENCRYPT_MODE, key); //初始化 String str = "我是需要被加密的明文"; byte [] ptext = str.getBytes("UTF8"); byte [] ctext = cp.doFinal(ptext); //加密
Decryption:
Cipher cp = Cipher.getInstance("AES"); //创建密码器 cp.init(Cipher.DECRYPT_MODE, key); //初始化 byte [] ptext = cp.doFinal(ctext); //解密 String str = new String(ptext, "UTF8"); //重新显示明文
I hope this article will be helpful to everyone’s java programming.
For more related articles on the analysis of AES encryption method examples in JAVA, please pay attention to the PHP Chinese website!