将 C# RSACryptoServiceProvider 翻译成 JAVA 代码 [英] Translating C# RSACryptoServiceProvider into JAVA Code

查看:29
本文介绍了将 C# RSACryptoServiceProvider 翻译成 JAVA 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了由 Web 服务团队编写的此 C# 代码,该代码公开了我计划使用的一些 Web 服务.我的密码需要使用此代码进行加密,以便网络服务知道如何在其端解密.

using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()){rsa.FromXmlString(publicKey);byte[] plainBytes = Encoding.Unicode.GetBytes(clearText);byte[] encryptedBytes = rsa.Encrypt(plainBytes, false);返回 Convert.ToBase64String(encryptedBytes);}

我正在使用 Java 来使用此 Web 服务,而现在,我在将 #C 代码转换为 Java 代码时遇到了问题,因为该 Web 服务无法正确解密我的密码.

这是我目前失败的尝试:-

//我的明文密码String clearTextPassword = "XXXXX";//这些值由 Web 服务团队提供字符串模数String = "...";String publicExponentString = "...";BigInteger 模数 = new BigInteger(1, Base64.decodeBase64(modulusString.getBytes("UTF-8")));BigInteger publicExponent = new BigInteger(1, Base64.decodeBase64(publicExponentString.getBytes("UTF-8")));KeyFactory keyFactory = KeyFactory.getInstance("RSA");RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");cipher.init(Cipher.ENCRYPT_MODE, publicKey);String encodingEncryptedPassword = new String(Base64.encodeBase64(cipher.doFinal(clearTextPassword.getBytes("UTF-8"))));

我做错了什么?非常感谢.

2013-08-07 - 更新

我正在阅读 这个网站 我意识到我的模数值和公共指数值不是十六进制的.因此,我稍微修改了我的代码并尝试使用@Dev 提到的 RSA/ECB/PKCS1PADDING.

//我的明文密码String clearTextPassword = "XXXXX";//这些是我从 Web 服务团队获得的实际值串modulusString = hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ + jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc =";String publicExponentString = "AQAB";Base64 base64Encoder = new Base64();String ModulusHex = new String(Hex.encodeHex(modulusString.getBytes("UTF-8")));String publicExponentHex = new String(Hex.encodeHex(publicExponentString.getBytes("UTF-8")));BigInteger 模数 = new BigInteger(modulusHex, 16);BigInteger publicExponent = new BigInteger(publicExponentHex);KeyFactory keyFactory = KeyFactory.getInstance("RSA");RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");cipher.init(Cipher.ENCRYPT_MODE, publicKey);String encodingEncryptedPassword = new String(base64Encoder.encode(cipher.doFinal(clearTextPassword.getBytes("UTF-8"))));

当我点击网络服务时,我收到这个错误:要解密的数据超过了这个模数的最大值 128 字节." 似乎明文密码仍然不是正确加密.

非常感谢任何帮助或建议.谢谢.

2013-08-09 - 解决方案

我在下面发布了我的最终工作解决方案.

解决方案

找到解决方案.

<预> <代码>字符串modulusString = hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ + jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc =";String publicExponentString = "AQAB";字节 [] 模数字节 = Base64.decodeBase64(modulusString);byte[] exponentBytes = Base64.decodeBase64(publicExponentString);BigInteger 模数 = new BigInteger(1,modularBytes);BigInteger publicExponent = new BigInteger(1, exponentBytes);RSAPublicKeySpec rsaPubKey = 新的 RSAPublicKeySpec(modulus, publicExponent);KeyFactory fact = KeyFactory.getInstance("RSA");PublicKey pubKey = fact.generatePublic(rsaPubKey);Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");cipher.init(Cipher.ENCRYPT_MODE, pubKey);byte[] plainBytes = clearTextPassword.getBytes("UTF-16LE");byte[] cipherData = cipher.doFinal(plainBytes);String encryptedStringBase64 = Base64.encodeBase64String(cipherData);

I was given this C# code written by the web service team that exposes some web service that I'm planning to consume. My password needs to be encrypted with this code so that the web service knows how to decrypt it on their end.

using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
    rsa.FromXmlString(publicKey);
    byte[] plainBytes = Encoding.Unicode.GetBytes(clearText);
    byte[] encryptedBytes = rsa.Encrypt(plainBytes, false);
    return Convert.ToBase64String(encryptedBytes);
}

I'm using Java to consume this web service and right now, I'm having problem translating that #C code into Java code because that web service can't decrypt my password properly.

Here's my current failed attempt:-

// my clear text password
String clearTextPassword = "XXXXX";

// these values are provided by the web service team
String modulusString = "...";
String publicExponentString = "...";

BigInteger modulus = new BigInteger(1, Base64.decodeBase64(modulusString.getBytes("UTF-8")));
BigInteger publicExponent = new BigInteger(1, Base64.decodeBase64(publicExponentString.getBytes("UTF-8")));

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);

String encodedEncryptedPassword = new String(Base64.encodeBase64(cipher.doFinal(clearTextPassword.getBytes("UTF-8"))));

What did I do wrong? Thanks much.

2013-08-07 - UPDATE

I was reading this website and I realized that my modulus value and public exponent value are not in Hex. So, I modified my code a little bit and tried with RSA/ECB/PKCS1PADDING as mentioned by @Dev.

// my clear text password
String clearTextPassword = "XXXXX";

// these are the actual values I get from the web service team
String modulusString = "hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ+jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc=";
String publicExponentString = "AQAB";

Base64 base64Encoder = new Base64();

String modulusHex = new String(Hex.encodeHex(modulusString.getBytes("UTF-8")));
String publicExponentHex = new String(Hex.encodeHex(publicExponentString.getBytes("UTF-8")));

BigInteger modulus = new BigInteger(modulusHex, 16);
BigInteger publicExponent = new BigInteger(publicExponentHex);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);

String encodedEncryptedPassword = new String(base64Encoder.encode(cipher.doFinal(clearTextPassword.getBytes("UTF-8"))));

When I hit the webservice, I'm getting this error: "The data to be decrypted exceeds the maximum for this modulus of 128 bytes." It seems like the clear text password is still not encrypted properly.

Any help or suggestion is greatly appreciated. Thanks.

2013-08-09 - SOLUTION

I posted my final working solution below.

解决方案

Found the solution.

String modulusString = "hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ+jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc=";
String publicExponentString = "AQAB";

byte[] modulusBytes = Base64.decodeBase64(modulusString);
byte[] exponentBytes = Base64.decodeBase64(publicExponentString);
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger publicExponent = new BigInteger(1, exponentBytes);

RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = clearTextPassword.getBytes("UTF-16LE");
byte[] cipherData = cipher.doFinal(plainBytes);
String encryptedStringBase64 = Base64.encodeBase64String(cipherData);

这篇关于将 C# RSACryptoServiceProvider 翻译成 JAVA 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆