Encrypt/Decrypt a String by 256 bit

Suman

New member
Joined
Nov 28, 2007
Messages
4
Programming Experience
1-3
Hi,

I want a VB.NET code to encrypt and decrypt a string with 256 bit.
Can any one of u send me the code or link(s).

Thanks in advance.

Regards,
Suman.S
 
RE: VB.NET Encrypt/Decrypt a String with 256 bit

Hi Jhon,


Thanks for your reply.

Actually I want a code to encrypt/decrypt a given string. The link which u have sent has code for encrypt/decrypt a file.

Please send the code or link for a encrypt/decrypt a given String.

Thanks and Regards,
Suman.S
 
You have to crypt with a stream, so in memory without a file you can use a MemoryStream instead of FileStream.

Here is a quick sample: (Imports System.Security.Cryptography)
VB.NET:
Dim r As RijndaelManaged = RijndaelManaged.Create
'encrypt
Dim mem As New IO.MemoryStream
Dim crypt As New CryptoStream(mem, r.CreateEncryptor, CryptoStreamMode.Write)
Dim writer As New IO.StreamWriter(crypt)
writer.Write("hello there")
Dim encrypted() As Byte = mem.ToArray
writer.Close()

'this is base64 view of encrypted bytes:
MsgBox(Convert.ToBase64String(encrypted))

'decrypt
mem = New IO.MemoryStream(encrypted)
crypt = New CryptoStream(mem, r.CreateDecryptor, CryptoStreamMode.Read)
Dim reader As New IO.StreamReader(crypt)
MsgBox(reader.ReadLine)
reader.Close()
Remember to handle the Key and IV, they are the "key and salt", and have to be the same for encryption and decryption. New random values are generated when you create a new RijndaelManaged.
 
RE: Encrypt/Decrypt String with 256 bit

Hi jhon,

sorry to trouble you!!!

i am trying to change this code for string encryption from file encryption.
but i coudn't understand the following code. can u please wirte the comments for below code?

or else please change the following code to "encrypt a string" and send me.


VB.NET:
Dim keyFile As New FileStream("key.bin", FileMode.CreateNew)
keyFile.Write(Rijndael.Key, 0, Rijndael.Key.Length)
keyFile.Close()

encrypt data -

Dim Transform As ICryptoTransform = 
Rijndael.CreateEncryptor()
Dim outFile As New FileStream("crypt.bin", FileMode.Create)
outFile.Write(Rijndael.IV, 0, Rijndael.IV.Length)
Dim cryptStrm As New CryptoStream(outFile, Transform, 
CryptoStreamMode.Write)
Thank u so much!!!

Regards,
Suman.S
 
Last edited by a moderator:
Please review the code sample in post 4 again.
 
RE: Encrypt/Decrypt 256 bit String

Hi Jhon,

Thanks for the response.

I have seen below code for encrypting string. but it is 64 bit encryption. I need 256 bit encryption of a string.

Can you please send the code for the 256 bit String encryption.

I need it very badly!!!

Thank u so much!!!

Regards,
Suman.S
 
RijndaelManaged uses 256bit keysize by default, as you can also see in KeySize property.
I think you're confused with the Base64 view of the encrypted bytes, it is the byte array that contains the encryption of the string. Base64 is just a way to present/store a byte array as a plain string since a byte value can represent many unprintable characters.
 
Back
Top