<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      RSA非對稱加密

      java RSA 分段分組加密


       

      文中公鑰和私鑰都從resource下去加載的,你可以直接使用字符串也是可以的

      一般情況使用公鑰加密,私鑰解密

      import lombok.AllArgsConstructor;
      import lombok.Data;
      import lombok.NoArgsConstructor;
      import org.springframework.core.io.ClassPathResource;
      
      import javax.crypto.Cipher;
      import javax.crypto.NoSuchPaddingException;
      import java.io.*;
      import java.security.*;
      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;
      
      /**
       * @author Mr.Fang
       * @version 1.0
       * @description: RAS 非對稱加密解密 2048
       * @date 2022/5/12 11:06
       */
      public class RSAUtil {
      
          /**
           * 1024 加密最大塊 117
           * 最大文件加密塊 2048 不能超過 245
           */
          private static final int MAX_ENCRYPT_BLOCK = 245;
      
          /**
           * 1024 解密最大塊 128
           * 最大文件解密塊 2048 不能超過 256
           */
          private static final int MAX_DECRYPT_BLOCK = 256;
      
          /**
           * 字符集
           */
          private static final String CHAR_SET_NAME = "UTF-8";
      
          /**
           * 加密類型
           */
          private static final String ENCRYPT_TYPE = "RSA";
      
      
          /**
           * description:  創建秘鑰對 公鑰和私鑰
           * create by: Mr.Fang
           *
           * @param keySize: 秘鑰字節數
           * @date: 2022/5/17 10:33
           */
          public RSAUtil.RSAKey createKey(int keySize) {
              try {
                  KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ENCRYPT_TYPE);
                  keyPairGenerator.initialize(keySize);
                  KeyPair keyPair = keyPairGenerator.generateKeyPair();
                  PublicKey publicKey = keyPair.getPublic();
                  String publicKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
                  PrivateKey privateKey = keyPair.getPrivate();
                  String privateKeyStr = Base64.getEncoder().encodeToString(privateKey.getEncoded());
                  return new RSAUtil.RSAKey(privateKeyStr, publicKeyStr);
              } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
                  return null;
              }
          }
      
          @Data
          @NoArgsConstructor
          @AllArgsConstructor
          public class RSAKey {
              private String privateKey;
              private String publicKey;
          }
      
          /**
           * description:  從 resource 加載公鑰
           * create by: Mr.Fang
           *
           * @return: java.security.interfaces.RSAPublicKey
           * @date: 2022/5/17 9:27
           */
          private static RSAPublicKey getPublicKey() {
              try {
                  ClassPathResource resource = new ClassPathResource("publicKey");
                  InputStream inputStream = null;
                  try {
                      inputStream = resource.getInputStream();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  //將流轉為字符串
                  String string = readToString(inputStream);
                  KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPT_TYPE);
                  X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.getMimeDecoder().decode(string));
                  RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
                  return publicKey;
              } catch (NoSuchAlgorithmException var4) {
                  var4.printStackTrace();
              } catch (InvalidKeySpecException var5) {
                  var5.printStackTrace();
              }
              return null;
          }
      
      
          /**
           * description: 從 resource 加載私鑰
           * create by: Mr.Fang
           *
           * @return: java.security.interfaces.RSAPrivateKey
           * @date: 2022/5/17 9:12
           */
          private static RSAPrivateKey getPrivateKey() {
              ClassPathResource resource = new ClassPathResource("privateKey");
              try (InputStream inputStream = resource.getInputStream()) {
                  String privateKeyStr = readToString(inputStream);
                  KeyFactory keyFactory = KeyFactory.getInstance(ENCRYPT_TYPE);
                  PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(privateKeyStr));
                  RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
                  return privateKey;
              } catch (NoSuchAlgorithmException var4) {
                  var4.printStackTrace();
              } catch (InvalidKeySpecException var5) {
                  var5.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }
      
          /**
           * description: 輸入流轉字符串
           * create by: Mr.Fang
           *
           * @param inputStream: 輸入流
           * @return: java.lang.String
           * @date: 2022/5/18 8:53
           */
          public static String readToString(InputStream inputStream) {
              try {
                  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, CHAR_SET_NAME));
                  StringBuffer sb = new StringBuffer();
                  for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                      sb.append(s);
                  }
                  String toString = sb.toString();
                  return toString;
              } catch (UnsupportedEncodingException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return null;
          }
      
      
          /**
           * description:   分段公鑰加密
           * create by: Mr.Fang
           *
           * @param inputStr: 待加密數據
           * @return: java.lang.String
           * @date: 2022/5/17 9:26
           */
          public static String publicEncrypt(String inputStr) {
              try {
                  // 獲取 cipher 加密對象
                  Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE);
                  // 獲取公鑰
                  RSAPublicKey rsaPublicKey = getPublicKey();
                  // 初始化加密對象 第一個參數加密模式,第二個參數公鑰
                  cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
                  // 加密字符串轉字節數組
                  byte[] bytes = inputStr.getBytes(CHAR_SET_NAME);
                  // 字節長度
                  int length = bytes.length;
                  // 偏移
                  int offset = 0;
                  // 當前下標
                  int i = 0;
                  // 創建字節輸出流
                  ByteArrayOutputStream byOutput = new ByteArrayOutputStream();
                  while (length - offset > 0) {
                      // 存放每次加密后的字節
                      byte[] cache; //
                      if (length - offset > MAX_ENCRYPT_BLOCK) {
                          cache = cipher.doFinal(bytes, offset, MAX_ENCRYPT_BLOCK);
                      } else {
                          cache = cipher.doFinal(bytes, offset, length - offset);
                      }
                      // 寫入字節流
                      byOutput.write(cache);
                      i++; // 自增
                      offset = MAX_ENCRYPT_BLOCK * i; // 偏移
                  }
                  byOutput.flush(); // 刷新
                  byOutput.close(); // 關閉字節流
                  // 轉字節數組
                  byte[] byteArray = byOutput.toByteArray();
                  // base64 編碼
                  return Base64.getEncoder().encodeToString(byteArray);
              } catch (NoSuchPaddingException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return null;
          }
      
          /**
           * description: 分段私鑰解密
           * create by: Mr.Fang
           *
           * @param inputStr: 密文
           * @return: java.lang.String
           * @date: 2022/5/17 9:20
           */
          public static String privateDecrypt(String inputStr) throws Exception {
              // 獲取 cipher 加密對象
              Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE);
              //獲取私鑰
              PrivateKey privateKey = getPrivateKey();
              // 初始化 cipher 對象,第一個參數解密模式,第二個參數解密秘鑰
              cipher.init(Cipher.DECRYPT_MODE, privateKey);
              // base64 解碼
              byte[] bytes = Base64.getDecoder().decode(inputStr);
              // 字節長度
              int length = bytes.length;
              // 偏移
              int offset = 0;
              // 當前下標
              int i = 0;
              // 創建字節輸出流
              ByteArrayOutputStream byOutput = new ByteArrayOutputStream();
              while (length - offset > 0) {
                  // 存放每次機密后的字節
                  byte[] cache; //
                  if (length - offset > MAX_DECRYPT_BLOCK) {
                      cache = cipher.doFinal(bytes, offset, MAX_DECRYPT_BLOCK);
                  } else {
                      cache = cipher.doFinal(bytes, offset, length - offset);
                  }
                  // 寫入字節流
                  byOutput.write(cache);
                  i++; // 自增
                  offset = MAX_DECRYPT_BLOCK * i; // 偏移
              }
              byOutput.flush(); // 刷新
              byOutput.close(); // 關閉字節流
              // 轉字節數組
              byte[] byteArray = byOutput.toByteArray();
              return new String(byteArray, CHAR_SET_NAME);
          }
      }

       測試結果

       

       

       

      posted @ 2022-05-18 09:11  天葬  閱讀(22)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲老熟女一区二区三区| 国产福利微视频一区二区| 少妇人妻偷人精品无码视频| 亚洲av无码乱码在线观看野外| 99久久国产综合精品成人影院| 黑河市| 欧美熟妇xxxxx欧美老妇不卡 | 久久精品国产久精国产果冻传媒 | 狠狠色噜噜狠狠狠狠777米奇| 无码人妻丝袜在线视频| 亚洲国产精品第一区二区| av无码精品一区二区三区四区 | 国产精品无码无卡在线播放| 国产av一区二区三区精品| 国产成人无码免费视频在线| 亚洲国产精品综合久久20| 男女动态无遮挡动态图| 久久久久久综合网天天| 日韩欧美亚洲综合久久| 少妇又爽又刺激视频| 91久久天天躁狠狠躁夜夜| 日韩一区在线中文字幕| 国产精品免费无遮挡无码永久视频 | 久久香蕉欧美精品| 国产人妻精品午夜福利免费| 国产精品国产精品偷麻豆| 久久人人97超碰人人澡爱香蕉| 十八禁国产精品一区二区| av在线网站手机播放| 一级女性全黄久久片免费| 91青青草视频在线观看| 亚洲无人区码二码三码区| 周宁县| 起碰免费公开97在线视频| 少妇av一区二区三区无码| 亚洲av日韩在线资源| 日韩毛片在线视频x| 少妇粗大进出白浆嘿嘿视频| 天天躁夜夜躁天干天干2020| 精品国产亚洲一区二区三区| 久久国产综合色免费观看|