//这是Java加密的方法
string value="12345678123456781234567812345678";
string publickey="30819F300D06092A864886F70D010101050003818D0030818902818100D323C781F7F967FCC570CB963FB2064D6600CDA46217D5BF593A5E28701DD91EDAF76F123B21B302C9A97C70D6405E10A58EDD4F369EE0B9C07635B39DC0FAEEA8D0056BB8E3DDC4111C1736BBE43FA8CDA7C26258F244DEE9520EC1960744D3B8FC16179A4C8B24B4E0316BE20D0D3E7893C42A98E26996B80D2C647882FDDB0203010001";
public static String encode(String value, String publicKey)
{
byte[] valueByte = value.getBytes();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = PublicUtil.parseHexStr2Byte(publicKey);
Key pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(
encodedKey));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] enBytes = cipher.doFinal(valueByte);
String result = PublicUtil.parseByte2HexStr(enBytes);
return result;
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
在.net中,如何将上面方法加密的值,给解密出来,提供了encodePrivateKey的值
30820276020100300D06092A864886F70D0101010500048202603082025C02010002818100D323C781F7F967FCC570CB963FB2064D6600CDA46217D5BF593A5E28701DD91EDAF76F123B21B302C9A97C70D6405E10A58EDD4F369EE0B9C07635B39DC0FAEEA8D0056BB8E3DDC4111C1736BBE43FA8CDA7C26258F244DEE9520EC1960744D3B8FC16179A4C8B24B4E0316BE20D0D3E7893C42A98E26996B80D2C647882FDDB02030100010281807C365159A1A3A54C6C6F36261724C956614AD9AFFA51615B27C811BAA272BCD7CD23C1C12CEE5F5CA57CC30BB2CFBD46F7CEFB5D6D99BC452AF7A012221D74C13EDD9F10856865C6F1F4DED24019026D3533AE46946DFC1C50C96C12AFAE4DEFBD309FBD0D5960B42B6EC2B0682974DCF00924FDFB8FD21837FA9702FF70B301024100F2878BF19598F3C7C88E6F7B772734CDF3B257237E89CD2121B86830B5F5D49ADF1DA77DB7FF2A69535D9AB25FBAD99EF1DA841ED1CBECEFCF7ABA4CBFBFC681024100DEDDE96AEA891D72E4A198A00179923D83FFED39CD5B63E7F33D37CFEF8A003D8ED0E9EB38C536BB2E48B5503A757072C634F35FE861A5558CBB0FD19A0C6E5B024100E2D89BB1CC7E32CED42D3DE31AD10617F8EFFF87488D35DBDE81709AD72767C3B01B399562AB7D8CE497F1E1139F5E7BC7B5DECC40E95FE5F287D58781F6AB810240156F2446DB1DAACE64FCC1B009D446635231A8001A2702F9C8E4075D213D957E5620F38E62DC5CC22DD66251BFFD917E086009541D065F1F56349B75DE29F9E702403E3F4B0415362A8D6C3A829E0E4D326C7E1652715FC3AAADAAA744186161276B22F4CC28624172D2CA79BDDC9A0C3B453E71BBE5C116E57462756C1B9BBEA008
在.net中如何解密
算法RSA KEY你也知道,调用系统的解密算法就行了啊,那个什么HEX2Str Str2HEX方法你也有啊,.net里写一个一样的转换结果先,然后解密就完了。
JAVA和.NET虽然都封装了,可能有些细节不一样,.NET也支持RSA算法啊,你看看这篇文章:
http://blog.csdn.net/llwinnner/article/details/4011936
我将秘钥转换成.net所需要的格式,
RSAKeyConvert rsa = new RSAKeyConvert();
privatekey = rsa.RSAPrivateKeyJava2DotNet(privatekey);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(privatekey);
byte[] rgb = parseHexStr2Byte(value);
byte[] buffer2 = provider.Decrypt(rgb, false);
string str2 = Encoding.ASCII.GetString(buffer2);
return str2;
用这个方法,就解密出来了
都是一样的做法啊,.net肯定也提供了RSA的,你利用给你的公钥就可以了