欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

java与IOS之间的RSA加解密

发表日期:2019-08 文章编辑:小灯 浏览次数:848


Java中使用OpenSSL生成的RSA公私钥进行数据加解密



Java代码  收藏代码

  1.  java.io.BufferedReader;  

  2.  java.io.IOException;  

  3.  java.io.InputStream;  

  4.  java.io.InputStreamReader;  

  5.  java.security.InvalidKeyException;  

  6.  java.security.KeyFactory;  

  7.  java.security.KeyPair;  

  8.  java.security.KeyPairGenerator;  

  9.  java.security.NoSuchAlgorithmException;  

  10.  java.security.SecureRandom;  

  11.  java.security.interfaces.RSAPrivateKey;  

  12.  java.security.interfaces.RSAPublicKey;  

  13.  java.security.spec.InvalidKeySpecException;  

  14.  java.security.spec.PKCS8EncodedKeySpec;  

  15.  java.security.spec.X509EncodedKeySpec;  


  16.  javax.crypto.BadPaddingException;  

  17.  javax.crypto.Cipher;  

  18.  javax.crypto.IllegalBlockSizeException;  

  19.  javax.crypto.NoSuchPaddingException;  


  20.  sun.misc.BASE64Decoder;  

  21.  sun.misc.BASE64Encoder;  


  22.   RSAEncrypt {  

  23.    String DEFAULT_PUBLIC_KEY=     

  24.  +  +    

  25.  +  +    

  26.  +  +    

  27.  + ;    


  28.    String DEFAULT_PRIVATE_KEY=    

  29.  +  +    

  30.  +  +    

  31.  +  +    

  32.  +  +    

  33.  +  +    

  34.  +  +    

  35.  +  +    

  36.  +  +    

  37.  +  +    

  38.  +  +    

  39.  +  +    

  40.  +  +    

  41.  +  +    

  42.  + ;    





  43.  RSAPrivateKey privateKey;    





  44.  RSAPublicKey publicKey;    





  45.    [] HEX_CHAR= {, , , , , , , , , , , , , , , };    







  46.  RSAPrivateKey getPrivateKey() {    

  47.  privateKey;    







  48.  RSAPublicKey getPublicKey() {    

  49.  publicKey;    






  50.   genKeyPair(){    

  51. ;    

  52.  {    

  53. );    

  54.  (NoSuchAlgorithmException e) {    



  55. ,  SecureRandom());    


  56. .privateKey= (RSAPrivateKey) keyPair.getPrivate();    

  57. .publicKey= (RSAPublicKey) keyPair.getPublic();    








  58.   loadPublicKey(InputStream in)  Exception{    

  59.  {    

  60.  BufferedReader( InputStreamReader(in));    

  61. ;    

  62.  StringBuilder();    

  63. ((readLine= br.readLine())!=){    

  64. (readLine.charAt()==){    

  65. ;    

  66. {    


  67. );    




  68.  (IOException e) {    

  69.   Exception();    

  70.  (NullPointerException e) {    

  71.   Exception();    










  72.   loadPublicKey(String publicKeyStr)  Exception{    

  73.  {    

  74.  BASE64Decoder();    

  75. [] buffer= base64Decoder.decodeBuffer(publicKeyStr);  

  76. );    

  77.  X509EncodedKeySpec(buffer);    

  78. .publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec);    

  79.  (NoSuchAlgorithmException e) {    

  80.   Exception();    

  81.  (InvalidKeySpecException e) {    

  82.   Exception();    

  83.  (IOException e) {    

  84.   Exception();    

  85.  (NullPointerException e) {    

  86.   Exception();    










  87.   loadPrivateKey(InputStream in)  Exception{    

  88.  {    

  89.  BufferedReader( InputStreamReader(in));    

  90. ;    

  91.  StringBuilder();    

  92. ((readLine= br.readLine())!=){    

  93. (readLine.charAt()==){    

  94. ;    

  95. {    


  96. );    




  97.  (IOException e) {    

  98.   Exception();    

  99.  (NullPointerException e) {    

  100.   Exception();    




  101.   loadPrivateKey(String privateKeyStr)  Exception{    

  102.  {    

  103.  BASE64Decoder();    

  104. [] buffer= base64Decoder.decodeBuffer(privateKeyStr);    

  105.  PKCS8EncodedKeySpec(buffer);    

  106. );    

  107. .privateKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec);    

  108.  (NoSuchAlgorithmException e) {    

  109.   Exception();    

  110.  (InvalidKeySpecException e) {    


  111.   Exception();    

  112.  (IOException e) {    

  113.   Exception();    

  114.  (NullPointerException e) {    

  115.   Exception();    











  116.  [] encrypt(RSAPublicKey publicKey, [] plainTextData)  Exception{    

  117. (publicKey== ){    

  118.   Exception();    


  119. ;    

  120.  {    

  121. );  


  122. [] output= cipher.doFinal(plainTextData);    

  123.  output;    

  124.  (NoSuchAlgorithmException e) {    

  125.   Exception();    

  126.  (NoSuchPaddingException e) {    


  127.  ;    

  128.  (InvalidKeyException e) {    

  129.   Exception();    

  130.  (IllegalBlockSizeException e) {    

  131.   Exception();    

  132.  (BadPaddingException e) {    

  133.   Exception();    











  134.  [] decrypt(RSAPrivateKey privateKey, [] cipherData)  Exception{    

  135.  (privateKey== ){    

  136.   Exception();    


  137. ;    

  138.  {    

  139. );  


  140. [] output= cipher.doFinal(cipherData);    

  141.  output;    

  142.  (NoSuchAlgorithmException e) {    

  143.   Exception();    

  144.  (NoSuchPaddingException e) {    


  145.  ;    

  146.  (InvalidKeyException e) {    

  147.   Exception();    

  148.  (IllegalBlockSizeException e) {    

  149.   Exception();    

  150.  (BadPaddingException e) {    

  151.   Exception();    










  152.   String byteArrayToString([] data){    

  153.  StringBuilder();    

  154.  ( i=; i<data.length; i++){    


  155. )>>> ]);    


  156. )]);    

  157.  (i<data.length-){    

  158. );    



  159.  stringBuilder.toString();    




  160.    main(String[] args){    

  161.  RSAEncrypt();    





  162.  {    


  163. );    

  164.  (Exception e) {    


  165. );    




  166.  {    


  167. );    

  168.  (Exception e) {    


  169. );    




  170. ;    

  171. +rsaEncrypt.getPrivateKey().toString().length());  

  172. +rsaEncrypt.getPublicKey().toString().length());  

  173.  {    


  174. [] cipher = rsaEncrypt.encrypt(rsaEncrypt.getPublicKey(), encryptStr.getBytes());    




  175. [] plainText = rsaEncrypt.decrypt(rsaEncrypt.getPrivateKey(), cipher);    




  176. + cipher.length);    


  177. + plainText.length);    


  178.  String(plainText));    

  179.  (Exception e) {    









本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://www.dengtar.com/20746.html
相关企业建站知识
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.