Java笔记-DH密钥交换获取密钥及AES加解密
Posted IT1995
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java笔记-DH密钥交换获取密钥及AES加解密相关的知识,希望对你有一定的参考价值。
DH密钥交换是这样的:
程序运行截图如下:
D:\\java8\\content\\bin\\java.exe -javaagent:D:\\idea\\content\\lib\\idea_rt.jar=61009:D:\\idea\\content\\bin -Dfile.encoding=UTF-8 -classpath D:\\java8\\content\\jre\\lib\\charsets.jar;D:\\java8\\content\\jre\\lib\\deploy.jar;D:\\java8\\content\\jre\\lib\\ext\\access-bridge-64.jar;D:\\java8\\content\\jre\\lib\\ext\\cldrdata.jar;D:\\java8\\content\\jre\\lib\\ext\\dnsns.jar;D:\\java8\\content\\jre\\lib\\ext\\jaccess.jar;D:\\java8\\content\\jre\\lib\\ext\\jfxrt.jar;D:\\java8\\content\\jre\\lib\\ext\\localedata.jar;D:\\java8\\content\\jre\\lib\\ext\\nashorn.jar;D:\\java8\\content\\jre\\lib\\ext\\sunec.jar;D:\\java8\\content\\jre\\lib\\ext\\sunjce_provider.jar;D:\\java8\\content\\jre\\lib\\ext\\sunmscapi.jar;D:\\java8\\content\\jre\\lib\\ext\\sunpkcs11.jar;D:\\java8\\content\\jre\\lib\\ext\\zipfs.jar;D:\\java8\\content\\jre\\lib\\javaws.jar;D:\\java8\\content\\jre\\lib\\jce.jar;D:\\java8\\content\\jre\\lib\\jfr.jar;D:\\java8\\content\\jre\\lib\\jfxswt.jar;D:\\java8\\content\\jre\\lib\\jsse.jar;D:\\java8\\content\\jre\\lib\\management-agent.jar;D:\\java8\\content\\jre\\lib\\plugin.jar;D:\\java8\\content\\jre\\lib\\resources.jar;D:\\java8\\content\\jre\\lib\\rt.jar;D:\\IDEAProject\\DHDemo\\target\\classes;D:\\newGenRepository\\repository\\commons-codec\\commons-codec\\1.11\\commons-codec-1.11.jar cn.it1995.Test
服务端公钥:MIHgMIGXBgkqhkiG9w0BAwEwgYkCQQD8poLOjhLKuibvzP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Q2iCmxBblC+eUykAgIBgAQzAjEAtjb/+dXXhPMI3xubgp3LipUgNYjMtWok30cn3KxGgkspHEqI35h+TLRmT2CZDopA
客户端计算出的AES密钥:g/tQbIkTCH3I1gXCVNObyyHKsBkWXTsNJIiPG3gVJII=
服务端计算出的AES密钥:g/tQbIkTCH3I1gXCVNObyyHKsBkWXTsNJIiPG3gVJII=
原文:Hello world, 中文
密文:���-�5�5�u�ɫ5>#�Ij>�/hC�
解密:Hello world, 中文
Process finished with exit code 0
程序结构如下:
代码如下:
DataSecret.java
package cn.it1995;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DataSecret {
private static final String SELECT_ALGORITHM = "AES"; //DES、DESede或者AES
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
//生成本地密钥
SecretKey secretKey = new SecretKeySpec(key, SELECT_ALGORITHM);
//加密
Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, SELECT_ALGORITHM);
Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
}
DH.java
package cn.it1995;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.interfaces.DHPublicKey;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public abstract class DH {
private static final String KEY_ALGORITHM = "DH";
private static final int KEY_SIZE = 512;
private static final String PUBLIC_KEY = "DHPublicKey";
private static final String PRIVATE_KEY = "DHPrivateKey";
private static final String SELECT_ALGORITHM = "AES"; //DES、DESede或者AES
/***
* 初始化服务端密钥
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
DHPublicKey publicKey = (DHPublicKey)keyPair.getPublic();
DHPrivateKey privateKey = (DHPrivateKey)keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<>();
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
keyAgree.init(priKey);
keyAgree.doPhase(pubKey, true);
SecretKey secretKey = keyAgree.generateSecret(SELECT_ALGORITHM);
return secretKey.getEncoded();
}
public static byte[] getPublicKey(Map<String, Object> keyMap){
Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
public static byte[] getPrivateKey(Map<String, Object> keyMap){
Key key = (Key) keyMap.get(PRIVATE_KEY);
return key.getEncoded();
}
}
Test.java
package cn.it1995;
import org.apache.commons.codec.binary.Base64;
import java.util.Map;
public class Test {
private static byte[] publicKey1;
private static byte[] privateKey1;
private static byte[] key1;
private static byte[] publicKey2;
private static byte[] privateKey2;
private static byte[] key2;
public static void main(String[] args) throws Exception {
System.getProperties().setProperty("jdk.crypto.KeyAgreement.legacyKDF", "true");
//服务端密钥
Map<String, Object> keyMap1 = DH.initKey();
publicKey1 = DH.getPublicKey(keyMap1);
privateKey1 = DH.getPrivateKey(keyMap1);
System.out.println("服务端公钥:" + Base64.encodeBase64String(publicKey1));
System.out.println("服务端私钥:" + Base64.encodeBase64String(privateKey1));
//客户端密钥
Map<String, Object> keyMap2 = DH.initKey();
publicKey2 = DH.getPublicKey(keyMap2);
privateKey2 = DH.getPrivateKey(keyMap2);
System.out.println("客户端公钥:" + Base64.encodeBase64String(publicKey2));
System.out.println("客户端私钥:" + Base64.encodeBase64String(privateKey2));
//服务端公钥和客户端私钥生成密钥
key1 = DH.getSecretKey(publicKey1, privateKey2);
System.out.println("客户端计算出的AES密钥:" + Base64.encodeBase64String(key1));
//客户端公钥和服务端私钥生成密钥
key2 = DH.getSecretKey(publicKey2, privateKey1);
System.out.println("服务端计算出的AES密钥:" + Base64.encodeBase64String(key2));
//开始进行加密
String string = "Hello world, 中文";
byte[] encrypt = DataSecret.encrypt(string.getBytes(), key1);
byte[] decrypt = DataSecret.decrypt(encrypt, key2);
System.out.println("原文:" + string);
System.out.println("密文:" + new String(encrypt));
System.out.println("解密:" + new String(decrypt));
}
}
以上是关于Java笔记-DH密钥交换获取密钥及AES加解密的主要内容,如果未能解决你的问题,请参考以下文章