對於ASPX網頁上暫存資料方面,ServerSide最常用多數都是用Session,ClientSide應該都是ViewState吧,前者應該沒有太多保安問題,但後者的ViewState其實要Decode根本不難,所以加密功夫其實很重要的。近期工作上經常要使用 jQuery + Web Service處理資料,有些敏感資料會經JSON傳回Browser,又或者經Hidden Field傳回Server,有些情況是很難避免的 (特別是上司要求…),我看過有些懶人只用Base64轉碼就當是加密其實真的很危險。

.NET Framework其實本身在System.Security.Cryptography NameSpace下已經提供了大量加密方式,在MSDN看過後,取了幾種針對String加密的,做了少少資料搜集有關 3DES / DES / RSAAES (Rijndael) 的比較,其實4種方法同樣都需要一項Key才可以進行正常解密,所以即使知道你網頁中加密後的字串,而不知道你所設定的Key都不可能正常解密的。

論安全性,粗略評估後的排行應該是 AES (Rijndael) > AES > 3DES > RSA / DES。

至於使用方面,Client-Side可以使用以下的Javascript 解密AES:
http://www.movable-type.co.uk/scripts/aes.html

Server-Side方面,其實MSDN上已有很清晰的Sample,以下就是使用AES (Rijndael)方式對String進行encrypt和decrypt的例子。

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

class RijndaelSample
{

    static void Main()
    {
        try
        {
            // Create a new Rijndael object to generate a key
            // and initialization vector (IV).
            Rijndael RijndaelAlg = Rijndael.Create();

            // Create a string to encrypt.
            string sData = "Here is some data to encrypt.";
            string FileName = "CText.txt";

            // Encrypt text to a file using the file name, key, and IV.
            EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV);

            // Decrypt the text from a file using the file name, key, and IV.
            string Final = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV);

            // Display the decrypted string to the console.
            Console.WriteLine(Final);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.ReadLine();
    }

    public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
    {
        try
        {
            // Create or open the specified file.
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

            // Create a new Rijndael object.
            Rijndael RijndaelAlg = Rijndael.Create();

            // Create a CryptoStream using the FileStream
            // and the passed key and initialization vector (IV).
            CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateEncryptor(Key, IV), CryptoStreamMode.Write);

            // Create a StreamWriter using the CryptoStream.
            StreamWriter sWriter = new StreamWriter(cStream);

            try
            {
                // Write the data to the stream
                // to encrypt it.
                sWriter.WriteLine(Data);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {
                // Close the streams and
                // close the file.
                sWriter.Close();
                cStream.Close();
                fStream.Close();
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
        }

    }

    public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
    {
        try
        {
            // Create or open the specified file.
            FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);

            // Create a new Rijndael object.
            Rijndael RijndaelAlg = Rijndael.Create();

            // Create a CryptoStream using the FileStream
            // and the passed key and initialization vector (IV).
            CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateDecryptor(Key, IV), CryptoStreamMode.Read);

            // Create a StreamReader using the CryptoStream.
            StreamReader sReader = new StreamReader(cStream);

            string val = null;

            try
            {
                // Read the data from the stream
                // to decrypt it.
                val = sReader.ReadLine();

            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: {0}", e.Message);
            }
            finally
            {
                // Close the streams and
                // close the file.
                sReader.Close();
                cStream.Close();
                fStream.Close();
            }

            // Return the string.
            return val;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
    }
}
收藏或分享這篇文章:
  • email
  • Facebook
  • Twitter
  • del.icio.us
  • Google Bookmarks
  • Digg

更多相關文章:

    C# 認識Enumeration – enum Keyword
    Enumeration(列舉型別)不是常用的類型,不過有時候做對比的程序,Enum就起了作用。自己寫程式都是比較少用Enum,所以溫故知新,在此講解一下Enum的用法。 找一個例子,假如你替一間遊戲店寫一個系統,需要做以下功能: – 由產品名稱找到售價 –...
    .NET的AndAlso與OrElse
    今天看看一個比較Fresh的同事做的VB Project,看到一些(IF中IF),我問他為什麼不用AndAlso,他似乎不知道是什麼,但他寫的C# Code卻有”&&”,最後我告訴他,其實”&&”是AndAlso,不是And,他就:( 哦~~~~!!!) 其實除了AndAlso之外,還有OrElse,自.NET第一代已經有。不過的確我看到很多討論區,一些學生問VB功課,他們都是用And/或者Or,可能教他們的老師都是受VB6影響。一些比較老的Code...
    PHP 連接不到MSSQL Server 2005解決方法
    這段時間的工作都是要寫PHP,我選擇了用NetBeans + PHP Plugin還不錯.而且或者我之前寫C# +...

太差了!沒有什麼特別。值得一看。精彩!經典! (暫時未有評分)
Loading ... Loading ...