程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# RSA和Java RSA互通

C# RSA和Java RSA互通

編輯:C#入門知識

今天調查了C# RSA和Java RSA,網上很多人說,C#加密或者java加密 ,Java不能解密或者C#不能解密

但是我嘗試了一下,發現是可以的,下面就是我嘗試的代碼,如果您有什麼問題,我想看看,他們為什麼不能互通?

Rsamain代碼  收藏代碼
  1. package rsa;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.security.KeyFactory;  
  5. import java.security.PrivateKey;  
  6. import java.security.PublicKey;  
  7. import java.security.spec.RSAPrivateKeySpec;  
  8. import java.security.spec.RSAPublicKeySpec;  
  9.   
  10. import javax.crypto.Cipher;  
  11.   
  12. import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;  
  13. import com.sun.org.apache.xml.internal.security.utils.Base64;  
  14.   
  15.   
  16. /**  
  17.  * @author cnchenhl  
  18.  * Jul 8, 2011  
  19.  */  
  20. public class RSAMain {  
  21.   
  22.     private static String module = "5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=";  
  23.     private static String exponentString = "AQAB";  
  24.     private static String delement = "vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=";  
  25.     private static String encryptString = "Vx/dGjS1YWKRubsoDgiShiwLgqyNE2z/eM65U7HZx+RogwaiZimNBxjuOS6acEhKZx66cMYEAd1fc6oewbEvDIfP44GaN9dCjKE/BkkQlwEg6aTO5q+yqy+nEGe1kvLY9EyXS/Kv1LDh3e/2xAk5FNj8Zp6oU2kq4ewL8kK/ai4=";  
  26.     /**  
  27.      * @param args  
  28.      */  
  29.     public static void main(String[] args) {  
  30.         byte[] en = encrypt();  
  31.         System.out.println(Base64.encode(en));  
  32.         byte[] enTest = null;  
  33.         try {  
  34.             enTest = Base64.decode(encryptString);  
  35.         } catch (Base64DecodingException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         System.out.println(enTest.length);  
  39.         System.out.println(en.length);  
  40.         System.out.println(new String(Dencrypt(en)));  
  41.         System.out.println(new String(Dencrypt(enTest)));  
  42.     }  
  43.   
  44.     public static byte[] encrypt() {  
  45.         try {  
  46.             byte[] modulusBytes = Base64.decode(module);  
  47.             byte[] exponentBytes = Base64.decode(exponentString);  
  48.             BigInteger modulus = new BigInteger(1, modulusBytes);  
  49.             BigInteger exponent = new BigInteger(1, exponentBytes);  
  50.   
  51.             RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);  
  52.             KeyFactory fact = KeyFactory.getInstance("RSA");  
  53.             PublicKey pubKey = fact.generatePublic(rsaPubKey);  
  54.   
  55.             Cipher cipher = Cipher.getInstance("RSA");  
  56.             cipher.init(Cipher.ENCRYPT_MODE, pubKey);  
  57.   
  58.             byte[] cipherData = cipher.doFinal(new String("chenhailong").getBytes());  
  59.             return cipherData;  
  60.         } catch (Exception e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.         return null;  
  64.   
  65.     }  
  66.   
  67.     public static byte[] Dencrypt(byte[] encrypted) {  
  68.         try {  
  69.             byte[] expBytes = Base64.decode(delement);  
  70.             byte[] modBytes = Base64.decode(module);  
  71.   
  72.             BigInteger modules = new BigInteger(1, modBytes);  
  73.             BigInteger exponent = new BigInteger(1, expBytes);  
  74.   
  75.             KeyFactory factory = KeyFactory.getInstance("RSA");  
  76.             Cipher cipher = Cipher.getInstance("RSA");  
  77.   
  78.             RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);  
  79.             PrivateKey privKey = factory.generatePrivate(privSpec);  
  80.             cipher.init(Cipher.DECRYPT_MODE, privKey);  
  81.             byte[] decrypted = cipher.doFinal(encrypted);  
  82.             return decrypted;  
  83.         } catch (Exception e) {  
  84.             e.printStackTrace();  
  85.         }  
  86.         return null;  
  87.     }  
  88. }  

 

C#代碼  收藏代碼
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Security.Cryptography;  
  6.   
  7. namespace RSA  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             string de  = "iBILuPJFgPMxgpbgN3F2JjD6XjcqRSApjVVbvBBEBDV21Pjj7lTrfhEjSVnJX/MVoZrmX0lxsvoXTMvvVwVF7K7W5hs7Qo+aMN96yWke7wiLEM9M4pPz60A/KSckskiona67tXcqOLXb8N18TKaNCKHv0Ce+GyEKK5+MT7e1vao=";  
  14.             //string encrypt = RSAEncrypt("", "chenhailong");  
  15.             byte[] encrypt = RSAEncrypt("chenhailong");  
  16.             //string name = RSADecrypt(encrypt);  
  17.             string name = RSADecrypt(Convert.FromBase64String(de));  
  18.             Console.WriteLine(encrypt.Length);  
  19.             Console.WriteLine(Convert.ToBase64String(encrypt));  
  20.             Console.WriteLine(name);  
  21.             Console.ReadKey();  
  22.         }  
  23.         /// <summary>  
  24.         /// RSA encrypt  
  25.         /// </summary>  
  26.         /// <param name="publickey"></param>  
  27.         /// <param name="content"></param>  
  28.         /// <returns></returns>  
  29.         public static byte[] RSAEncrypt(string content)  
  30.         {  
  31.             string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";  
  32.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  33.             byte[] cipherbytes;  
  34.             rsa.FromXmlString(publickey);  
  35.             cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);  
  36.   
  37.             //return Convert.ToBase64String(cipherbytes);  
  38.             return cipherbytes;  
  39.         }  
  40.   
  41.         /// <summary>  
  42.         /// RSA decrypt  
  43.         /// </summary>  
  44.         /// <param name="privatekey"></param>  
  45.         /// <param name="content"></param>  
  46.         /// <returns></returns>  
  47.         public static string RSADecrypt(byte[] content)  
  48.         {  
  49.             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>";  
  50.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  51.             byte[] cipherbytes;  
  52.             rsa.FromXmlString(privatekey);  
  53.             cipherbytes = rsa.Decrypt(content, false);  
  54.   
  55.             return Encoding.UTF8.GetString(cipherbytes);  
  56.         }  
  57.   
  58.   
  59.     }  
  60.   
  61. }  

 有什麼問題 請給我留言

下面是Key的互通代碼

Java代碼  收藏代碼
  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.   
  10.       return data1;  
  11.   }  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved