I want to run my C# code to login a web site, so I need to implement the RSA Encryption method in the web site. Below is my C# and JavaScript test code to encrypt "test", but they display the different results.How to modify the C# code to get the same result as the JavaScript code?
JavaScript:Save the code to a html file and opened by web browser will see the result.
Or run the code online:https://onlinegdb.com/fzKiCrbGf
It is the JavaScript RSA library with documentation comments:http://www.ohdave.com/rsa/RSA.js
<script src="http://www.ohdave.com/rsa/RSA.js"></script>
<script src="http://www.ohdave.com/rsa/Barrett.js"></script>
<script src="http://www.ohdave.com/rsa/BigInt.js"></script>
<script>
setMaxDigits(130);
var key = new RSAKeyPair("010001","","906C793510FB049452764740B21B97A51DAEA794AB6E43836269D5E6317D49226C12362BA22DAB5EC3BC79553A8A098B01F3C4D81A87B3EE5BD2F4F1431CC495EE2FE54688B212145BB32D56EEEEE1430CE26234331B291CFC53C9B84FAFFDF0B44371A032880C3D567F588D2CD5FCE28D9CDD2923CB547DAD219A6A1B8B5D3D");
var result=encryptedString(key,"test")
document.write(result);
</script>
C#:It is the code of the C# Consloe Program.Run the code will see the result output to the consloe.
Or run the code online:https://onlinegdb.com/B1wG_5rXu
class Program
{
static void Main(string[] args)
{
System.Security.Cryptography.RSAParameters rsaParams = new System.Security.Cryptography.RSAParameters
{
Modulus = HexToByteArray("906C793510FB049452764740B21B97A51DAEA794AB6E43836269D5E6317D49226C12362BA22DAB5EC3BC79553A8A098B01F3C4D81A87B3EE5BD2F4F1431CC495EE2FE54688B212145BB32D56EEEEE1430CE26234331B291CFC53C9B84FAFFDF0B44371A032880C3D567F588D2CD5FCE28D9CDD2923CB547DAD219A6A1B8B5D3D"),
Exponent = HexToByteArray("010001"),
};
System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
rsa.ImportParameters(rsaParams);
byte[] result = rsa.Encrypt(System.Text.Encoding.UTF8.GetBytes("test"), false);
System.Console.Write(ByteArrayToHex(result));
System.Console.ReadKey();
}
public static byte[] HexToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static string ByteArrayToHex(byte[] ba)
{
System.Text.StringBuilder hex = new System.Text.StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
}