using System; using System.Text; using System.Security.Cryptography; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var encoding = new UTF8Encoding(); byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 }; byte[] iv = { 8, 7, 6, 5, 4, 3, 2, 1 }; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Key = key; des.IV = iv; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.PKCS7; byte[] plain_string = encoding.GetBytes("さつまいもキャラメル"); byte[] encrypted_string; byte[] decrypted_string; encrypted_string = des.CreateEncryptor(key, iv).TransformFinalBlock(plain_string, 0, plain_string.Length); Console.Write("encrypted string:" + encoding.GetString(encrypted_string) + "\n"); decrypted_string = des.CreateDecryptor(key, iv).TransformFinalBlock(encrypted_string, 0, encrypted_string.Length); Console.Write("decrypted string:" + encoding.GetString(decrypted_string)); } } }
結果
C:\...\Debug>tripledes encrypted string:9??O?M?=i1???#x???p???pм%o? decrypted string:さつまいもキャラメル
参照したURL:
http://www.codeproject.com/KB/vb/VB_NET_TripleDES.aspx