java中RSA加解密的實現
阿新 • • 發佈:2019-02-16
原文:https://blog.csdn.net/draven1122/article/details/55212195
關於加密資料長度和解密資料長度大家可以看一下我前一篇文章內的介紹:
- publicstaticvoid main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- HashMap<String, Object> map = RSAUtils.getKeys();
- //生成公鑰和私鑰
- RSAPublicKey publicKey = (RSAPublicKey) map.get("public"
- RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
- //模
- String modulus = publicKey.getModulus().toString();
- //公鑰指數
- String public_exponent = publicKey.getPublicExponent().toString();
- //私鑰指數
- String private_exponent = privateKey.getPrivateExponent().toString();
- //明文
- String ming = "123456789";
- //使用模和指數生成公鑰和私鑰
- RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);
- RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);
- //加密後的密文
- String mi = RSAUtils.encryptByPublicKey(ming, pubKey);
- System.err.println(mi);
- //解密後的明文
- ming = RSAUtils.decryptByPrivateKey(mi, priKey);
- System.err.println(ming);
- }
RSAUtils.java
- package yyy.test.rsa;
- import java.math.BigInteger;
- import java.security.KeyFactory;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.NoSuchAlgorithmException;
- import java.security.interfaces.RSAPrivateKey;
- import java.security.interfaces.RSAPublicKey;
- import java.security.spec.RSAPrivateKeySpec;
- import java.security.spec.RSAPublicKeySpec;
- import java.util.HashMap;
- import javax.crypto.Cipher;
- publicclass RSAUtils {
- /**
- * 生成公鑰和私鑰
- * @throws NoSuchAlgorithmException
- *
- */
- publicstatic HashMap<String, Object> getKeys() throws NoSuchAlgorithmException{
- HashMap<String, Object> map = new HashMap<String, Object>();
- KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
- keyPairGen.initialize(1024);
- KeyPair keyPair = keyPairGen.generateKeyPair();
- RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
- RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
- map.put("public", publicKey);
- map.put("private", privateKey);
- return map;
- }
- /**
- * 使用模和指數生成RSA公鑰
- * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如Android預設是RSA
- * /None/NoPadding】
- *
- * @param modulus
- * 模
- * @param exponent
- * 指數
- * @return
- */
- publicstatic RSAPublicKey getPublicKey(String modulus, String exponent) {
- try {
- BigInteger b1 = new BigInteger(modulus);
- BigInteger b2 = new BigInteger(exponent);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
- return (RSAPublicKey) keyFactory.generatePublic(keySpec);
- } catch (Exception e) {
- e.printStackTrace();
- returnnull;
- }
- }
- /**
- * 使用模和指數生成RSA私鑰
- * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如Android預設是RSA
- * /None/NoPadding】
- *
- * @param modulus
- * 模
- * @param exponent
- * 指數
- * @return
- */
- publicstatic RSAPrivateKey getPrivateKey(String modulus, String exponent) {
- try {
- BigInteger b1 = new BigInteger(modulus);
- BigInteger b2 = new BigInteger(exponent);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
- return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
- } catch (Exception e) {
- e.printStackTrace();
- returnnull;
- }
- }
- /**
- * 公鑰加密
- *
- * @param data
- * @param publicKey
- * @return
- * @throws Exception
- */
- publicstatic String encryptByPublicKey(String data, RSAPublicKey publicKey)
- throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- // 模長
- int key_len = publicKey.getModulus().bitLength() / 8;
- // 加密資料長度 <= 模長-11
- String[] datas = splitString(data, key_len - 11);
- String mi = "";
- //如果明文長度大於模