加密方法大致分为对称与非对称加密
对称加密:最快速、最简单的加密方式,加密解密使用同一个密钥。
非对称加密:非常安全,但是对比对称加密来说 性能过低。其使用一对密钥:公钥和私钥。私钥由一方安全保管,公钥发送给请求它的人
对称加密
加密解密使用相同密钥的算法,常见加密算法:DES、3DES、AES、PBE等,安全性逐渐增强。
DES加密:简便的加密算法,密钥长度比较短,DES使用56位密钥,24h内可被破解
public class Test0 {
//密钥算法
private static final String KEY_ALGORITHM= "DES";
//加密/解密算法-工作模式-填充模式
private static final String DEFAULT_CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";//默认的加密算法
//利用加密密钥进行加密操作 content-需要加密内容 key-加密密钥
public static String encrypt(String content, String key){
try {
//根据指定的加密算法 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//获取要加密内容的字节
byte[] byteContent = content.getBytes("utf-8");
//利用加密密钥 将 密码器对象初始化:参数一mode:加密/解密模式,参数二:加密后的钥匙(密钥)
cipher.init(Cipher.ENCRYPT_MODE,getSecretKey(key));
byte[] result = cipher.doFinal(byteContent);//加密
return Base64.encodeBase64String(result);//防止乱码,使用Base64编码
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//对钥匙进行加密,生成加密密钥
private static SecretKeySpec getSecretKey(final String key){
//指定算法密钥生成器
KeyGenerator k = null;
try {
//DES密钥算法加密钥匙
k = KeyGenerator.getInstance(KEY_ALGORITHM);
//DES要求密钥长度为56
k.init(56,new SecureRandom(key.getBytes()));
//生成一个密钥
SecretKey secretKey = k.generateKey();
//转换为DES专用密钥
return new SecretKeySpec(secretKey.getEncoded(),KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content, String key){
Cipher cipher = null;
try {
//创建密码器
cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//密码器初始化-解密
cipher.init(Cipher.DECRYPT_MODE,getSecretKey(key));
//加密操作
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
return new String(result,"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Test
public void DESTest(){
String content = "hello,您好";
String key = "sde@5f98H*^hsff%dfs$r344&df8543*er";
System.out.println("content:" + content);
String s1 = Test0.encrypt(content, key);
System.out.println("s1:" + s1);
System.out.println("s2:"+ Test0.decrypt(s1, key));
}
}
3DES加密:在DES基础上时使用三重数据加密算法,提供相对简单的方法增强DES密钥长度,破解的概率小了,但是使用三重数据加密算法可能比较消耗性能
public class Test0 {
//密钥算法
private static final String KEY_ALGORITHM= "DESede";
//加密/解密算法-工作模式-填充模式
private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";//默认的加密算法
//利用加密密钥进行加密操作 content-需要加密内容 key-加密密钥
public static String encrypt(String content, String key){
try {
//根据指定的加密算法 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//获取要加密内容的字节
byte[] byteContent = content.getBytes("utf-8");
//利用加密密钥 将 密码器对象初始化:参数一mode:加密/解密模式,参数二:加密后的钥匙(密钥)
cipher.init(Cipher.ENCRYPT_MODE,getSecretKey(key));
byte[] result = cipher.doFinal(byteContent);//加密
return Base64.encodeBase64String(result);//防止乱码,使用Base64编码
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//对钥匙进行加密,生成加密密钥
private static SecretKeySpec getSecretKey(final String key){
//指定算法密钥生成器
KeyGenerator k = null;
try {
//3DES密钥算法加密钥匙
k = KeyGenerator.getInstance(KEY_ALGORITHM);
k.init(new SecureRandom(key.getBytes()));
//生成一个密钥
SecretKey secretKey = k.generateKey();
//转换为3DES专用密钥
return new SecretKeySpec(secretKey.getEncoded(),KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content, String key){
Cipher cipher = null;
try {
//创建密码器
cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//密码器初始化-解密
cipher.init(Cipher.DECRYPT_MODE,getSecretKey(key));
//加密操作
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
return new String(result,"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Test
public void DESTest(){
String content = "hello,您好";
String key = "sde@5f98H*^hsff%dfs$r344&df8543*er";
System.out.println("content:" + content);
String s1 = Test0.encrypt(content, key);
System.out.println("s1:" + s1);
System.out.println("s2:"+ Test0.decrypt(s1, key));
}
}
AES实现:
public class Test0 {
//密钥算法
private static final String KEY_ALGORITHM= "AES";
//加密/解密算法-工作模式-填充模式
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
//利用加密密钥进行加密操作 content-需要加密内容 key-加密密钥
public static String encrypt(String content, String key){
try {
//根据指定的加密算法 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//获取要加密内容的字节
byte[] byteContent = content.getBytes("utf-8");
//利用加密密钥 将 密码器对象初始化:参数一mode:加密/解密模式,参数二:加密后的钥匙(密钥)
cipher.init(Cipher.ENCRYPT_MODE,getSecretKey(key));
byte[] result = cipher.doFinal(byteContent);//加密
return Base64.encodeBase64String(result);//防止乱码,使用Base64编码
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//对钥匙进行加密,生成加密密钥
private static SecretKeySpec getSecretKey(final String key){
//指定算法密钥生成器
KeyGenerator k = null;
try {
//3DES密钥算法加密钥匙
k = KeyGenerator.getInstance(KEY_ALGORITHM);
k.init(new SecureRandom(key.getBytes()));
//生成一个密钥
SecretKey secretKey = k.generateKey();
//转换为AES专用密钥
return new SecretKeySpec(secretKey.getEncoded(),KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String content, String key){
Cipher cipher = null;
try {
//创建密码器
cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//密码器初始化-解密
cipher.init(Cipher.DECRYPT_MODE,getSecretKey(key));
//加密操作
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
return new String(result,"utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Test
public void DESTest(){
String content = "hello,您好";
String key = "sde@5f98H*^hsff%dfs$r344&df8543*er";
System.out.println("content:" + content);
String s1 = Test0.encrypt(content, key);
System.out.println("s1:" + s1);
System.out.println("s2:"+ Test0.decrypt(s1, key));
}
}
参考:(9条消息) java对称加密与非对称加密_chengbinbbs的博客-CSDN博客_java对称加密和非对称加密