星期三, 9月 03, 2014

[C#] 字串加解密

MVA Twenty C# Questions Explained - [11 Explain how to encrypt/decrypt a string in .NET]

利用 TripleDES 來對字串進行加解密,這篇就單純紀錄課程中的 Code 而已
namespace MVATwentyQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            string plainText = "This is plain text that we will encrypt";
            string password = "P@$$w0rd";
            string depassword = "doesn't work";

            Console.WriteLine(plainText);
            Console.WriteLine();

            // create a new instance of our encryption class passing in the password as the key
            DESEncrypt testEncrypt = new DESEncrypt();

            string encText = testEncrypt.EncryptString(plainText, password);
            Console.WriteLine("****** Encrypted text ******");
            Console.WriteLine(encText);
            Console.WriteLine();

            Console.WriteLine("****** Decrypted text ******");
            //string plain = testEncrypt.DecryptString(encText, password);
            string plain = testEncrypt.DecryptString(encText, depassword);
            Console.WriteLine(plain);
            Console.WriteLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace MVATwentyQuestions
{
    class DESEncrypt
    {
        static TripleDES CreateDES(string key)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            TripleDES des = new TripleDESCryptoServiceProvider();
            des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
            des.IV = new byte[des.BlockSize / 8];
            return des;
        } 

        public string EncryptString(string plainText, string password)
        {
            // first we convert the plain text into a byte array
            byte[] plainTextBytes = Encoding.Unicode.GetBytes(plainText);

            // use a memory stream to hold the bytes
            MemoryStream myStream = new MemoryStream();

            // create the key and initialization vector using the password
            TripleDES des = CreateDES(password);

            // create the encoder that will write to the memory stream
            CryptoStream cryptStream = new CryptoStream(myStream, des.CreateEncryptor(), CryptoStreamMode.Write);

            // we now use the crypto stream to write our byte array to the memory stream
            cryptStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptStream.FlushFinalBlock();

            // change the encrypted stream to a printable version of our encrypted string
            return Convert.ToBase64String(myStream.ToArray());
        }

        public string DecryptString(string encryptedText, string password)
        {
            // convert our encrypted string to a byte array
            byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);

            // create a memory stream to hold the bytes
            MemoryStream myStream = new MemoryStream();

            // create the key and initialization vector using the password
            TripleDES des = CreateDES(password);

            // create our decoder to write to the stream
            CryptoStream decryptStream = new CryptoStream(myStream, des.CreateDecryptor(), CryptoStreamMode.Write);

            // we now use the crypto stream to the byte array
            decryptStream.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);
            decryptStream.FlushFinalBlock();

            // convert our stream to a string value
            return Encoding.Unicode.GetString(myStream.ToArray());
        }
    }
}

沒有留言:

張貼留言