RSA Encrypt And Decrypt From File - Parameters Error

  • Thread starter Thread starter MuzafferAnlas
  • Start date Start date
M

MuzafferAnlas

Guest
Hello, I trying RSA two classes (decrypt,encrypt) but getting a error.

Error Name : {"Array cannot be null." & vbCrLf & "Parameter name: bytes"}

Name Value Type
m_paramName "bytes" String


Encrypt Class :

Imports System.Configuration
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Rsa_Encrypt

Public Function Encrypt(ByVal data() As Byte, ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()

Try

Using rsa As New RSACryptoServiceProvider()
rsa.ImportParameters(rsakey)
Dim endData = rsa.Encrypt(data, doPadding)
Return endData
End Using

Catch ex As Exception
Console.WriteLine(ex.Message)
Return Nothing
End Try

End Function

End Class

Public Class Encrypt

Private config As Configuration
Private fileName As String = ConfigurationManager.AppSettings.Get("Location") + "\pass.txt"
Private RsaSec As Rsa_Encrypt = New Rsa_Encrypt()
Dim encoding As Encoding = New UTF8Encoding
Dim rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
Dim plainText As Byte()
Dim cypherText As Byte()

Sub Main()
If fileName <> "" Then
Dim text = "CREATED CODE"

If (Not String.IsNullOrEmpty(text)) Then
plainText = encoding.GetBytes(text)

Dim rsaParam = rsa.ExportParameters(False)

cypherText = RsaSec.Encrypt(plainText, rsa.ExportParameters(False), False)
Dim encryptText = encoding.GetString(cypherText)
Console.WriteLine("Key : " & encryptText)

Try
File.WriteAllBytes(fileName, cypherText)
Catch ex As Exception
If Not System.IO.File.Exists(fileName) Then
System.IO.File.Create(fileName).Dispose()
'My.Computer.FileSystem.DeleteFile(fileName)
End If
File.WriteAllBytes(fileName, cypherText)
End Try

Console.WriteLine()
Console.WriteLine()
End If

Else
MsgBox("Not Found Config File !", "Alert")
End If
End Sub

End Class



Decrypt Class :

Imports System.Configuration
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Rsa_Decrypt

Public Function Decrypt(ByVal data() As Byte, ByVal rsakey As RSAParameters, ByVal doPadding As Boolean) As Byte()

Dim decryptData As Byte()
Try
Using rsa As New RSACryptoServiceProvider()
rsa.ImportParameters(rsakey)
decryptData = rsa.Decrypt(data, doPadding)
Return decryptData
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
Return Nothing
End Try

End Function

End Class

Public Class Decrypt

Private config As Configuration
Private fileName As String = ConfigurationManager.AppSettings.Get("Location") + "\pass.txt"
Private RsaSec As Rsa_Decrypt = New Rsa_Decrypt()
Dim encoding As Encoding = New UTF8Encoding
Dim rsa As RSACryptoServiceProvider = New RSACryptoServiceProvider()
Dim plainText As Byte()
Dim cypherText As Byte()
Dim resultText As Byte()
Dim Convert As String
Dim Result


Sub Main()
If fileName <> "" Then
Dim rsaParam = rsa.ExportParameters(False)
Try
Result = File.ReadAllBytes(fileName)
Catch ex As Exception
If Not System.IO.File.Exists(fileName) Then
System.IO.File.Create(fileName).Dispose()
'My.Computer.FileSystem.DeleteFile(fileName)
End If
Result = File.ReadAllBytes(fileName)

End Try

Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Key : " & encoding.GetString(Result))
Console.WriteLine()
Console.WriteLine()

resultText = Result

Dim decryptedText As Byte() = RsaSec.Decrypt(resultText, rsa.ExportParameters(True), False)


Console.WriteLine(" Decrypted Text : " & encoding.GetString(decryptedText))
Console.ReadLine()

End If
End Sub

End Class



I'm waiting for your help and Thank you for your interest.

Sorry for my language errors trying to fix.

Continue reading...
 
Back
Top