Webサービスチームによって書かれたこのC#コードは、私が使用しようとしているWebサービスを公開しています。私のパスワードはこのコードで暗号化されている必要があります。そのため、Webサービスはそのコードを解読する方法を知っています。C#RSACryptoServiceProviderをJAVコードに翻訳する
using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
byte[] plainBytes = Encoding.Unicode.GetBytes(clearText);
byte[] encryptedBytes = rsa.Encrypt(plainBytes, false);
return Convert.ToBase64String(encryptedBytes);
}
私は、このWebサービスを利用するためにJavaを使用していて、そのWebサービスが適切に自分のパスワードを解読することはできませんので、今、私は、Javaコードにその#Cコードを変換する問題を抱えています。私は間違って何をしました
// 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"))));
- :
は、ここに私の現在の失敗したのか?どうもありがとう。
2013年8月7日 - UPDATE
私はthis websiteを読んでいたし、私は私のモジュラス値と公開指数値が六角になっていないことに気づきました。だから、コードを少し修正して、@DevのようにRSA/ECB/PKCS1PADDING
で試してみました。
// 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"))));
私はWebサービスを打つとき、私はこのエラーを取得しています:「のデータを復号化するには、128バイトのこの係数の最大値を超えています。」クリアテキストパスワードがまだ適切に暗号化されていないようです。
ご迷惑をおかけして申し訳ございません。ありがとう。
2013年8月9日 -
ソリューションは、私は以下の私の最終作業溶液を掲載しました。
私はこれが暗号の代わりにログインプロトコルの問題になるだろうと感じました。私たちは他のすべてをカバーしました。あなたがそれを理解してうれしいです。 – Dev
'Encoding.Unicode.GetBytes(String)'が見えたら、UTF-16LEではなく、UTF-8であると思います。 – Dev
ありがとうございます - 私の問題を解決しました。 NETの人々は、UTF-8ではないことをよく認識していないEncoding.Unicode.GetBytes/Encoding.Unicode.GetStringを使用するように見えます。 – mvmn