For john skeet cryptography

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
 
J

Jon Skeet [C# MVP]

Alen Gurovic said:
i did not saw your messgae

Now i want to set secret key but i do not know how

Simply use the Key and IV properties.

Note that your code has another problem - you're converting the binary
data to base64, but then when you decode it, you're converting it back
to binary using Encoding.Default. You need to use
Convert.FromBase64String.

One golden rule of this kind of work is to apply the reverse operation
in the reverse order. So in your case, you've got:

Text -> Encoding.Default.GetBytes -> Encryption -> binary -> Base64

To decrypt, you need:

Base64 -> binary -> Decryption -> Encoding.Default.GetString -> Text
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top