package com.nelson.des;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
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.xml.bind.annotation.adapters.HexBinaryAdapter;
import org.apache.commons.codec.binary.Hex;
public class DESTest {
//定义一个要加密的字符串
private static final String password = "xiehuaxin123456";
public static void main(String[] args) {
// TODO Auto-generated method stub
jdkDES();
}
//JDK的实现
public static void jdkDES() {
try {
//1.生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");//Key的生成器
keyGenerator.init(56);//指定keySize
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//2.KEY转换
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);//实例化DESKey秘钥的相关内容
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");//实例一个秘钥工厂,指定加密方式
Key convertSecretKey = factory.generateSecret(desKeySpec);
//3.加密 DES/ECB/PKCS5Padding--->算法/工作方式/填充方式
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//通过Cipher这个类进行加解密相关操作
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(password.getBytes());//输入要加密的内容
System.out.println("加密的结果:" + Hex.encodeHexString(result));
//4.解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(result);
System.out.println("解密结果:" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//BC的实现
public static void bcDES() {
try {
Security.addProvider(new BouncyCastleProvider());
//1.生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES","BC");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//2.转换KEY
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
Key convertKey = factory.generateSecret(desKeySpec);
//3.加密
Cipher cipher = Cipher.getInstance("/DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertKey);
byte[] result = cipher.doFinal(password.getBytes());
System.out.println(Hex.encodeHexString(result));
//4.解密
cipher.init(Cipher.DECRYPT_MODE,convertKey);
result = cipher.doFinal(result);
System.out.println(new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.1 三重DES相对于DES的好处有:秘钥长度增强、迭代次数提高
package com.nelson.des;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Hex;
public class ThreeDESTest {
private static final String password = "xiehuaxin1124";
public static void main(String[] args) {
jdkThreeDES();
}
public static void jdkThreeDES() {
try {
//1.生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DESEde");
// keyGenerator.init(168);
keyGenerator.init(new SecureRandom());
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//2.转换KEY
DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESEde");
Key convertKey = factory.generateSecret(deSedeKeySpec);
//3.加密
Cipher cipher = Cipher.getInstance("DESEde/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertKey);
byte[] result = cipher.doFinal(password.getBytes());
System.out.println("加密后:" + Hex.encodeHexString(result));
//4.解密
cipher.init(Cipher.DECRYPT_MODE, convertKey);
result = cipher.doFinal(result);
System.out.println("解密后:" + new String(result));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
AES使用的最多的对称加密算法,AES的优势之一是至今尚未被破解。AES通常用于移动通信系统加密以及基于SSH协议的软件。
package com.nelson.jiami;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
public class AESTest {
private static String password = "xiehuaxin";
public static void main(String[] args) {
jdkAES();
}
public static void jdkAES() {
try {
//1.生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(new SecureRandom());
SecretKey secretKey = keyGenerator.generateKey();
byte[] byteKey = secretKey.getEncoded();
//2.转换KEY
Key key = new SecretKeySpec(byteKey,"AES");
//3.加密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(password.getBytes());
System.out.println("加密后:" + Hex.encodeHexString(result));
//4.解密
cipher.init(Cipher.DECRYPT_MODE, key);
result = cipher.doFinal(result);
System.out.println("解密后:" + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
PBE(Password Based Encryption 基于口令加密)算法结合了消息摘要算法和对称加密算法的优点。但是口令还是有可能被穷举出来,所以一般通过加盐salt的方式进行加密。PBE算法只是对已有算法进行了包装,通常有JDK和BC 两种实现。
package com.nelson.jiami;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Hex;
public class PBETest {
private static String src = "xiehuaxin";//待加密的字符串
public static void main(String[] args) {
jdkPBE();
}
public static void jdkPBE() {
try {
//1. 初始化盐
SecureRandom secureRandom = new SecureRandom();
byte[] salt = secureRandom.generateSeed(8);
//2. 口令与秘钥
//2.1 定义密码
String password = "NELSON";//这个是加密用的口令
//2.2 把密码转换成秘钥
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
Key key = factory.generateSecret(pbeKeySpec);
//3. 加密
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, 100);//100是你选择迭代的次数
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("加密后:" + Hex.encodeHexString(result));
//4.解密
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
result = cipher.doFinal(result);
System.out.println("解密后:" + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}