描述
1. 什么是对称式加密和非对称式加密
对称式加密:解密方式是加密方式的逆运算,即加密和解密使用的是同一个密钥(初等加密算法)。
非对称式加密:加密和解密需要两个密钥进行,这两个密钥是公钥和私钥(高等加密算法)。
2. 对称式加密和非对称式加密的主要实现方式
对称式加密主要实现方式:
1) DES(Data Encryption Standard):安全性较差,一般不建议使用。
2) 3DES:三重DES,针对DES密钥长度较短的问题,3DES可以实现最少112位、最高168位密钥长度。
3) AES:相比于3DES拥有更好的安全性。
4) PBE(Password Based Encryption):基于口令的加密,结合了DES和AES的优点。
非对称式加密主要实现方式:
1) DH(Diffie-Hellman):密钥交换算法。
2) RSA:基于因子分解。
3) ElGamal:基于离散对数。
4) ECC(Elliptical Curve Cryptography):椭圆曲线加密。
3. 对称式加密在JAVA中的实现(PBE加密):
[java] view plain copypublic class PBE {
private static String src=“security with PBE”;
public static void main(String[] args){
jdkPBE();
}
public static void jdkPBE(){
try {
//初始化盐(扰码)
SecureRandom random=new SecureRandom();
byte[] salt=random.generateSeed(8);
//口令与密钥
String password=“CSDN”;
PBEKeySpec pbeKeySpec=new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory=SecretKeyFactory.getInstance(“PBEWITHMD5andEDS”);
Key key=factory.generateSecret(pbeKeySpec);
//加密
PBEParameterSpec pbeParameterSpec=new PBEParameterSpec(salt,100);//参数1.盐,参数2.迭代次数
Cipher cipher=Cipher.getInstance(“PBEWITHMD5andEDS”);
cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println(“jdk pbe encrypt:” + Base64.encodeToString(result,1));
//解密
cipher.init(Cipher.DECRYPT_MODE,key,pbeParameterSpec);
result=cipher.doFinal(result);
System.out.println(“jdk pbe decrypt:”+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
4. 非对称式加密在JAVA中的实现(DH加密):
[java] view plain copypublic class DH {
private static String src=“security with DH”;
public static void main(String[] args){
jdkDH();
}
public static void jdkDH(){
try {
//初始化发送方密钥
KeyPairGenerator senderKeyPairGenerator=KeyPairGenerator.getInstance(“DH”);
senderKeyPairGenerator.initialize(512);
KeyPair senderKeyPair=senderKeyPairGenerator.generateKeyPair();
byte[] senderPublicKeyEnc=senderKeyPair.getPublic().getEncoded();
//初始化接收方密钥
KeyFactory receiverKeyFactory=KeyFactory.getInstance(“DH”);
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(senderPublicKeyEnc);
PublicKey receiverPublicKey=receiverKeyFactory.generatePublic(x509EncodedKeySpec);
DHParameterSpec dhParameterSpec=((DHPublicKey)receiverPublicKey).getParams();
KeyPairGenerator receiverKeyPairGenerator=KeyPairGenerator.getInstance(“DH”);
receiverKeyPairGenerator.initialize(dhParameterSpec);
KeyPair receiverKeyPair=receiverKeyPairGenerator.generateKeyPair();
PrivateKey receiverPrivateKey=receiverKeyPair.getPrivate();
byte[] receiverPublicKeyEnc =receiverKeyPair.getPublic().getEncoded();
//密钥构建
KeyAgreement receiverKeyAgreement = KeyAgreement.getInstance(“DH”);
receiverKeyAgreement.init(receiverPrivateKey);
receiverKeyAgreement.doPhase(receiverPublicKey, true);
SecretKey receiverDesKey=receiverKeyAgreement.generateSecret(“DES”);
KeyFactory senderKeyFactory=KeyFactory.getInstance(“DH”);
x509EncodedKeySpec=new X509EncodedKeySpec(receiverPublicKeyEnc);
PublicKey senderPublicKey=senderKeyFactory.generatePublic(x509EncodedKeySpec);
KeyAgreement senderKeyAgreement=KeyAgreement.getInstance(“DH”);
senderKeyAgreement.init(senderKeyPair.getPrivate());
senderKeyAgreement.doPhase(senderPublicKey, true);
SecretKey senderDesKey=senderKeyAgreement.generateSecret(“DES”);
if(Objects.equals(receiverDesKey,senderDesKey)){
System.out.println(“双方密钥相同”);
}
//加密
Cipher cipher=Cipher.getInstance(“DES”);
cipher.init(Cipher.ENCRYPT_MODE,senderDesKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println(“jdk dh encrypt:”+Base64.encodeToString(result,1));
//解密
cipher=Cipher.getInstance(“DES”);
cipher.init(Cipher.DECRYPT_MODE,receiverDesKey);
result=cipher.doFinal(result);
System.out.println(“jdk dh encrypt:”+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. 非对称式加密在JAVA中的实现(RSA加密):
[java] view plain copypublic class RSA {
private static String src=“security with rsa”;
public static void main(String[] args){
jdkRSA();
}
public static void jdkRSA(){
try {
//初始化密钥
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance(“RSA”);
keyPairGenerator.initialize(512);//64的整倍数,512~65536
KeyPair keyPair=keyPairGenerator.generateKeyPair();
RSAPrivateKey rsaPrivateKey= (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey rsaPublicKey= (RSAPublicKey) keyPair.getPublic();
System.out.println(“Public key:”+ Base64.encodeToString(rsaPrivateKey.getEncoded(),1));
System.out.println(“Private key:”+ Base64.encodeToString(rsaPrivateKey.getEncoded(),1));
//私钥加密,公钥解密——加密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory=KeyFactory.getInstance(“RSA”);
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher=Cipher.getInstance(“RSA”);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println(“私钥加密,公钥解密——加密:”+Base64.encodeToString(result,1));
//私钥加密,公钥解密——解密
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance(“RSA”);
PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
cipher=Cipher.getInstance(“RSA”);
cipher.init(Cipher.DECRYPT_MODE,publicKey);
result=cipher.doFinal(result);
System.out.println(new String(result));
//公钥加密,私钥解密——加密
x509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance(“RSA”);
publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
cipher=Cipher.getInstance(“RSA”);
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
result=cipher.doFinal(src.getBytes());
System.out.println(“钥加密,私钥解密——加密:”+Base64.encodeToString(result,1));
//公钥加密,私钥解密——解密
pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
keyFactory=KeyFactory.getInstance(“RSA”);
privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
cipher=Cipher.getInstance(“RSA”);
cipher.init(Cipher.DECRYPT_MODE,privateKey);
cipher.doFinal(result);
System.out.println(“公钥加密,私钥解密——解密:” + Base64.encodeToString(result, 1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
6、RSA非对称加密算法
唯一广泛接受并实现
数据加密&数字签名
既可公钥加密,私钥解密
又可私钥加密,公钥解密
代码实现
try {
//初始化秘钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(“RSA”);
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
//私钥加密,公钥解密--加密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory factory = KeyFactory.getInstance(“RSA”);
PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(str.getBytes());
System.out.println(“私加公解 rsa en:”+Base64.toBase64String(result));
//私钥加密,公钥解密--解密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
factory = KeyFactory.getInstance(“RSA”);
PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec);
cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] raw = cipher.doFinal(result);
System.out.println(“私加公解 rsa de:”+ new String(raw));
//公钥加密,私钥解密--加密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result2 = cipher.doFinal(str.getBytes());
System.out.println(“公加私解 rsa en:”+Base64.toBase64String(result2));
//公钥加密,私钥解密--解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] raw2 = cipher.doFinal(result2);
System.out.println(“公加私解 rsa de:” + new String (raw2));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
打开APP阅读更多精彩内容
点击阅读全文