use*_*860 10 java security exchange-server key
我正在开发一个Java的个人项目,涉及通过不安全的渠道发送敏感数据.我需要知道如何使用其库在Java中实现Diffie Hellman密钥交换(DHKE).我知道关于它的所有加密理论所以不需要详细说明,我只需要一个非常基本的实现,所以我可以让2个程序共享一个密钥.我从java2s.com获得了示例,但它并不完整:
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;
public class Main {
public final static int pValue = 47;
public final static int gValue = 71;
public final static int XaValue = 9;
public final static int XbValue = 14;
public static void main(String[] args) throws Exception {
BigInteger p = new BigInteger(Integer.toString(pValue));
BigInteger g = new BigInteger(Integer.toString(gValue));
BigInteger Xa = new BigInteger(Integer.toString(XaValue));
BigInteger Xb = new BigInteger(Integer.toString(XbValue));
int bitLength = 512; // 512 bits
SecureRandom rnd = new SecureRandom();
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p, g);
}
public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
DHParameterSpec param = new DHParameterSpec(p, g);
kpg.initialize(param);
KeyPair kp = kpg.generateKeyPair();
KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
DHPublicKeySpec.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么继续这个?任何人都可以帮我完成剩余的代码吗?
以下代码使用 Elliptic Curve Diffie-Hellman 生成并共享 128 位密钥和 AES 进行加密。
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.*;
public class AESSecurityCap {
private PublicKey publickey;
KeyAgreement keyAgreement;
byte[] sharedsecret;
String ALGO = "AES";
AESSecurityCap() {
makeKeyExchangeParams();
}
private void makeKeyExchangeParams() {
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("EC");
kpg.initialize(128);
KeyPair kp = kpg.generateKeyPair();
publickey = kp.getPublic();
keyAgreement = KeyAgreement.getInstance("ECDH");
keyAgreement.init(kp.getPrivate());
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
}
}
public void setReceiverPublicKey(PublicKey publickey) {
try {
keyAgreement.doPhase(publickey, true);
sharedsecret = keyAgreement.generateSecret();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
public String encrypt(String msg) {
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(msg.getBytes());
return new BASE64Encoder().encode(encVal);
} catch (BadPaddingException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return msg;
}
public String decrypt(String encryptedData) {
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
return new String(decValue);
} catch (BadPaddingException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
return encryptedData;
}
public PublicKey getPublickey() {
return publickey;
}
protected Key generateKey() {
return new SecretKeySpec(sharedsecret, ALGO);
}
}
Run Code Online (Sandbox Code Playgroud)
扩展您自己的类以添加 AES 加密功能
public class Node extends AESSecurityCap {
//your class
}
Run Code Online (Sandbox Code Playgroud)
最后,使用加密的方法
public class Main {
public static void main(String[] args) throws IOException {
Node server = new Node();
Node client = new Node();
server.setReceiverPublicKey(client.getPublickey());
client.setReceiverPublicKey(server.getPublickey());
String data = "hello";
String enc = server.encrypt(data);
System.out.println("hello is coverted to "+enc);
System.out.println(enc+" is converted to "+client.decrypt(enc));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
hello is coverted to OugbNvUuylvAr9mKv//nLA==
OugbNvUuylvAr9mKv//nLA== is converted to hello
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)