웹 서비스 팀에서 작성한이 C# 코드는 내가 사용하려고하는 일부 웹 서비스를 제공합니다. 웹 서비스가 암호를 해독하는 방법을 알 수 있도록 암호를이 코드로 암호화해야합니다.C# RSACryptoServiceProvider를 자바 코드로 변환
using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
byte[] plainBytes = Encoding.Unicode.GetBytes(clearText);
byte[] encryptedBytes = rsa.Encrypt(plainBytes, false);
return Convert.ToBase64String(encryptedBytes);
}
내가 그 웹 서비스가 제대로 암호를 해독 할 수 있기 때문에 자바 코드에 그 #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"))));
, 나는이 오류를 받고 있어요 : "데이터의 암호를 해독 할 수는 128 바이트의 계수의 최대 값을 초과합니다." 일반 텍스트 암호가 여전히 제대로 암호화되지 않은 것 같습니다.
도움이나 의견을 크게 주시면 감사하겠습니다. 감사.
2013년 8월 9일 -
솔루션은 내가 아래에있는 내 마지막 작업 솔루션을 기록했다.
이것이 암호 대신 로그인 프로토콜 문제가된다는 느낌이 들었습니다. 우리는 그 밖의 모든 것을 다룹니다. 다행스럽게 생각 했어. – Dev
'Encoding.Unicode.GetBytes (String)'을 볼 때 'UTF-16LE'이 아닌'UTF-8'을 생각하고 있습니다. 분명하고 잘못된 가정으로 여러분의 의견을 잘 파악하고 있습니다. – Dev
고마워 - 내 문제를 해결. NET에서 사람들이 UTF-8이 아니라는 것을 깨닫지 못하는 것처럼 Encoding.Unicode.GetBytes/Encoding.Unicode.GetString을 자주 사용하는 것처럼 보입니다. – mvmn