[NOTE-ENCRYPT]Java实现非对称加密RSA、DH算法

着重介绍Java语言实现RSA、DH算法,两项均为非对称加密算法,需要生成公私钥对,关于算法的具体实现细节,可以参考文章非对称加密算法与RSA详解了解算法详情

关于此篇文章均可以git平台下载方便运行查看:https://git.oschina.net/accacc/java_demos

RSA/DH类详解

RSA算法实现

生成公私钥对

String content = "congcong.us";

//构建秘钥,这里选择RSA方式;initialize(512)RSA秘钥位数,这里需要注意
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();

byte[] privateKey = keyPair.getPrivate().getEncoded();
byte[] publicKey = keyPair.getPublic().getEncoded();

System.out.println("privateKey:"+Base64.getEncoder().encodeToString(privateKey));
System.out.println("publicKey:"+Base64.getEncoder().encodeToString(publicKey));

私钥加密公钥解密之私钥加密

public static byte[] encryptByPriKey(byte[] content, byte[] privateKey) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, priKey);
    byte[] encodeRes = cipher.doFinal(content);

    return encodeRes;
}

私钥加密公钥解密之公钥解密

public static byte[] decryptByPubKey(byte[] content, byte[] publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    byte[] decodeRes = cipher.doFinal(content);

    return decodeRes;
}

公钥加密私钥解密之公钥加密

public static byte[] encryptByPubKey(byte[] content, byte[] publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKey));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    byte[] encodeRes = cipher.doFinal(content);

    return encodeRes;
}

公钥加密私钥解密之私钥解密

public static byte[] decryptByPriKey(byte[] content, byte[] privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    Key priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, priKey);
    byte[] decodeRes = cipher.doFinal(content);

    return decodeRes;
}

DH算法实现

公私钥对生成

// 初始化发送发秘钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
keyPairGenerator.initialize(512);
KeyPair senderKeyPair = keyPairGenerator.generateKeyPair();
byte[] senderPublicKeyEnc = senderKeyPair.getPublic().getEncoded();

// 初始化接收方秘钥
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(senderPublicKeyEnc);

// ---通过发送方的公钥获取公钥参数规范
KeyFactory receiverKeyFactory = KeyFactory.getInstance("DH");
PublicKey receiverPublicKey = receiverKeyFactory.generatePublic(x509EncodedKeySpec);
DHParameterSpec dhParameterSpec = ((DHPublicKey) receiverPublicKey).getParams();

// ---通过发送方的公钥参数来生成接收方的秘钥
keyPairGenerator.initialize(dhParameterSpec);
KeyPair receiverKeyPair = keyPairGenerator.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 senderDEStKey = senderKeyAgreement.generateSecret("DES");

if (Objects.equals(senderDEStKey, receiverDESKey)) {
    System.out.println("DH算法,发送发和接收方成功交换了秘钥");
}

加密

Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encodeResult = cipher.doFinal(content);
return encodeResult;

解密

Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodeResult = cipher.doFinal(content);
return decodeResult;
0条留言