[Java] java RSA加密类 →→→→→进入此内容的聊天室

来自 , 2019-07-16, 写在 Java, 查看 102 次.
URL http://www.code666.cn/view/b534ba68
  1. import java.security.KeyPair;
  2. import java.security.KeyPairGenerator;
  3. import java.security.interfaces.RSAPrivateKey;
  4. import java.security.interfaces.RSAPublicKey;
  5. import javax.crypto.Cipher;
  6.  
  7. /**
  8.  * RSA加密类
  9.  *
  10.  */
  11. public class RSAEncrypt {
  12.  
  13.         public static void main(String[] args) {
  14.                 try {
  15.                         RSAEncrypt encrypt = new RSAEncrypt();
  16.                         String encryptText = "12345678";
  17.                         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  18.                         keyPairGen.initialize(1024);
  19.                         KeyPair keyPair = keyPairGen.generateKeyPair();
  20.                         // Generate keys
  21.                         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 私钥
  22.                         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥
  23.                         byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
  24.                         byte[] de = encrypt.decrypt(privateKey, e);
  25.                         System.out.println(encrypt.bytesToString(e));
  26.                         System.out.println();
  27.  
  28.                         System.out.println(encrypt.bytesToString(de));
  29.                 } catch (Exception e) {
  30.                         e.printStackTrace();
  31.                 }
  32.         }
  33.  
  34.         /**
  35.          * byte数组转为string
  36.          *
  37.          * @param encrytpByte
  38.          * @return
  39.          */
  40.         protected String bytesToString(byte[] encrytpByte) {
  41.                 String result = "";
  42.                 for (Byte bytes : encrytpByte) {
  43.                         result += (char) bytes.intValue();
  44.                 }
  45.                 return result;
  46.         }
  47.  
  48.         /**
  49.          * 加密方法
  50.          *
  51.          * @param publicKey
  52.          * @param obj
  53.          * @return
  54.          */
  55.         protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
  56.                 if (publicKey != null) {
  57.                         try {
  58.                                 Cipher cipher = Cipher.getInstance("RSA");
  59.                                 cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  60.                                 return cipher.doFinal(obj);
  61.                         } catch (Exception e) {
  62.                                 e.printStackTrace();
  63.                         }
  64.                 }
  65.                 return null;
  66.         }
  67.  
  68.         /**
  69.          * 解密方法
  70.          *
  71.          * @param privateKey
  72.          * @param obj
  73.          * @return
  74.          */
  75.         protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
  76.                 if (privateKey != null) {
  77.                         try {
  78.                                 Cipher cipher = Cipher.getInstance("RSA");
  79.                                 cipher.init(Cipher.DECRYPT_MODE, privateKey);
  80.                                 return cipher.doFinal(obj);
  81.                         } catch (Exception e) {
  82.                                 e.printStackTrace();
  83.                         }
  84.                 }
  85.                 return null;
  86.         }
  87. }

回复 "java RSA加密类"

这儿你可以回复上面这条便签

captcha