1、对称加密:采用单钥对信息进行加密和解密,即同一个秘钥既可以对信息进行加密,也可以进行解密。此类型称之为对称加密。特点速度快,常用于对大量数据信息或文件加密时使用。常用例子:DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES。
优点:速度快,对称性加密通常在消息发送方需要加密大量数据时使用,算法公开、计算量小、加密速度快、加密效率高。
缺点:在数据传送前,发送方和接收方必须商定好秘钥,然后 使双方都能保存好秘钥。其次如果一方的秘钥被泄露,那么加密信息也就不安全了。另外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的唯一秘 钥,这会使得收、发双方所拥有的钥匙数量巨大,密钥管理成为双方的负担
AES:密钥的长度可以为128、192和256位,也就是16个字节、24个字节和32个字节
DES:密钥的长度64位,8个字节。
1.AES加密算法
package com.yirong;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class Test_AES {
//加密的内容
private static String src = "security AES";
public static void main(String[] args) {
jdkAES();
}
public static void jdkAES() {
try {
//创建AES的Key生产者
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
// 利用用户密码作为随机数初始化出
keyGenerator.init(new SecureRandom());
// 根据用户密码,生成一个密钥
SecretKey secretKey = keyGenerator.generateKey();
// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回null
byte[] keyBytes = secretKey.getEncoded();
// 转换为AES专用密钥
Key key = new SecretKeySpec(keyBytes, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, key);
//加密
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("jdk aes encrypt:" + Base64.encode(result));
// 初始化为解密模式的密码器
cipher.init(Cipher.DECRYPT_MODE, key);
//解密
result = cipher.doFinal(result);
System.out.println("jdk aes encrypt:" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.DES加密算法
package com.yirong;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Hex;
public class Test_DES {
//加密的内容
private static String src = "security sha";
public static void main(String[] args) {
jdk3DES();
jdkDES();
}
public static void jdkDES() {
try {
//生成一个key
KeyGenerator keyGenerator;
keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
//得到key数组
byte[] bytesKey = secretKey.getEncoded();
//key转换
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
//加密方式
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
//生成的秘钥
Key secret = factory.generateSecret(desKeySpec);
//加密 加解密算法,工作模式,填充方式
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("jdk des encrypt: " + Hex.encodeHexString(result));
//解密
cipher.init(Cipher.DECRYPT_MODE, secret);
result = cipher.doFinal(result);
System.out.println("jdk des encrypt: " + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void jdk3DES() {
try {
//生成一个key
KeyGenerator keyGenerator;
keyGenerator = KeyGenerator.getInstance("DESede");
//生成的长度
//keyGenerator.init(128);
keyGenerator.init(new SecureRandom());
SecretKey secretKey = keyGenerator.generateKey();
//得到key数组
byte[] bytesKey = secretKey.getEncoded();
//key转换
DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
//加密方式
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
//生成的秘钥
Key secret = factory.generateSecret(desKeySpec);
//加密 加解密算法,工作模式,填充方式
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("jdk des encrypt: " + Hex.encodeHexString(result));
//解密
cipher.init(Cipher.DECRYPT_MODE, secret);
result = cipher.doFinal(result);
System.out.println("jdk des encrypt: " + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.PBE加密算法
package com.yirong;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class Test_PBE {
public static void main(String[] args) {
jdkPbe();
}
//加密的内容
private static String src = "security pbe";
public static void jdkPbe() {
try {
//初始化盐
SecureRandom random = new SecureRandom();
byte[] salt = random.generateSeed(8);
//口令密钥
String password = "123";
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
Key key = factory.generateSecret(pbeKeySpec);
//加密
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
cipher.init(Cipher.ENCRYPT_MODE, key,parameterSpec);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("jdk pbe encrypt:" + Base64.encode(result));
//解密
cipher.init(Cipher.DECRYPT_MODE, key,parameterSpec);
result = cipher.doFinal(result);
System.out.println("jdk pde encrypt:" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}