From 5b48828c7719c8aba7a494be3d4bd91ddb4250c8 Mon Sep 17 00:00:00 2001 From: Carol <947752894@qq.com> Date: Wed, 1 Sep 2021 13:30:04 +0800 Subject: [PATCH] change --- .idea/jarRepositories.xml | 15 ++ .../442292b8a7efeabbe4cc176709b833b1792140ec | 0 .../82864735cd8af377e6ad061ae189505a1e3e9d7c | 0 .../eaa75651d9d6785c2bd75843529d61a722450af5 | 0 .idea/sonarlint/issuestore/index.pb | 6 - .idea/workspace.xml | 28 +-- CommonUtil.iml | 8 + .../site/cnkj/common/utils/io/RSAEncrypt.java | 174 ++++++++---------- 8 files changed, 107 insertions(+), 124 deletions(-) delete mode 100644 .idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec delete mode 100644 .idea/sonarlint/issuestore/8/2/82864735cd8af377e6ad061ae189505a1e3e9d7c delete mode 100644 .idea/sonarlint/issuestore/e/a/eaa75651d9d6785c2bd75843529d61a722450af5 create mode 100644 CommonUtil.iml diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index 2dee72d..9687c92 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -1,6 +1,11 @@ + + + + + + - - - - - - - - - - - + + + + + - - + - - - @@ -388,7 +374,7 @@ - + diff --git a/CommonUtil.iml b/CommonUtil.iml new file mode 100644 index 0000000..4a4ce55 --- /dev/null +++ b/CommonUtil.iml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java b/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java index 65ae08d..b4ecb50 100644 --- a/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java +++ b/src/main/java/site/cnkj/common/utils/io/RSAEncrypt.java @@ -1,7 +1,7 @@ package site.cnkj.common.utils.io; import org.bouncycastle.jce.provider.BouncyCastleProvider; -import sun.misc.BASE64Decoder; +import org.apache.commons.codec.binary.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -18,15 +18,19 @@ import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; +/** + * RSA非对称数据加解密 + * @version 1.0 created by LXW on 2019/4/15 15:31 + */ public class RSAEncrypt { - private static final String DEFAULT_PUBLIC_KEY= + public static final String DEFAULT_PUBLIC_KEY= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChDzcjw/rWgFwnxunbKp7/4e8w" + "\r" + "/UmXx2jk6qEEn69t6N2R1i/LmcyDT1xr/T2AHGOiXNQ5V8W4iCaaeNawi7aJaRht" + "\r" + "Vx1uOH/2U378fscEESEG8XDqll0GCfB1/TjKI2aitVSzXOtRs8kYgGU78f7VmDNg" + "\r" + "XIlk3gdhnzh+uoEQywIDAQAB" + "\r"; - private static final String DEFAULT_PRIVATE_KEY= + public static final String DEFAULT_PRIVATE_KEY= "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKEPNyPD+taAXCfG" + "\r" + "6dsqnv/h7zD9SZfHaOTqoQSfr23o3ZHWL8uZzINPXGv9PYAcY6Jc1DlXxbiIJpp4" + "\r" + "1rCLtolpGG1XHW44f/ZTfvx+xwQRIQbxcOqWXQYJ8HX9OMojZqK1VLNc61GzyRiA" + "\r" + @@ -42,6 +46,8 @@ public class RSAEncrypt { "Fae7oAqkMAd3xCNY6ec180tAe57hZ6kS+SYLKwb4gGzYaCxc22vMtYksXHtUeamo" + "\r" + "1NMLzI2ZfUoX" + "\r"; + + /** * 私钥 */ @@ -85,170 +91,144 @@ public class RSAEncrypt { e.printStackTrace(); } if (keyPairGen != null){ - keyPairGen.initialize(1024, new SecureRandom()); + keyPairGen.initialize(2048, new SecureRandom()); KeyPair keyPair= keyPairGen.generateKeyPair(); this.privateKey= (RSAPrivateKey) keyPair.getPrivate(); this.publicKey= (RSAPublicKey) keyPair.getPublic(); } } + private StringBuilder loadKey(InputStream in) throws IOException { + BufferedReader br= new BufferedReader(new InputStreamReader(in)); + String readLine= null; + StringBuilder sb= new StringBuilder(); + while((readLine= br.readLine())!=null){ + if(readLine.charAt(0)!='-'){ + sb.append(readLine); + sb.append('\r'); + } + } + return sb; + } + + /** * 从文件中输入流中加载公钥 * @param in 公钥输入流 - * @throws Exception 加载公钥时产生的异常 + * @throws IOException e + * @throws NoSuchAlgorithmException e + * @throws InvalidKeySpecException e */ - public void loadPublicKey(InputStream in) throws Exception{ - try { - BufferedReader br= new BufferedReader(new InputStreamReader(in)); - String readLine= null; - StringBuilder sb= new StringBuilder(); - while((readLine= br.readLine())!=null){ - if(readLine.charAt(0)=='-'){ - continue; - }else{ - sb.append(readLine); - sb.append('\r'); - } - } - loadPublicKey(sb.toString()); - } catch (IOException e) { - throw new Exception("公钥数据流读取错误"); - } catch (NullPointerException e) { - throw new Exception("公钥输入流为空"); - } + public void loadPublicKey(InputStream in) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { + loadPublicKey(loadKey(in).toString()); } /** * 从字符串中加载公钥 * @param publicKeyStr 公钥数据字符串 - * @throws Exception 加载公钥时产生的异常 + * @throws NoSuchAlgorithmException e + * @throws InvalidKeySpecException e */ - public void loadPublicKey(String publicKeyStr) throws Exception{ - try { - BASE64Decoder base64Decoder= new BASE64Decoder(); - byte[] buffer= base64Decoder.decodeBuffer(publicKeyStr); - KeyFactory keyFactory= KeyFactory.getInstance("RSA"); - X509EncodedKeySpec keySpec= new X509EncodedKeySpec(buffer); - this.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec); - } catch (NoSuchAlgorithmException e) { - throw new Exception("无此算法"); - } catch (InvalidKeySpecException e) { - throw new Exception("公钥非法"); - } catch (IOException e) { - throw new Exception("公钥数据内容读取错误"); - } catch (NullPointerException e) { - throw new Exception("公钥数据为空"); - } + public void loadPublicKey(String publicKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException { + byte[] buffer= Base64.decodeBase64(publicKeyStr); + KeyFactory keyFactory= KeyFactory.getInstance("RSA"); + X509EncodedKeySpec keySpec= new X509EncodedKeySpec(buffer); + this.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec); } + /** * 从文件中加载私钥 * @param in 输入流 - * @return 是否成功 - * @throws Exception + * @throws NoSuchAlgorithmException e + * @throws InvalidKeySpecException e + * @throws IOException e */ - public void loadPrivateKey(InputStream in) throws Exception{ - try { - BufferedReader br= new BufferedReader(new InputStreamReader(in)); - String readLine= null; - StringBuilder sb= new StringBuilder(); - while((readLine= br.readLine())!=null){ - if(readLine.charAt(0)=='-'){ - continue; - }else{ - sb.append(readLine); - sb.append('\r'); - } - } - loadPrivateKey(sb.toString()); - } catch (IOException e) { - throw new Exception("私钥数据读取错误"); - } catch (NullPointerException e) { - throw new Exception("私钥输入流为空"); - } + public void loadPrivateKey(InputStream in) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException{ + loadPrivateKey(loadKey(in).toString()); } - public void loadPrivateKey(String privateKeyStr) throws Exception{ + public void loadPrivateKey(String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException { try { - BASE64Decoder base64Decoder= new BASE64Decoder(); - byte[] buffer= base64Decoder.decodeBuffer(privateKeyStr); + byte[] buffer= Base64.decodeBase64(privateKeyStr); PKCS8EncodedKeySpec keySpec= new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory= KeyFactory.getInstance("RSA"); this.privateKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { - throw new Exception("无此算法"); + throw new NoSuchAlgorithmException("无此算法"); } catch (InvalidKeySpecException e) { - throw new Exception("私钥非法"); - } catch (IOException e) { - throw new Exception("私钥数据内容读取错误"); + throw new InvalidKeySpecException("私钥非法"); } catch (NullPointerException e) { - throw new Exception("私钥数据为空"); + throw new NullPointerException("私钥数据为空"); } } + /** * 加密过程 * @param publicKey 公钥 * @param plainTextData 明文数据 - * @return - * @throws Exception 加密过程中的异常信息 + * @return 加密后字节 + * @throws NoSuchAlgorithmException e + * @throws InvalidKeyException e + * @throws IllegalBlockSizeException e + * @throws BadPaddingException e + * @throws NoSuchPaddingException e */ - public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception{ + public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { if(publicKey== null){ - throw new Exception("加密公钥为空, 请设置"); + throw new NullPointerException("加密公钥为空, 请设置"); } Cipher cipher= null; try { cipher= Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); - byte[] output= cipher.doFinal(plainTextData); - return output; + return cipher.doFinal(plainTextData); } catch (NoSuchAlgorithmException e) { - throw new Exception("无此加密算法"); - } catch (NoSuchPaddingException e) { - e.printStackTrace(); - return null; + throw new NoSuchAlgorithmException("无此加密算法"); } catch (InvalidKeyException e) { - throw new Exception("加密公钥非法,请检查"); + throw new InvalidKeyException("加密公钥非法,请检查"); } catch (IllegalBlockSizeException e) { - throw new Exception("明文长度非法"); + throw new IllegalBlockSizeException("明文长度非法"); } catch (BadPaddingException e) { - throw new Exception("明文数据已损坏"); - } catch (Exception e){ - e.printStackTrace(); - return null; + throw new BadPaddingException("明文数据已损坏"); + } catch (NoSuchPaddingException e) { + throw new NoSuchPaddingException("无此类填充"); } } + /** * 解密过程 * @param privateKey 私钥 * @param cipherData 密文数据 * @return 明文 - * @throws Exception 解密过程中的异常信息 + * @throws NoSuchAlgorithmException e + * @throws NoSuchPaddingException e + * @throws InvalidKeyException e + * @throws IllegalBlockSizeException e + * @throws BadPaddingException e */ - public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception{ + public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { if (privateKey== null){ - throw new Exception("解密私钥为空, 请设置"); + throw new NullPointerException("解密私钥为空, 请设置"); } Cipher cipher= null; try { cipher= Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", new BouncyCastleProvider()); cipher.init(Cipher.DECRYPT_MODE, privateKey); - byte[] output= cipher.doFinal(cipherData); - return output; + return cipher.doFinal(cipherData); } catch (NoSuchAlgorithmException e) { - throw new Exception("无此解密算法"); + throw new NoSuchAlgorithmException("无此解密算法"); } catch (NoSuchPaddingException e) { - e.printStackTrace(); - return null; + throw new NoSuchPaddingException("无此类填充"); }catch (InvalidKeyException e) { - throw new Exception("解密私钥非法,请检查"); + throw new InvalidKeyException("解密私钥非法,请检查"); } catch (IllegalBlockSizeException e) { - throw new Exception("密文长度非法"); + throw new IllegalBlockSizeException("密文长度非法"); } catch (BadPaddingException e) { - throw new Exception("密文数据已损坏"); + throw new BadPaddingException("密文数据已损坏"); } }