I am trying to send a third party an RSA encrypted string. They have provided me with a public key. I have multiple systems that need to transmit the data.
One such system is vb.net/asp.net, and is using javascript to do the encryption which works perfectly, via jsencrypt: https://unpkg.com/browse/jsencrypt@2.3.1/README.md The result is consistently 344 characters and ends with =
I also have a desktop app in vb.net that needs to do the same, so I'm using System.Security.Cryptography.RSACryptoServiceProvider, but the result is much longer than what I see sending via the JS code. (It also doesn't end with "=" like the JS code) The result is consistently 392 and never ends with =
Here is my vb.net code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strPublicKey As String = "<RSAKeyValue><Modulus>[Redacted]</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
Dim fStringToSend As String = "This is a test."
Dim byStringToSend As Byte() = Encoding.UTF8.GetBytes(fStringToSend)
Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider(1024)
RSA.FromXmlString(strPublicKey)
Dim strEncryptedStringToSend As String = Convert.ToBase64String(RSA.Encrypt(byStringToSend, True))
TextBox1.Text = strEncryptedStringToSend
End Sub
What am I missing? Let me know if any further data is needed to diagnose & thanks!
EDIT (adding full code, with a key I generated myself to test with)
EncTest.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="EncTest.aspx.vb" Inherits="EncTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="jsencrypt.js"></script>
<script type="text/javascript">
const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0qoqRSNKH+sq7mZKm9f822bhqtD4y4DIRD8++9d0zok5FQrjZHSuGc917zzUGd2IiUzRrLOt/ZMenk42/Wm55rkYf9/MsdHo94vqcHj/5yFYg3QUhgN7l7okIRfAzCkxMl/6x4RymVInB1td5F6N2mChqRm+hzWr9d0I4wLWOpwIDAQAB';
function encText() {
var textToEncrypt = 'This is a test.';
const crypt = new JSEncrypt();
crypt.setKey(publicKey);
const encryptedText = crypt.encrypt(textToEncrypt);
document.getElementById('Label2').innerHTML = encryptedText;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="net" /><br />
<br />
<asp:label ID="Label1" runat="server"></asp:label><br />
<br />
<div id="Label2"></div><br />
<br />
<div onclick="encText();">js</div>
</div>
</form>
</body>
</html>
EncTest.aspx.vb
Partial Class EncTest
Inherits System.Web.UI.Page
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strPublicKey As String = "<RSAKeyValue><Modulus>MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0qoqRSNKH+sq7mZKm9f822bhqtD4y4DIRD8++9d0zok5FQrjZHSuGc917zzUGd2IiUzRrLOt/ZMenk42/Wm55rkYf9/MsdHo94vqcHj/5yFYg3QUhgN7l7okIRfAzCkxMl/6x4RymVInB1td5F6N2mChqRm+hzWr9d0I4wLWOpwIDAQAB</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
Dim fStringToSend As String = "This is a test."
Dim byStringToSend As Byte() = Encoding.UTF8.GetBytes(fStringToSend)
Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider = New System.Security.Cryptography.RSACryptoServiceProvider(1024)
RSA.FromXmlString(strPublicKey)
Dim strEncryptedStringToSend As String = Convert.ToBase64String(RSA.Encrypt(byStringToSend, False))
Label1.Text = strEncryptedStringToSend
End Sub
End Class
And here are examples of the encrypted strings: .net: JNG3rMmqZkzWXwIIOLVCa2e3vTV2doPEKiSyxBFtgYEbnKpkkCKiri7Vdkovn+2T+ArPuVzgTpPEelWN/3gBtvTxSQ4Vt1+H4MMUjFcCwkYoom1V2FyzIBIZ0eqjNQfqos9F10FIQ3NkqiDCDoNHKA5yNfPLBeNQfB5C/bCzCffE6hgHqc7+zkc651V9WSBTuZq7KzgHFUmYHB+juLuZxZGt
javascript: UGjvEQy5ZFSDJt/SKL/39giZP7iYqlR3u3f6P5FpCf/z5p/lnIIyhSNNoSOwKQspPajbczO9DxSFElKKmQSEH9keQrhj4DtcSyPMHInFWra13/fWK9VA5XiktEc2vb9XUQVeZ7DS19ZDYVq7cpA9vApiYztSMClS8CCMuatO0Gs=