java rsa加密是什么?让我们一起来了解一下吧!
java rsa加密是java中的一种非对称加密算法。非对称加密是指加密密钥与加密秘钥不一样。我们经常看见的非对称加密算法有两种,分别是rsa和dsa。
应用rsa的时候我们一般需要公钥与私钥,但我们采用公钥加密的时候,便使用私钥解密;反之,采用私钥加密的时候,便要使用公钥进行解密。在java中,我们先应用genkeypair()函数产生公钥与私钥同时保存在map集合里面。接着,我们以产生的公钥对明文进行加密。针对已加密的密文,我们再利用私钥对其解密。
实战演练,具体步骤如下:
package rsademo;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* @author 一只鲨go
* @title RSA_Test
* @CreateTime 2021-05-13
*/
public class RSA_Algorithm {
private PrivateKey privateKey;
private PublicKey publicKey;
private static String algorithm = "RSA";
private static String signAlgorithm = "MD5withRSA";
public RSA_Algorithm() throws NoSuchAlgorithmException {
//生成密钥对对象
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
//生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//生成公钥
this.publicKey = keyPair.getPublic();
//生成私钥
this.privateKey = keyPair.getPrivate();
}
/**
* 公钥字符串还原为公钥
*
* @param publicKeyString 公钥字符串
* @return 公钥
* @throws Exception
*/
public Key publicKeyStringToKey(String publicKeyString) throws Exception {
byte[] publicBytes = Base64.getDecoder().decode(publicKeyString);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicBytes));
return publicKey;
}
/**
* 私钥字符串还原为私钥
*
* @param privateKeyString 私钥字符串
* @return 私钥
* @throws Exception
*/
public PrivateKey privateKeyStringToKey(String privateKeyString) throws Exception {
byte[] privateBytes = Base64.getDecoder().decode(privateKeyString);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateBytes));
return privateKey;
}
/**
* 返回公钥字节数组
*
* @return
*/
public byte[] publicKeyEncoded() {
return this.publicKey.getEncoded();
}
/**
* 返回私钥字节数组
*
* @return
*/
public byte[] privateKeyEncoded() {
return this.privateKey.getEncoded();
}
/**
* 公钥byteToString转码
*
* @return
*/
public String publicKeyToString() {
return Base64.getEncoder().encodeToString(publicKeyEncoded());
}
/**
* 私钥byteToString转码
*
* @return
*/
public String privateKeyToString() {
return Base64.getEncoder().encodeToString(privateKeyEncoded());
}
/**
* 公钥加密
*
* @param input 明文
* @param publicKey 公钥
* @return 密文字符串
* @throws Exception
*/
public String pkEncoded(String input, String publicKey) throws Exception {
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, publicKeyStringToKey(publicKey));
byte[] cipherText = cipher.doFinal(bytes);
return Base64.getEncoder().encodeToString(cipherText);
}
/**
* 私钥解密
*
* @param cipherText 密文
* @param privateKey 私钥
* @return 明文字符串
* @throws Exception
*/
public String skDecoded(String cipherText, String privateKey) throws Exception {
byte[] cipherbytes = Base64.getDecoder().decode(cipherText);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, privateKeyStringToKey(privateKey));
byte[] input = cipher.doFinal(cipherbytes);
return new String(input);
}
/**
* 数字签名:私钥加密
*
* @param signature 签名明文字符串
* @param privateKey 私钥字符串
* @return 签名字符串
* @throws Exception
*/
public String skEncoded(String signature,String privateKey) throws Exception {
Signature signature1 = Signature.getInstance(signAlgorithm);
signature1.initSign(privateKeyStringToKey(privateKey));
signature1.update(signature.getBytes(StandardCharsets.UTF_8));
byte[] sign = signature1.sign();
return Base64.getEncoder().encodeToString(sign);
}
/**
* 判断签名:公钥解密
* @param input
* @param signDate 签名密文字符串
* @param publicKey 公钥
* @return boolen
* @throws Exception
*/
public boolean pkDecoded(String input,String signDate,String publicKey)throws Exception {
Signature signature = Signature.getInstance(signAlgorithm);
signature.initVerify(publicKeyStringToKey(publicKey));
signature.update(input.getBytes(StandardCharsets.UTF_8));
return signature.verify(Base64.getDecoder().decode(signDate));
}
}以上就是小编今天的分享了,希望可以帮助到大家。