import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.codec.binary.Base64;
/*
DH流程:
1.甲方构建密钥对儿,将公钥公布给乙方,将私钥保留;双方约定数据加密算法;乙方通过甲方公钥构建密钥对儿,将公钥公布给甲方,将私钥保留。
2.甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给乙方加密后的数据;乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
3.乙方使用私钥、甲方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥加密数据,发送给甲方加密后的数据;甲方使用私钥、乙方公钥、约定数据加密算法构建本地密钥,然后通过本地密钥对数据解密。
*/
public class DH {
public static final String src="xiaoweiba";
public static void main(String[] args) throws Exception {
jdkDH();
}
public static void jdkDH() throws Exception{
//初始化密钥
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 recieverKeyPairGenerator = KeyPairGenerator .getInstance("DH");
recieverKeyPairGenerator.initialize(dhParameterSpec);
KeyPair recieverKeyPair = senderKeyPairGenerator.generateKeyPair();
PrivateKey recieverPrivateKey = recieverKeyPair.getPrivate();
byte[] recieverPublicKeyEnc = recieverKeyPair.getPublic().getEncoded();
//密钥构建
KeyAgreement recieverKeyAgreement = KeyAgreement.getInstance("DH");
recieverKeyAgreement.init(recieverPrivateKey);
recieverKeyAgreement.doPhase(receiverPublicKey, true);
SecretKey recieverDESKey = recieverKeyAgreement.generateSecret("DES");
KeyFactory senderKeyFactory = KeyFactory.getInstance("DH");
x509EncodedKeySpec = new X509EncodedKeySpec(recieverPublicKeyEnc);
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(senderDESKey, recieverDESKey)){
System.out.println("双方密钥相同");
}
//加密
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,senderDESKey);
byte[] result = cipher.doFinal(src.getBytes());
//commons-codec.jar
System.out.println("jdk des encrypt:"+Base64.encodeBase64String(result));
//解密
cipher.init(Cipher.DECRYPT_MODE,recieverDESKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt:"+ new String(result));
}
}
结果:
双方密钥相同
jdk des encrypt:vCdW7pmmzX3qqsVmPbv9sA==
jdk des decrypt:xiaoweiba
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
/* RSA流程:
1、甲乙商量好算法
2、甲方产生一个密钥对,一个公钥一个私钥,用私钥加密,把公钥给乙方,乙方用该公钥解密
3、乙方用该公钥加密,把密码给乙方,甲方用私钥解密*/
public class RSA {
public static final String src="xiaoweiba";
public static void main(String[] args) throws Exception {
jdkRSA();
}
public static void jdkRSA() throws Exception{
//初始化密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
System.out.println("Public key:"+Base64.encodeBase64String(rsaPublicKey.getEncoded()));
System.out.println("Private key:"+Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
//私钥加密,公钥解密-加密
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("jdk des encrypt:"+ Base64.encodeBase64String(result));
//私钥加密,公钥解密-解密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE,publicKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt:"+ 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("jdk des encrypt:"+ Base64.encodeBase64String(result));
//公钥加密,私钥解密-解密
pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE,privateKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt:"+ new String(result));
}
}
结果:
jdk des encrypt:aq/Zkv0ivSKu7xaTGlNk2zxmWTdMpSZCQER0ZfnTp/XwiZ0nWoIRIZauldQXoe5c8pkM38oO6g0arCPuA8hG9A==
jdk des decrypt:xiaoweiba
最近在整理一些资源工具,放在网站分享 http://tools.maqway.com
欢迎关注公众号:麻雀唯伊 , 不定时更新资源文章,生活优惠,或许有你想看的