import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class EncodeUtils {
public static final String TYPE = "RSA";
public static final Integer SIZE = 1024;
public static final String PUBLIC_KEY = "publicKey";
public static final String PRIVATE_KEY = "privateKey";
/**
* RSA encrypt
*
* @param str
* @param publicKey
* @return String
*/
public static String encrypt(String str, String publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
byte[] decoded = Base64.getDecoder().decode(publicKey);
RSAPublicKey pubKey =
(RSAPublicKey)KeyFactory.getInstance(TYPE).generatePublic(new X509EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(TYPE);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
}
/**
* RSA decrypt
*
* @param str
* @param privateKey
* @return String
*/
public static String decrypt(String str, String privateKey)
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
byte[] inputByte = Base64.getDecoder().decode(str.getBytes(StandardCharsets.UTF_8));
byte[] decoded = Base64.getDecoder().decode(privateKey);
RSAPrivateKey priKey =
(RSAPrivateKey)KeyFactory.getInstance(TYPE).generatePrivate(new PKCS8EncodedKeySpec(decoded));
Cipher cipher = Cipher.getInstance(TYPE);
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8);
}
/**
* generate key pair
*
* @param
* @return Map
**/
public static Map<String, String> generateKeyPair() {
Map<String, String> keyMap = new HashMap<>(16);
KeyPairGenerator keyPairGen;
try {
keyPairGen = KeyPairGenerator.getInstance(TYPE);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
keyPairGen.initialize(SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
String privateKeyString = Base64.getEncoder().encodeToString((privateKey.getEncoded()));
keyMap.put(PUBLIC_KEY, publicKeyString);
keyMap.put(PRIVATE_KEY, privateKeyString);
return keyMap;
}
public static void main(String[] args) throws Exception{
String str = "a1234567";
Map<String, String> map = generateKeyPair();
String publickey = map.get(PUBLIC_KEY);
String privateKey = map.get(PRIVATE_KEY);
String encrypt = encrypt(str, publickey);
System.out.println("加密后的数据:"+encrypt);
String decrypt = decrypt(encrypt, privateKey);
System.out.println("解密后的数据:"+decrypt);
}
}
RSA加解密
于 2021-04-02 17:15:41 首次发布