首页 > 学院 > 开发设计 > 正文

C# RSA和Java RSA互通

2019-11-17 03:06:39
字体:
来源:转载
供稿:网友
C# RSA和java RSA互通

今天调查了C# RSA和Java RSA,网上很多人说,C#加密或者java加密 ,Java不能解密或者C#不能解密

但是我尝试了一下,发现是可以的,下面就是我尝试的代码,如果您有什么问题,我想看看,他们为什么不能互通?

Rsamain代码收藏代码
  1. packagersa;
  2. importjava.math.BigInteger;
  3. importjava.security.KeyFactory;
  4. importjava.security.PRivateKey;
  5. importjava.security.PublicKey;
  6. importjava.security.spec.RSAPrivateKeySpec;
  7. importjava.security.spec.RSAPublicKeySpec;
  8. importjavax.crypto.Cipher;
  9. importcom.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
  10. importcom.sun.org.apache.xml.internal.security.utils.Base64;
  11. /**
  12. *@authorcnchenhl
  13. *Jul8,2011
  14. */
  15. publicclassRSAMain{
  16. privatestaticStringmodule="5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubQQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=";
  17. privatestaticStringexponentString="AQAB";
  18. privatestaticStringdelement="vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=";
  19. privatestaticStringencryptString="Vx/dGjS1YWKRubsoDgiShiwLgqyNE2z/eM65U7HZx+RogwaiZimNBxjuOS6acEhKZx66cMYEAd1fc6oewbEvDIfP44GaN9dCjKE/BkkQlwEg6aTO5q+yqy+nEGe1kvLY9EyXS/Kv1LDh3e/2xAk5FNj8Zp6oU2kq4ewL8kK/ai4=";
  20. /**
  21. *@paramargs
  22. */
  23. publicstaticvoidmain(String[]args){
  24. byte[]en=encrypt();
  25. System.out.println(Base64.encode(en));
  26. byte[]enTest=null;
  27. try{
  28. enTest=Base64.decode(encryptString);
  29. }catch(Base64DecodingExceptione){
  30. e.printStackTrace();
  31. }
  32. System.out.println(enTest.length);
  33. System.out.println(en.length);
  34. System.out.println(newString(Dencrypt(en)));
  35. System.out.println(newString(Dencrypt(enTest)));
  36. }
  37. publicstaticbyte[]encrypt(){
  38. try{
  39. byte[]modulusBytes=Base64.decode(module);
  40. byte[]exponentBytes=Base64.decode(exponentString);
  41. BigIntegermodulus=newBigInteger(1,modulusBytes);
  42. BigIntegerexponent=newBigInteger(1,exponentBytes);
  43. RSAPublicKeySpecrsaPubKey=newRSAPublicKeySpec(modulus,exponent);
  44. KeyFactoryfact=KeyFactory.getInstance("RSA");
  45. PublicKeypubKey=fact.generatePublic(rsaPubKey);
  46. Ciphercipher=Cipher.getInstance("RSA");
  47. cipher.init(Cipher.ENCRYPT_MODE,pubKey);
  48. byte[]cipherData=cipher.doFinal(newString("chenhailong").getBytes());
  49. returncipherData;
  50. }catch(Exceptione){
  51. e.printStackTrace();
  52. }
  53. returnnull;
  54. }
  55. publicstaticbyte[]Dencrypt(byte[]encrypted){
  56. try{
  57. byte[]expBytes=Base64.decode(delement);
  58. byte[]modBytes=Base64.decode(module);
  59. BigIntegermodules=newBigInteger(1,modBytes);
  60. BigIntegerexponent=newBigInteger(1,expBytes);
  61. KeyFactoryfactory=KeyFactory.getInstance("RSA");
  62. Ciphercipher=Cipher.getInstance("RSA");
  63. RSAPrivateKeySpecprivSpec=newRSAPrivateKeySpec(modules,exponent);
  64. PrivateKeyprivKey=factory.generatePrivate(privSpec);
  65. cipher.init(Cipher.DECRYPT_MODE,privKey);
  66. byte[]decrypted=cipher.doFinal(encrypted);
  67. returndecrypted;
  68. }catch(Exceptione){
  69. e.printStackTrace();
  70. }
  71. returnnull;
  72. }
  73. }

C#代码收藏代码
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Security.Cryptography;
  6. namespaceRSA
  7. {
  8. classProgram
  9. {
  10. staticvoidMain(string[]args)
  11. {
  12. stringde="iBILuPJFgPMxgpbgN3F2JjD6XjcqRSApjVVbvBBEBDV21Pjj7lTrfhEjSVnJX/MVoZrmX0lxsvoXTMvvVwVF7K7W5hs7Qo+aMN96yWke7wiLEM9M4pPz60A/KSckskiona67tXcqOLXb8N18TKaNCKHv0Ce+GyEKK5+MT7e1vao=";
  13. //stringencrypt=RSAEncrypt("","chenhailong");
  14. byte[]encrypt=RSAEncrypt("chenhailong");
  15. //stringname=RSADecrypt(encrypt);
  16. stringname=RSADecrypt(Convert.FromBase64String(de));
  17. Console.WriteLine(encrypt.Length);
  18. Console.WriteLine(Convert.ToBase64String(encrypt));
  19. Console.WriteLine(name);
  20. Console.ReadKey();
  21. }
  22. ///<summary>
  23. ///RSAencrypt
  24. ///</summary>
  25. ///<paramname="publickey"></param>
  26. ///<paramname="content"></param>
  27. ///<returns></returns>
  28. publicstaticbyte[]RSAEncrypt(stringcontent)
  29. {
  30. stringpublickey=@"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
  31. RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();
  32. byte[]cipherbytes;
  33. rsa.FromXmlString(publickey);
  34. cipherbytes=rsa.Encrypt(Encoding.UTF8.GetBytes(content),false);
  35. //returnConvert.ToBase64String(cipherbytes);
  36. returncipherbytes;
  37. }
  38. ///<summary>
  39. ///RSAdecrypt
  40. ///</summary>
  41. ///<paramname="privatekey"></param>
  42. ///<paramname="content"></param>
  43. ///<returns></returns>
  44. publicstaticstringRSADecrypt(byte[]content)
  45. {
  46. stringprivatekey=@"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
  47. RSACryptoServiceProviderrsa=newRSACryptoServiceProvider();
  48. byte[]cipherbytes;
  49. rsa.FromXmlString(privatekey);
  50. cipherbytes=rsa.Decrypt(content,false);
  51. returnEncoding.UTF8.GetString(cipherbytes);
  52. }
  53. }
  54. }

有什么问题 请给我留言

下面是Key的互通代码

Java代码收藏代码
  1. privatebyte[]removeMSZero(byte[]data){
  2. byte[]data1;
  3. intlen=data.length;
  4. if(data[0]==0){
  5. data1=newbyte[data.length-1];
  6. System.arraycopy(data,1,data1,0,len-1);
  7. }else
  8. data1=data;
  9. returndata1;
  10. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表