我需要复制C#代码以生成加密字符串。 C#代码如下
private RSACryptoServiceProvider m_rsa;
m_rsa = new RSACryptoServiceProvider(1024);
m_rsa.FromXmlString(privKey);//privKey is from properties file
string encrypted = Convert.ToBase64String(m_rsa.SignData(Encoding.UTF8.GetBytes("Original Text to Encrypt"), "SHA1"));
我在一些goolgle搜索堆栈流的帮助下尝试了如下的Java代码,但是我得到了不同的加密,即与生成的c#不匹配。
String modulusString = "yJNZfy7lMF/O9N......=";//shorted for better view
String privateExponentString = "AQAB";
byte[] modulusBytes = Base64.decodeBase64(modulusString);
byte[] exponentBytes = Base64.decodeBase64(privateExponentString);
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger privateExponent = new BigInteger(1, exponentBytes);
RSAPrivateKeySpec rsaPrvKey =new RSAPrivateKeySpec(modulus, privateExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privateKey = fact.generatePrivate(rsaPrvKey);
Base64 base64Encoder = new Base64();//Base64 is from apache commons codec as I am using java 1.7
byte[] encBytes = tocrypt.getBytes("UTF-8");
Signature sig=Signature.getInstance("SHA1WithRSA");
sig.initSign(privateKey);
sig.update(encBytes);
String encrypted = new String(base64Encoder.encode(sig.sign()));
System.out.println(encrypted);
请帮我弄错。
感谢和问候,