RayFrangie
asked on
VB.NET : RSA Encryption : Encryption not always encrypting at 128 Bits
Hi All,
I'm trying to encrypt a few details (4 variables to be exact) in this application that i'm creating using the RSA Encryption Method.
I can never seem to get it to encrypt all 4 correctly @ 128 bits, even though it says that the length of the encryption is 128 bits (tested using a MsgBox Feature)
The code seems fine to me... It comes back saying that it's encrypted at 128bits ok, but when decryption time comes, at least 1 of the 4 will cause an error in mscorlib.dll saying Bad Data on the line where the RSA.Decrypt method is called.
The Following is the Code of the clsEncryption.vb File
************************** ********** ******
Imports System.Security.Cryptograp hy
Imports System.Text
Public Class clsEncryption
Dim PlainText As Byte()
Dim EncryptedText As Byte()
Dim xmlKeys As String
Dim xmlPublicKey As String
Public Function ProcessEncryption(ByVal PrivateKey As String, ByVal PublicKey As String, ByVal Text As String)
Try
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
RSA.FromXmlString(PublicKe y)
PlainText = (New UnicodeEncoding).GetBytes( Text)
EncryptedText = RSA.Encrypt(PlainText, False)
Dim FinalisedText As String = ""
For i As Integer = 0 To EncryptedText.Length - 1
FinalisedText += Chr(EncryptedText(i))
Next i
If FinalisedText.Length = 128 Then
MsgBox("Encrypted @ 128 Bits OK", 64, "Encryption OK")
Else
MsgBox("Unable to Encrypt @ 128 Bits", 16, "Encryption Failed")
End If
Return FinalisedText
Catch ex As Exception
clsErrors.DisplayError(ex)
End Try
End Function
Public Function ProcessDecryption(ByVal PrivateKey As String, ByVal PublicKey As String, ByVal EncryptedText As Byte())
Try
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
RSA.FromXmlString(PrivateK ey)
PlainText = RSA.Decrypt(EncryptedText, False)
Dim ProperText As String
For i As Integer = 0 To (PlainText.Length - 1) Step 2
ProperText += Chr(PlainText(i))
Next i
Return ProperText
Catch ex As Exception
clsErrors.DisplayError(ex)
End Try
End Function
Public Function CreateKeys(ByVal Keys() As String)
Dim rsa As New RSACryptoServiceProvider
xmlKeys = rsa.ToXmlString(True)
Keys(0) = xmlKeys
xmlPublicKey = rsa.ToXmlString(False)
Keys(1) = xmlPublicKey
Return Keys
End Function
End Class
************************** ********** ******
The Following is the code where the encryption process takes place
************************** ********** ******
Private Sub btnSubmitAppDetails_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmitAppDetails.Click
If txtSQLServer.Text = "" Then
MsgBox("SQL Server Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Server Field Empty")
Exit Sub
ElseIf txtSQLDatabase.Text = "" Then
MsgBox("SQL Database Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database Name Field Empty")
Exit Sub
ElseIf txtSQLuser.Text = "" Then
MsgBox("SQL Database User Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database User Name Field Empty")
Exit Sub
ElseIf txtSQLPassword.Text = "" Then
MsgBox("SQL Database Password Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database Password Field Empty")
Exit Sub
End If
Dim DBServer As String = txtSQLServer.Text
Dim DBName As String = txtSQLDatabase.Text
Dim DBUserName As String = txtSQLuser.Text
Dim DBPassword As String = txtSQLPassword.Text
lblStatus.Text = "Please Wait... Saving Data To Registry..."
lblStatus.Update()
lblStatus.Refresh()
Dim Keys(1) As String
Cryptography.CreateKeys(Ke ys)
lblStatus.Text = "Please Wait... Creating Registry Keys..."
lblStatus.Update()
lblStatus.Refresh()
RegKey = Registry.LocalMachine.Crea teSubKey(" SYSTEM\Cur rentContro lSet\Contr ol\Class\{ 0F0F4125-D CDB-405E-A D8E-F09312 79A240}\00 00\AppData ")
RegKey.SetValue("PRKey", Keys(0))
RegKey.SetValue("PUKey", Keys(1))
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 1 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBServer = Cryptography.ProcessEncryp tion(Keys( 0), Keys(1), DBServer)
RegKey.SetValue("DBServer" , DBServer)
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 2 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBName = Cryptography.ProcessEncryp tion(Keys( 0), Keys(1), DBName)
RegKey.SetValue("DBName", DBName)
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 3 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBUserName = Cryptography.ProcessEncryp tion(Keys( 0), Keys(1), DBUserName)
RegKey.SetValue("DBUser", DBUserName)
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 4 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBPassword = Cryptography.ProcessEncryp tion(Keys( 0), Keys(1), DBPassword)
RegKey.SetValue("DBPass", DBPassword)
lblStatus.Text = "Process Completed Successfully"
lblStatus.Update()
lblStatus.Refresh()
RegKey.Close()
Me.Dispose()
End Sub
************************** ********** ******
The Following Code is where the Decryption takes place
************************** ********** ******
... ... ...
'Decrypt Registry Data and Save to Global Variables
RegKey = Registry.LocalMachine.Open SubKey("SY STEM\Curre ntControlS et\Control \Class\{0F 0F4125-DCD B-405E-AD8 E-F0931279 A240}\0000 \AppData", False)
Dim EncryptedVariable As Byte()
Dim TextVariable As String
Dim PrivateKey As String = RegKey.GetValue("PRKey")
Dim PublicKey As String = RegKey.GetValue("PUKey")
EncryptedVariable = System.Text.Encoding.Defau lt.GetByte s(RegKey.G etValue("D BServer"))
TextVariable = Cryptography.ProcessDecryp tion(Priva teKey, PublicKey, EncryptedVariable)
SQLServer = TextVariable
EncryptedVariable = System.Text.Encoding.Defau lt.GetByte s(RegKey.G etValue("D BName"))
TextVariable = Cryptography.ProcessDecryp tion(Priva teKey, PublicKey, EncryptedVariable)
SQLDBName = TextVariable
EncryptedVariable = System.Text.Encoding.Defau lt.GetByte s(RegKey.G etValue("D BUser"))
TextVariable = Cryptography.ProcessDecryp tion(Priva teKey, PublicKey, EncryptedVariable)
SQLUser = TextVariable
EncryptedVariable = System.Text.Encoding.Defau lt.GetByte s(RegKey.G etValue("D BPass"))
TextVariable = Cryptography.ProcessDecryp tion(Priva teKey, PublicKey, EncryptedVariable)
SQLPassword = TextVariable
... ... ...
************************** ********** ******
If Anyone can provide some assistance into why it aint encrypting/decrypting correctly it'll be greatly appreciated
Many thanks in Advance
Kind Regards,
Ray.
I'm trying to encrypt a few details (4 variables to be exact) in this application that i'm creating using the RSA Encryption Method.
I can never seem to get it to encrypt all 4 correctly @ 128 bits, even though it says that the length of the encryption is 128 bits (tested using a MsgBox Feature)
The code seems fine to me... It comes back saying that it's encrypted at 128bits ok, but when decryption time comes, at least 1 of the 4 will cause an error in mscorlib.dll saying Bad Data on the line where the RSA.Decrypt method is called.
The Following is the Code of the clsEncryption.vb File
**************************
Imports System.Security.Cryptograp
Imports System.Text
Public Class clsEncryption
Dim PlainText As Byte()
Dim EncryptedText As Byte()
Dim xmlKeys As String
Dim xmlPublicKey As String
Public Function ProcessEncryption(ByVal PrivateKey As String, ByVal PublicKey As String, ByVal Text As String)
Try
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
RSA.FromXmlString(PublicKe
PlainText = (New UnicodeEncoding).GetBytes(
EncryptedText = RSA.Encrypt(PlainText, False)
Dim FinalisedText As String = ""
For i As Integer = 0 To EncryptedText.Length - 1
FinalisedText += Chr(EncryptedText(i))
Next i
If FinalisedText.Length = 128 Then
MsgBox("Encrypted @ 128 Bits OK", 64, "Encryption OK")
Else
MsgBox("Unable to Encrypt @ 128 Bits", 16, "Encryption Failed")
End If
Return FinalisedText
Catch ex As Exception
clsErrors.DisplayError(ex)
End Try
End Function
Public Function ProcessDecryption(ByVal PrivateKey As String, ByVal PublicKey As String, ByVal EncryptedText As Byte())
Try
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
RSA.FromXmlString(PrivateK
PlainText = RSA.Decrypt(EncryptedText,
Dim ProperText As String
For i As Integer = 0 To (PlainText.Length - 1) Step 2
ProperText += Chr(PlainText(i))
Next i
Return ProperText
Catch ex As Exception
clsErrors.DisplayError(ex)
End Try
End Function
Public Function CreateKeys(ByVal Keys() As String)
Dim rsa As New RSACryptoServiceProvider
xmlKeys = rsa.ToXmlString(True)
Keys(0) = xmlKeys
xmlPublicKey = rsa.ToXmlString(False)
Keys(1) = xmlPublicKey
Return Keys
End Function
End Class
**************************
The Following is the code where the encryption process takes place
**************************
Private Sub btnSubmitAppDetails_Click(
If txtSQLServer.Text = "" Then
MsgBox("SQL Server Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Server Field Empty")
Exit Sub
ElseIf txtSQLDatabase.Text = "" Then
MsgBox("SQL Database Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database Name Field Empty")
Exit Sub
ElseIf txtSQLuser.Text = "" Then
MsgBox("SQL Database User Name Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database User Name Field Empty")
Exit Sub
ElseIf txtSQLPassword.Text = "" Then
MsgBox("SQL Database Password Field Empty. Please Complete All Fields before Proceeding", 48, "SQL Database Password Field Empty")
Exit Sub
End If
Dim DBServer As String = txtSQLServer.Text
Dim DBName As String = txtSQLDatabase.Text
Dim DBUserName As String = txtSQLuser.Text
Dim DBPassword As String = txtSQLPassword.Text
lblStatus.Text = "Please Wait... Saving Data To Registry..."
lblStatus.Update()
lblStatus.Refresh()
Dim Keys(1) As String
Cryptography.CreateKeys(Ke
lblStatus.Text = "Please Wait... Creating Registry Keys..."
lblStatus.Update()
lblStatus.Refresh()
RegKey = Registry.LocalMachine.Crea
RegKey.SetValue("PRKey", Keys(0))
RegKey.SetValue("PUKey", Keys(1))
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 1 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBServer = Cryptography.ProcessEncryp
RegKey.SetValue("DBServer"
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 2 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBName = Cryptography.ProcessEncryp
RegKey.SetValue("DBName", DBName)
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 3 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBUserName = Cryptography.ProcessEncryp
RegKey.SetValue("DBUser", DBUserName)
lblStatus.Text = "Please Wait... Encrypting Details and Saving to System: 4 / 4..."
lblStatus.Update()
lblStatus.Refresh()
DBPassword = Cryptography.ProcessEncryp
RegKey.SetValue("DBPass", DBPassword)
lblStatus.Text = "Process Completed Successfully"
lblStatus.Update()
lblStatus.Refresh()
RegKey.Close()
Me.Dispose()
End Sub
**************************
The Following Code is where the Decryption takes place
**************************
... ... ...
'Decrypt Registry Data and Save to Global Variables
RegKey = Registry.LocalMachine.Open
Dim EncryptedVariable As Byte()
Dim TextVariable As String
Dim PrivateKey As String = RegKey.GetValue("PRKey")
Dim PublicKey As String = RegKey.GetValue("PUKey")
EncryptedVariable = System.Text.Encoding.Defau
TextVariable = Cryptography.ProcessDecryp
SQLServer = TextVariable
EncryptedVariable = System.Text.Encoding.Defau
TextVariable = Cryptography.ProcessDecryp
SQLDBName = TextVariable
EncryptedVariable = System.Text.Encoding.Defau
TextVariable = Cryptography.ProcessDecryp
SQLUser = TextVariable
EncryptedVariable = System.Text.Encoding.Defau
TextVariable = Cryptography.ProcessDecryp
SQLPassword = TextVariable
... ... ...
**************************
If Anyone can provide some assistance into why it aint encrypting/decrypting correctly it'll be greatly appreciated
Many thanks in Advance
Kind Regards,
Ray.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Greg,
Skip that last comment, i managed to find that post where you got that code from (on vbCity)
Couple of lines did the trick:
Shared KeyExchangeFormater As RSAOAEPKeyExchangeFormatte r = New RSAOAEPKeyExchangeFormatte r
Shared KeyExchangeDeformatter As RSAOAEPKeyExchangeDeformat ter = New RSAOAEPKeyExchangeDeformat ter
Although Quick Question.... Isn't OEAP Limited to Windows XP and Later???
Let me know
Kind Regards,
Ray.
Skip that last comment, i managed to find that post where you got that code from (on vbCity)
Couple of lines did the trick:
Shared KeyExchangeFormater As RSAOAEPKeyExchangeFormatte
Shared KeyExchangeDeformatter As RSAOAEPKeyExchangeDeformat
Although Quick Question.... Isn't OEAP Limited to Windows XP and Later???
Let me know
Kind Regards,
Ray.
I grabbed it more so to show the change of using byte arrays and base 64 to make strings ...
GetStringFromByteArray and GetByteArrayFromString
GetStringFromByteArray and GetByteArrayFromString
ASKER
Its cool, its solved the problem and the points are all yours
Just a quick Question as per the above post:
... ... ...
Shared KeyExchangeFormater As RSAOAEPKeyExchangeFormatte r = New RSAOAEPKeyExchangeFormatte r
Shared KeyExchangeDeformatter As RSAOAEPKeyExchangeDeformat ter = New RSAOAEPKeyExchangeDeformat ter
Although Quick Question.... Isn't OEAP Limited to Windows XP and Later???
Many thanks and Regards,
Ray.
Just a quick Question as per the above post:
... ... ...
Shared KeyExchangeFormater As RSAOAEPKeyExchangeFormatte
Shared KeyExchangeDeformatter As RSAOAEPKeyExchangeDeformat
Although Quick Question.... Isn't OEAP Limited to Windows XP and Later???
Many thanks and Regards,
Ray.
Use RSAOAEPKeyExchangeDeformat ter to receive the key exchange and extract the secret information from it.
CAUTION It is highly recommended that you not attempt to create your own key exchange method from the basic functionality provided, because many details of the operation must be performed carefully in order for the key exchange to be successful.
Requirements
Namespace: System.Security.Cryptograp hy
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
so I guess it may work.
CAUTION It is highly recommended that you not attempt to create your own key exchange method from the basic functionality provided, because many details of the operation must be performed carefully in order for the key exchange to be successful.
Requirements
Namespace: System.Security.Cryptograp
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
so I guess it may work.
ASKER
Hmmm... Interesting... I thought OEAP was only limited to XP and above...
Oh Well.... Only One way to find out...
Nevertheless, i'm now able to progress in this application thanks to you
Many thanks
Regards,
Ray.
Oh Well.... Only One way to find out...
Nevertheless, i'm now able to progress in this application thanks to you
Many thanks
Regards,
Ray.
Glad to help.
Cheers,
Greg
Cheers,
Greg
ASKER
Many thanks for your reply.
Quick Question (I know this may sound stupid):
KeyExchangeFormatter and KeyExchangeDeformatter come up as Not Declared
What am I missing? Import Statetments are:
Imports System.Security.Cryptograp
Imports System.Text
Imports System.IO
Any help is greatly appreciated
Kind Regards,
Ray