AES,高级加密标准,用来代替之前的DES,是一种对称分组加密;
密钥长度可以是128、192或者256位;
几个demo:
AES_ECB加密:
/**
* 2023年4月11日上午10:15:23
*/
package testAES;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* @author XWF
*
*/
public class TestAES_ECB {
/**
* @param args
*/
public static void main(String[] args) {
String pass = "1234567890asdfgh"; //加密解密用的密码,长度16或者24或者32
// String pass = "123456789012345678901234";
// String pass = "12345678901234567890123456789012";
String msg = "hello 你好";
try {
byte[] key = pass.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(key, "AES"); //使用AES
//加密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); //参数格式:Names/Modes/Padding,可以组合出多种
cipher.init(Cipher.ENCRYPT_MODE, keyspec); //加密模式
byte[] encodeBytes = cipher.doFinal(msg.getBytes("utf-8"));
System.out.println("AES_ECB加密后:" + new String(encodeBytes));
//解密
cipher.init(Cipher.DECRYPT_MODE, keyspec); //解密模式(对称加密使用相同密码)
byte[] decodeBytes = cipher.doFinal(encodeBytes);
System.out.println("AES_ECB解密后:" + new String(decodeBytes, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
AES_CBC加密:
/**
* 2023年4月11日上午10:24:39
*/
package testAES;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @author XWF
*
*/
public class TestAES_CBC {
/**
* @param args
*/
public static void main(String[] args) {
String pass = "1234567890asdfgh";
String msg = "hello 你好";
try {
byte[] key = pass.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding"); //Modes使用CBC,需要初始化向量IV,填充换个别的试试
IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes("utf-8")); //初始化向量IV,长度只能16
cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv); //CBC模式需要传入初始化向量IV
byte[] encodeBytes = cipher.doFinal(msg.getBytes("utf-8"));
System.out.println("使用AES_CBC加密后:" + new String(encodeBytes));
cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
byte[] decodeBytes = cipher.doFinal(encodeBytes);
System.out.println("使用AES_CBC解密后:" + new String(decodeBytes, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
AES_CFB加密:
/**
* 2023年4月11日上午11:00:29
*/
package testAES;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @author XWF
*
*/
public class TestAES_CFB {
/**
* @param args
*/
public static void main(String[] args) {
String pass = "1122334455667788";
String msg = "hello 你好";
try {
byte[] key = pass.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
//加密
Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/CFB8/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/CFB32/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/CFB64/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("1234567887654321".getBytes("utf-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv);
byte[] encodeBytes = cipher.doFinal(msg.getBytes("utf-8"));
System.out.println("使用AES_CFB加密后:" + new String(encodeBytes));
//解密
cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
byte[] decodeBytes = cipher.doFinal(encodeBytes);
System.out.println("使用AES_CFB解密后:" + new String(decodeBytes, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
AES_OFB加密:
/**
* 2023年4月11日上午11:09:02
*/
package testAES;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @author XWF
*
*/
public class TestAES_OFB {
/**
* @param args
*/
public static void main(String[] args) {
String pass = "1122334455667788";
String msg = "hello 你好";
try {
byte[] key = pass.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
//加密
Cipher cipher = Cipher.getInstance("AES/OFB24/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/OFB40/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/OFB48/PKCS5Padding");
// Cipher cipher = Cipher.getInstance("AES/OFB56/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("1234567887654321".getBytes("utf-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv);
byte[] encodeBytes = cipher.doFinal(msg.getBytes("utf-8"));
System.out.println("使用AES_OFB加密后:" + new String(encodeBytes));
//解密
cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
byte[] decodeBytes = cipher.doFinal(encodeBytes);
System.out.println("使用AES_OFB解密后:" + new String(decodeBytes, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
之前的DES加密也可以使用,将AES换成DES,改变密码长度即可:
/**
* 2023年4月11日上午10:34:13
*/
package testAES;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* @author XWF
*
*/
public class TestDES_CBC {
/**
* @param args
*/
public static void main(String[] args) {
String pass = "12345678"; //DES密码长度是8
String msg = "hello 你好";
try {
byte[] key = pass.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(key, "DES"); //DES
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("87654321".getBytes("utf-8")); //长度也是8
cipher.init(Cipher.ENCRYPT_MODE, keyspec, iv);
byte[] encodeBytes = cipher.doFinal(msg.getBytes("utf-8"));
System.out.println("使用DES_CBC加密后:" + new String(encodeBytes));
cipher.init(Cipher.DECRYPT_MODE, keyspec, iv);
byte[] decodeBytes = cipher.doFinal(encodeBytes);
System.out.println("使用DES_CBC解密后:" + new String(decodeBytes, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
Cipher的参数Names/Modes/Padding可以从官网查看:Java Security Standard Algorithm Names (oracle.com)
AES规范文档:FIPS 197, Advanced Encryption Standard (AES) (nist.gov)