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. 对称式加密和非对称式加密的对比
非对称加密与对称加密相比,其安全性更好:对称加密的通信双方使用相同的秘钥,如果一方的秘钥遭泄露,那么整个通信就会被破解。而非对称加密使用一对秘钥,一个用来加密,一个用来解密,而且公钥是公开的,秘钥是自己保存的,不需要像对称加密那样在通信之前要先同步密钥。
4. 对称式加密在Java中的实现(PBE加密):
public class PBE {
private static Stringsrc="security with PBE";
public static void main(String[]args){
jdkPBE();
}
public static void jdkPBE(){
try {
//初始化盐(扰码)
SecureRandom random=newSecureRandom();
byte[]salt=random.generateSeed(8);
//口令与密钥
Stringpassword="CSDN";
PBEKeySpecpbeKeySpec=new PBEKeySpec(password.toCharArray());
SecretKeyFactoryfactory=SecretKeyFactory.getInstance("PBEWITHMD5andEDS");
Keykey=factory.generateSecret(pbeKeySpec);
//加密
PBEParameterSpecpbeParameterSpec=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();
}
}
}
5. 非对称式加密在JAVA中的实现(DH加密):
public class DH {
private static Stringsrc="security with DH";
public static void main(String[]args){
jdkDH();
}
public static void jdkDH(){
try {
//初始化发送方密钥
KeyPairGeneratorsenderKeyPairGenerator=KeyPairGenerator.getInstance("DH");
senderKeyPairGenerator.initialize(512);
KeyPairsenderKeyPair=senderKeyPairGenerator.generateKeyPair();
byte[]senderPublicKeyEnc=senderKeyPair.getPublic().getEncoded();
//初始化接收方密钥
KeyFactoryreceiverKeyFactory=KeyFactory.getInstance("DH");
X509EncodedKeySpecx509EncodedKeySpec=new X509EncodedKeySpec(senderPublicKeyEnc);
PublicKeyreceiverPublicKey=receiverKeyFactory.generatePublic(x509EncodedKeySpec);
DHParameterSpecdhParameterSpec=((DHPublicKey)receiverPublicKey).getParams();
KeyPairGeneratorreceiverKeyPairGenerator=KeyPairGenerator.getInstance("DH");
receiverKeyPairGenerator.initialize(dhParameterSpec);
KeyPairreceiverKeyPair=receiverKeyPairGenerator.generateKeyPair();
PrivateKeyreceiverPrivateKey=receiverKeyPair.getPrivate();
byte[]receiverPublicKeyEnc =receiverKeyPair.getPublic().getEncoded();
//密钥构建
KeyAgreementreceiverKeyAgreement = KeyAgreement.getInstance("DH");
receiverKeyAgreement.init(receiverPrivateKey);
receiverKeyAgreement.doPhase(receiverPublicKey,true);
SecretKeyreceiverDesKey=receiverKeyAgreement.generateSecret("DES");
KeyFactorysenderKeyFactory=KeyFactory.getInstance("DH");
x509EncodedKeySpec=newX509EncodedKeySpec(receiverPublicKeyEnc);
PublicKeysenderPublicKey=senderKeyFactory.generatePublic(x509EncodedKeySpec);
KeyAgreementsenderKeyAgreement=KeyAgreement.getInstance("DH");
senderKeyAgreement.init(senderKeyPair.getPrivate());
senderKeyAgreement.doPhase(senderPublicKey, true);
SecretKeysenderDesKey=senderKeyAgreement.generateSecret("DES");
if(Objects.equals(receiverDesKey,senderDesKey)){
System.out.println("双方密钥相同");
}
//加密
Ciphercipher=Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,senderDesKey);
byte[]result=cipher.doFinal(src.getBytes());
System.out.println("jdk dhencrypt:"+Base64.encodeToString(result,1));
//解密
cipher=Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,receiverDesKey);
result=cipher.doFinal(result);
System.out.println("jdk dhencrypt:"+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
6. 非对称式加密在JAVA中的实现(RSA加密):
public class RSA {
private static Stringsrc="security with rsa";
public static void main(String[]args){
jdkRSA();
}
public static void jdkRSA(){
try {
//初始化密钥
KeyPairGeneratorkeyPairGenerator=KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);//64的整倍数,512~65536
KeyPairkeyPair=keyPairGenerator.generateKeyPair();
RSAPrivateKeyrsaPrivateKey= (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKeyrsaPublicKey= (RSAPublicKey) keyPair.getPublic();
System.out.println("Public key:"+Base64.encodeToString(rsaPrivateKey.getEncoded(),1));
System.out.println("Private key:"+ Base64.encodeToString(rsaPrivateKey.getEncoded(),1));
//私钥加密,公钥解密——加密
PKCS8EncodedKeySpecpkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactorykeyFactory=KeyFactory.getInstance("RSA");
PrivateKeyprivateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Ciphercipher=Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[]result=cipher.doFinal(src.getBytes());
System.out.println("私钥加密,公钥解密——加密:"+Base64.encodeToString(result,1));
//私钥加密,公钥解密——解密
X509EncodedKeySpecx509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory=KeyFactory.getInstance("RSA");
PublicKeypublicKey=keyFactory.generatePublic(x509EncodedKeySpec);
cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,publicKey);
result=cipher.doFinal(result);
System.out.println(newString(result));
//公钥加密,私钥解密——加密
x509EncodedKeySpec=newX509EncodedKeySpec(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=newPKCS8EncodedKeySpec(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();
}
}
}