[英]java cipher to c# equivalent
I am very grateful with any help.我非常感谢任何帮助。 I am trying to convert a rsa cryptor java implementation to c#.
我正在尝试将 rsa cryptor java 实现转换为 c#。 I need to encrypt credit card data.
我需要加密信用卡数据。 The platform i use provides cryptography SDK for javascript and android, but I need a C# implementation.
我使用的平台为 javascript 和 android 提供了加密 SDK,但我需要一个 C# 实现。
try {
cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
} catch (SecurityException se) {
//workaround for tests
Log.i("Moip SDK", "No SC provider, running test profile");
cipher = Cipher.getInstance("RSA");
}
BufferedReader pemReader = null;
pemReader = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(publicKey.getBytes("UTF-8"))));
StringBuffer content = new StringBuffer();
String line = null;
while ((line = pemReader.readLine()) != null) {
if (line.indexOf("-----BEGIN PUBLIC KEY-----") != -1) {
while ((line = pemReader.readLine()) != null) {
if (line.indexOf("-----END PUBLIC KEY") != -1) {
break;
}
content.append(line.trim());
}
break;
}
}
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT))));
byte[] cipherText = cipher.doFinal(toHash().getBytes());
return Base64.encodeToString(cipherText, Base64.DEFAULT);
The code I created looking at the implementation in java, it works but fails to be decrypted on the platform, I must be forgetting something.我创建的代码查看了 java 中的实现,它可以工作但无法在平台上解密,我一定是忘记了一些东西。 My implementation:
我的实现:
private string Payload(string number, string expirationMonth, string expirationYear, string cvc) {
return String.Join(
"&",
new List<string> {
$"number={number}",
$"cvc={cvc}",
$"expirationMonth={expirationMonth}",
$"expirationYear={expirationYear}",
}
);
}
private string Encrypt(string key, string payload) {
var publicKey = $"<RSAKeyValue><Modulus>{key}</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
var rsa = new RSACryptoServiceProvider(2048);
rsa.FromXmlString(publicKey);
var encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(payload), RSAEncryptionPadding.Pkcs1);
rsa.Dispose();
return Convert.ToBase64String(encryptedData);
}
var payload = Payload(number, expirationMonth, expirationYear, cvc);
hash = Encrypt(ClientManager.Environment.publicKeyCreditCard, payload);
Welcome to Stackoverflow.欢迎使用 Stackoverflow。 I run your codes and I could encrypt and decrypt on both platforms (Java & C#) successfully.
我运行你的代码,我可以成功地在两个平台(Java 和 C#)上加密和解密。
So in my eyes the only reason why decryption fails when doing encryption on C# is that you didn't convert the public key correctly.因此,在我看来,在 C# 上进行加密时解密失败的唯一原因是您没有正确转换公钥。
I recommend to use (as it is the pub key) an online converter like https://superdry.apphb.com/tools/online-rsa-key-converter .我建议使用(因为它是公钥)像https://superdry.apphb.com/tools/online-rsa-key-converter这样的在线转换器。
Just copy the public key in the lower input text area "PEM to XML" like this one (it's an unsecure 512 bit RSA-key so it will be shorter than your one):只需像这样复制下方输入文本区域“PEM to XML”中的公钥(它是一个不安全的512 位 RSA 密钥,因此它会比您的密钥短):
-----BEGIN PUBLIC KEY-----" +
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJn4LYaoLyuT+pD9NKI8wOvPrRjMIxGn
HbqIxUrpGsyj162fEOV4836oCZg0N8HFnt4Vivdjt8/7ZgLjeOygNGUCAwEAAQ==
-----END PUBLIC KEY-----
and press "Convert" - below you get the public key in xml-format:然后按“转换” - 在下面你会得到 xml 格式的公钥:
<RSAKeyValue><Modulus>mfgthqgvK5P6kP00ojzA68+tGMwjEacduojFSukazKPXrZ8Q5XjzfqgJmDQ3wcWe3hWK92O3z/tmAuN47KA0ZQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
The partial key that you have to present to your C# method is after Modulus:您必须向 C# 方法提供的部分键位于 Modulus 之后:
mfgthqgvK5P6kP00ojzA68+tGMwjEacduojFSukazKPXrZ8Q5XjzfqgJmDQ3wcWe3hWK92O3z/tmAuN47KA0ZQ==
You should have an eye on the value - in most times it is "AQAB" but in case you receive another value you have to adjust your source code in C#.您应该注意该值 - 在大多数情况下它是“AQAB”,但如果您收到另一个值,您必须在 C# 中调整源代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.