RSA Encrypt And Decrypt From File - Parameters Error

  • Thread starter Thread starter MigrationUser 1
  • Start date Start date
M

MigrationUser 1

Guest
I have working Code But I want to make two classes.

Working Code

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

Public Class RsaEnc

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

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


Module Module1

Private FILE_NAME As String = "C:\Secret\pass.txt"
Private RsaSec As RsaEnc = New RsaEnc()

Sub Main()
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 Result

Console.WriteLine("SIFRE GIRINIZ")
Console.WriteLine()
Dim text = Console.ReadLine()
Console.WriteLine()

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(" Encrypt Text : " & encryptText)

File.WriteAllBytes(FILE_NAME, cypherText)

Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Tamamlandı : " & encryptText)
Console.WriteLine()
Console.WriteLine()

Console.WriteLine("Press enter to decrypt text")
Console.ReadLine()


Result = File.ReadAllBytes(FILE_NAME)
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("Read Key : " & encoding.GetString(Result))
Console.WriteLine()
Console.WriteLine()

resultText = Result

Dim decryptedText As Byte() = RsaSec.Decrypt(resultText, rsa.ExportParameters(True), False)
Console.WriteLine(" Decrypt Text : " & encoding.GetString(decryptedText))
Console.ReadLine()

End If

End Sub

End Module


I trying two class (Encrypt,Decrypt) give a parameters error ;

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 = "PASSWORD KEY"

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 Preview : " & 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("Config Error !", "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...
 

Similar threads

P
Replies
0
Views
95
Paulywog0667_Laptop
P
C
Replies
0
Views
113
Claude du Québec
C
A
Replies
0
Views
102
AbanoubZak
A
Back
Top