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

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

  1. package rsa;
  2. import java.math.BigInteger;
  3. import java.security.KeyFactory;
  4. import java.security.PrivateKey;
  5. import java.security.PublicKey;
  6. import java.security.spec.RSAPrivateKeySpec;
  7. import java.security.spec.RSAPublicKeySpec;
  8. import javax.crypto.Cipher;
  9. import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
  10. import com.sun.org.apache.xml.internal.security.utils.Base64;
  11. /**
  12. * @author cnchenhl
  13. * Jul 8, 2011
  14. */
  15. public class RSAMain {
  16. private static String module = "5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=";
  17. private static String exponentString = "AQAB";
  18. private static String delement = "vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=";
  19. private static String encryptString = "Vx/dGjS1YWKRubsoDgiShiwLgqyNE2z/eM65U7HZx+RogwaiZimNBxjuOS6acEhKZx66cMYEAd1fc6oewbEvDIfP44GaN9dCjKE/BkkQlwEg6aTO5q+yqy+nEGe1kvLY9EyXS/Kv1LDh3e/2xAk5FNj8Zp6oU2kq4ewL8kK/ai4=";
  20. /**
  21. * @param args
  22. */
  23. public static void main(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 (Base64DecodingException e) {
  30. e.printStackTrace();
  31. }
  32. System.out.println(enTest.length);
  33. System.out.println(en.length);
  34. System.out.println(new String(Dencrypt(en)));
  35. System.out.println(new String(Dencrypt(enTest)));
  36. }
  37. public static byte[] encrypt() {
  38. try {
  39. byte[] modulusBytes = Base64.decode(module);
  40. byte[] exponentBytes = Base64.decode(exponentString);
  41. BigInteger modulus = new BigInteger(1, modulusBytes);
  42. BigInteger exponent = new BigInteger(1, exponentBytes);
  43. RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
  44. KeyFactory fact = KeyFactory.getInstance("RSA");
  45. PublicKey pubKey = fact.generatePublic(rsaPubKey);
  46. Cipher cipher = Cipher.getInstance("RSA");
  47. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  48. byte[] cipherData = cipher.doFinal(new String("chenhailong").getBytes());
  49. return cipherData;
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. return null;
  54. }
  55. public static byte[] Dencrypt(byte[] encrypted) {
  56. try {
  57. byte[] expBytes = Base64.decode(delement);
  58. byte[] modBytes = Base64.decode(module);
  59. BigInteger modules = new BigInteger(1, modBytes);
  60. BigInteger exponent = new BigInteger(1, expBytes);
  61. KeyFactory factory = KeyFactory.getInstance("RSA");
  62. Cipher cipher = Cipher.getInstance("RSA");
  63. RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);
  64. PrivateKey privKey = factory.generatePrivate(privSpec);
  65. cipher.init(Cipher.DECRYPT_MODE, privKey);
  66. byte[] decrypted = cipher.doFinal(encrypted);
  67. return decrypted;
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return null;
  72. }
  73. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. namespace RSA
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string de  = "iBILuPJFgPMxgpbgN3F2JjD6XjcqRSApjVVbvBBEBDV21Pjj7lTrfhEjSVnJX/MVoZrmX0lxsvoXTMvvVwVF7K7W5hs7Qo+aMN96yWke7wiLEM9M4pPz60A/KSckskiona67tXcqOLXb8N18TKaNCKHv0Ce+GyEKK5+MT7e1vao=";
  13. //string encrypt = RSAEncrypt("", "chenhailong");
  14. byte[] encrypt = RSAEncrypt("chenhailong");
  15. //string name = RSADecrypt(encrypt);
  16. string name = 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. /// RSA encrypt
  24. /// </summary>
  25. /// <param name="publickey"></param>
  26. /// <param name="content"></param>
  27. /// <returns></returns>
  28. public static byte[] RSAEncrypt(string content)
  29. {
  30. string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
  31. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  32. byte[] cipherbytes;
  33. rsa.FromXmlString(publickey);
  34. cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
  35. //return Convert.ToBase64String(cipherbytes);
  36. return cipherbytes;
  37. }
  38. /// <summary>
  39. /// RSA decrypt
  40. /// </summary>
  41. /// <param name="privatekey"></param>
  42. /// <param name="content"></param>
  43. /// <returns></returns>
  44. public static string RSADecrypt(byte[] content)
  45. {
  46. string privatekey = @"<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. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  48. byte[] cipherbytes;
  49. rsa.FromXmlString(privatekey);
  50. cipherbytes = rsa.Decrypt(content, false);
  51. return Encoding.UTF8.GetString(cipherbytes);
  52. }
  53. }
  54. }

有什么问题 请给我留言

下面是Key的互通代码

  1. private byte[] removeMSZero(byte[] data) {
  2. byte[] data1;
  3. int len = data.length;
  4. if (data[0] == 0) {
  5. data1 = new byte[data.length - 1];
  6. System.arraycopy(data, 1, data1, 0, len - 1);
  7. } else
  8. data1 = data;
  9. return data1;
  10. }
05-04 12:30