Home > Java > javaTutorial > body text

Example analysis of AES encryption method in JAVA

高洛峰
Release: 2017-01-24 11:10:24
Original
1387 people have browsed it

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(); //生成密匙,可用多种方法来保存密匙
Copy after login

Encryption:

Cipher cp = Cipher.getInstance("AES"); //创建密码器
cp.init(Cipher.ENCRYPT_MODE, key); //初始化
String str = "我是需要被加密的明文";
byte [] ptext = str.getBytes("UTF8");
byte [] ctext = cp.doFinal(ptext); //加密
Copy after login

Decryption:

Cipher cp = Cipher.getInstance("AES"); //创建密码器
cp.init(Cipher.DECRYPT_MODE, key); //初始化
byte [] ptext = cp.doFinal(ctext); //解密
String str = new String(ptext, "UTF8"); //重新显示明文
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template