A
Alen Gurovic
hello john
i did not saw your messgae
Now i want to set secret key but i do not know how
need help with that code
In sub crypt encrypt text and that is ok
I don't know how to set .key and .IV property
so i want to set my secret key
in other sub decrypt i can't decrypt text
thanks again
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Call encrypt_data()
End Sub
Sub encrypt_data()
Dim stream As Stream = New FileStream("data.txt", FileMode.Open)
Dim sw As New StreamWriter("data_encrypt.txt", False)
Dim sr As New StreamReader(stream)
Dim sb As New StringBuilder
While sr.Peek <> -1
sb.Append(sr.ReadLine.ToString)
End While
sr.Close()
Dim x_message_byte As Byte() = Encoding.Default.GetBytes(sb.ToString)
Dim alg As SymmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael")
alg.KeySize = 256
alg.GenerateKey()
alg.GenerateIV()
Dim alg_crypt As ICryptoTransform = alg.CreateEncryptor()
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, alg_crypt, CryptoStreamMode.Write)
cs.Write(x_message_byte, 0, x_message_byte.Length)
cs.Close()
Dim plaintext As Byte() = ms.ToArray
Dim m As String = System.Convert.ToBase64String(plaintext)
sw.WriteLine(m)
sw.Close()
Console.Write(m)
Console.ReadLine()
Console.WriteLine()
Console.WriteLine()
Console.WriteLine()
Call decrypt()
Console.ReadLine()
End Sub
Sub decrypt()
Dim alg As SymmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael")
Dim alg_decrypt As ICryptoTransform = alg.CreateDecryptor
Dim x_bytes_len() As Byte
Dim stream As Stream = New FileStream("data_encrypt.txt", FileMode.Open)
Dim sr As New StreamReader(stream)
Dim sb As New StringBuilder
While sr.Peek <> -1
sb.Append(sr.ReadLine.ToString)
End While
sr.Close()
stream.Close()
Dim x_dexcry_data As Byte() = Encoding.Default.GetBytes(sb.ToString)
x_bytes_len = New Byte(x_dexcry_data.Length) {}
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, alg_decrypt, CryptoStreamMode.Read)
cs.Read(x_dexcry_data, 0, x_dexcry_data.Length)
cs.Close()
Dim plain() As Byte = ms.ToArray
Dim m As String = System.Convert.ToBase64String(plain)
Console.Write(m)
End Sub
End Module
i did not saw your messgae
Now i want to set secret key but i do not know how
need help with that code
In sub crypt encrypt text and that is ok
I don't know how to set .key and .IV property
so i want to set my secret key
in other sub decrypt i can't decrypt text
thanks again
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Call encrypt_data()
End Sub
Sub encrypt_data()
Dim stream As Stream = New FileStream("data.txt", FileMode.Open)
Dim sw As New StreamWriter("data_encrypt.txt", False)
Dim sr As New StreamReader(stream)
Dim sb As New StringBuilder
While sr.Peek <> -1
sb.Append(sr.ReadLine.ToString)
End While
sr.Close()
Dim x_message_byte As Byte() = Encoding.Default.GetBytes(sb.ToString)
Dim alg As SymmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael")
alg.KeySize = 256
alg.GenerateKey()
alg.GenerateIV()
Dim alg_crypt As ICryptoTransform = alg.CreateEncryptor()
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, alg_crypt, CryptoStreamMode.Write)
cs.Write(x_message_byte, 0, x_message_byte.Length)
cs.Close()
Dim plaintext As Byte() = ms.ToArray
Dim m As String = System.Convert.ToBase64String(plaintext)
sw.WriteLine(m)
sw.Close()
Console.Write(m)
Console.ReadLine()
Console.WriteLine()
Console.WriteLine()
Console.WriteLine()
Call decrypt()
Console.ReadLine()
End Sub
Sub decrypt()
Dim alg As SymmetricAlgorithm = SymmetricAlgorithm.Create("Rijndael")
Dim alg_decrypt As ICryptoTransform = alg.CreateDecryptor
Dim x_bytes_len() As Byte
Dim stream As Stream = New FileStream("data_encrypt.txt", FileMode.Open)
Dim sr As New StreamReader(stream)
Dim sb As New StringBuilder
While sr.Peek <> -1
sb.Append(sr.ReadLine.ToString)
End While
sr.Close()
stream.Close()
Dim x_dexcry_data As Byte() = Encoding.Default.GetBytes(sb.ToString)
x_bytes_len = New Byte(x_dexcry_data.Length) {}
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, alg_decrypt, CryptoStreamMode.Read)
cs.Read(x_dexcry_data, 0, x_dexcry_data.Length)
cs.Close()
Dim plain() As Byte = ms.ToArray
Dim m As String = System.Convert.ToBase64String(plain)
Console.Write(m)
End Sub
End Module