Encrypting and Decrypting in .net
VB.NET:
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim inFileName As String = "C:\in.txt"
Dim outFileName As String = "C:\out.txt"
Dim password As String = "Joey"
Dim salt As Byte() = Encoding.Unicode.GetBytes("Salt adds to left spaces")
Dim key As New Rfc2898DeriveBytes(password, salt)
Dim rm As New RijndaelManaged()
rm.Key = key.GetBytes(rm.KeySize / 8)
rm.IV = key.GetBytes(rm.BlockSize / 8)
Dim inFile As New FileStream(inFileName, FileMode.Open, FileAccess.Read)
Dim data(inFile.Length - 1) As Byte
inFile.Read(data, 0, data.Length)
Dim encryptor As ICryptoTransform = rm.CreateEncryptor
Dim outFile As New FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim stream As New CryptoStream(outFile, encryptor, CryptoStreamMode.Write)
stream.Write(data, 0, data.Length)
stream.Close()
outFile.Close()
inFile.Close()
End Sub
End Module
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim inFileName As String = "C:\in.txt"
Dim outFileName As String = "C:\out.txt"
Dim password As String = "Joey"
Dim salt As Byte() = Encoding.Unicode.GetBytes("Salt adds to left spaces")
Dim key As New Rfc2898DeriveBytes(password, salt)
Dim rm As New RijndaelManaged()
rm.Key = key.GetBytes(rm.KeySize / 8)
rm.IV = key.GetBytes(rm.BlockSize / 8)
Dim inFile As New FileStream(inFileName, FileMode.Open, FileAccess.Read)
Dim data(inFile.Length - 1) As Byte
inFile.Read(data, 0, data.Length)
Dim encryptor As ICryptoTransform = rm.CreateEncryptor
Dim outFile As New FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write)
Dim stream As New CryptoStream(outFile, encryptor, CryptoStreamMode.Write)
stream.Write(data, 0, data.Length)
stream.Close()
outFile.Close()
inFile.Close()
End Sub
End Module
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptingAndDecryptingCS
{
class Program
{
static void Main(string[] args)
{
string inFileName = @"C:\in.txt";
string outFileName = @"C:\out.txt";
string password = "Joey";
byte[] salt = Encoding.Unicode.GetBytes("Salt adds to left spaces");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelManaged rm = new RijndaelManaged();
rm.Key = key.GetBytes(rm.KeySize / 8);
rm.IV = key.GetBytes(rm.BlockSize / 8);
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[inFile.Length];
inFile.Read(data, 0, data.Length);
ICryptoTransform encryptor = rm.CreateEncryptor();
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
CryptoStream stream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
stream.Write(data, 0, data.Length);
stream.Close();
outFile.Close();
inFile.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptingAndDecryptingCS
{
class Program
{
static void Main(string[] args)
{
string inFileName = @"C:\in.txt";
string outFileName = @"C:\out.txt";
string password = "Joey";
byte[] salt = Encoding.Unicode.GetBytes("Salt adds to left spaces");
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt);
RijndaelManaged rm = new RijndaelManaged();
rm.Key = key.GetBytes(rm.KeySize / 8);
rm.IV = key.GetBytes(rm.BlockSize / 8);
FileStream inFile = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
byte[] data = new byte[inFile.Length];
inFile.Read(data, 0, data.Length);
ICryptoTransform encryptor = rm.CreateEncryptor();
FileStream outFile = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
CryptoStream stream = new CryptoStream(outFile, encryptor, CryptoStreamMode.Write);
stream.Write(data, 0, data.Length);
stream.Close();
outFile.Close();
inFile.Close();
}
}
}