Chilkat HOME Android™ ASP Visual Basic VB.NET C# iOS (IPhone) Objective-C C++ C Unicode C++ Unicode C MFC Delphi DLL Delphi ActiveX FoxPro Java Perl PHP Extension PHP ActiveX Python PowerShell Ruby SQL Server VBScript
Blowfish Encryption / DecryptionDownload: Chilkat .NET Assemblies C# example source code showing how to do 256-bit blowfish encryption and decryption. NOTE: This example uses the Chilkat.Crypt class, not Chilkat.Crypt2 public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
private void button1_Click(object sender, System.EventArgs e)
{
Chilkat.Crypt crypt = new Chilkat.Crypt();
crypt.UnlockComponent("UnlockCode");
// Generate some data.
String fileContents = "Test\n";
int i;
for (i=0; i<100; i++)
{
fileContents = fileContents + "This is a test 1234\n";
}
byte [] byteData = StrToByteArray(fileContents);
textBox1.Text = "Data Length = " + Convert.ToString(byteData.Length);
// Set our algorithm and key-length
crypt.SetAlgorithmBlowfish();
crypt.KeyLength = 256; // 256-bit encryption.
// Set our password.
crypt.SetSecretKeyViaPassPhrase("myPassword");
// Encrypt the data.
byte [] encryptedData = crypt.Encrypt(byteData);
textBox1.Text = textBox1.Text + "\r\nEncrypted Length = " + Convert.ToString(encryptedData.Length);
// Write the encrypted binary file.
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(
System.IO.File.Create("encrypted.dat"));
bw.Write(encryptedData);
bw.Close();
// Now read the binary file
System.IO.FileInfo fInfo = new System.IO.FileInfo("encrypted.dat");
System.IO.BinaryReader br = new System.IO.BinaryReader(
System.IO.File.OpenRead("encrypted.dat"));
byte [] eBytes = br.ReadBytes((int)fInfo.Length);
br.Close();
// Decrypt the bytes.
byte [] dBytes = crypt.Decrypt(eBytes);
string str = "";
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
str = enc.GetString(dBytes);
textBox1.Text = textBox1.Text + "\r\n" + str;
}
}
|
© 2000-2013 Chilkat Software, Inc. All Rights Reserved.