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();
}
}
}
評論
查看更多