Revision: 35658
Updated Code
at November 11, 2010 18:09 by falconzy
Updated Code
private TripleDESCryptoServiceProvider cryptdes = new TripleDESCryptoServiceProvider(); private UTF8Encoding utf8 = new UTF8Encoding(); public string EncrypToText(string textToEncrypt) { try { return Convert.ToBase64String(this.EncryptToBinary(textToEncrypt)); } catch (Exception ex) { throw new EncryptionException(ex.Message); } } public byte[] EncryptToBinary(string textToEncrypt) { try { byte[] input = this.utf8.GetBytes(textToEncrypt); return this.Transform(input, this.cryptdes.CreateEncryptor(this.key, this.iv)); } catch (Exception ex) { throw new EncryptionException(ex.Message); } } private byte[] Transform(byte[] input, ICryptoTransform CryptoTransform) { // create the necessary streams MemoryStream memStream = new MemoryStream(); CryptoStream cryptStream = new CryptoStream(memStream, CryptoTransform, CryptoStreamMode.Write); // transform the bytes as requested cryptStream.Write(input, 0, input.Length); cryptStream.FlushFinalBlock(); // Read the memory stream and convert it back into byte array memStream.Position = 0; byte[] result = memStream.ToArray(); // Clean up memStream.Close(); cryptStream.Close(); return result; }
Revision: 35657
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 11, 2010 18:07 by falconzy
Initial Code
public string EncrypToText(string textToEncrypt) { try { return Convert.ToBase64String(this.EncryptToBinary(textToEncrypt)); } catch (Exception ex) { throw new EncryptionException(ex.Message); } } public byte[] EncryptToBinary(string textToEncrypt) { try { byte[] input = this.utf8.GetBytes(textToEncrypt); return this.Transform(input, this.cryptdes.CreateEncryptor(this.key, this.iv)); } catch (Exception ex) { throw new EncryptionException(ex.Message); } } private byte[] Transform(byte[] input, ICryptoTransform CryptoTransform) { // create the necessary streams MemoryStream memStream = new MemoryStream(); CryptoStream cryptStream = new CryptoStream(memStream, CryptoTransform, CryptoStreamMode.Write); // transform the bytes as requested cryptStream.Write(input, 0, input.Length); cryptStream.FlushFinalBlock(); // Read the memory stream and convert it back into byte array memStream.Position = 0; byte[] result = memStream.ToArray(); // Clean up memStream.Close(); cryptStream.Close(); return result; }
Initial URL
Initial Description
Initial Title
System.Security.Cryptography
Initial Tags
Initial Language
C#