文件加密解密 URl参数加密解密

Sliverlight 加密解密 

public static class Encryption
    {
        #region silverlight数据加密
        /**/
        ///


        /// 加密数据
        ///

        /// 加密前的字符串
        /// 加密后的字符串
        public static string Encrypt(string input)
        {
            //定义的字符串
            string saltValue = "saltValue";
            // 密码值
            string pwdValue = "pwdValue";

            byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
            byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);

            // AesManaged - 高级加密标准(AES) 对称算法的管理类
            System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();

            // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
            // 通过 密码 和 salt 派生密钥
            System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);

            /**/
            /*
         * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
         * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
         * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
         * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
         * AesManaged.Key - 对称算法的密钥
         * AesManaged.IV - 对称算法的密钥大小
         * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
         */
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
            System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();

            // 加密后的输出流
            System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();

            // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
            System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
                (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);

            // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
            encryptor.Write(data, 0, data.Length);
            encryptor.Close();

            // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
            string encryptedString = Convert.ToBase64String(encryptStream.ToArray());

            return encryptedString;
        }
        #endregion

        #region silverlight密码解密
        /**/
        ///


        /// 解密数据
        ///

        /// 加密后的字符串
        /// 加密前的字符串
        public static string Decrypt(string input)
        {
            // 盐值(与加密时设置的值一致)
            string saltValue = "saltValue";
            // 密码值(与加密时设置的值一致)
            string pwdValue = "pwdValue";

            byte[] encryptBytes = Convert.FromBase64String(input);
            byte[] salt = System.Text.Encoding.UTF8.GetBytes(saltValue);

            System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();

            System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
            System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();

            // 解密后的输出流
            System.IO.MemoryStream decryptStream = new System.IO.MemoryStream();

            // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
            System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
                decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);

            // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Close();

            // 将解密后所得到的流转换为字符串
            byte[] decryptBytes = decryptStream.ToArray();
            string decryptedString = System.Text.UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);

            return decryptedString;
        }
        #endregion

    }

 

 

   (2)

 ///


    /// 加密解密辅助类
    ///

    public class EncrptHelp
    {
        ///
        /// 加密
        ///

        ///
        ///
        public static string SimpleEncrpt(string sText)
        {
            byte[] btBuffer = System.Text.Encoding.UTF8.GetBytes(sText);
            for (int i = 0; i < btBuffer.Length; i++)
            {
                btBuffer[i] = SwithByte(btBuffer[i]);
            }
            return System.Convert.ToBase64String(btBuffer);
        }

       
        private static byte SwithByte(byte bSouce)
        {
            int bResult = bSouce;
            int bTemp1, bTemp2;
            int nStep = 6;
            byte bMark;
            int nCount = 0;
            int nWhileRange = 2;

            for (int i = 0; i < 2; i++)
            {
                while (nCount < nWhileRange)
                {
                    bTemp1 = bResult & m_aryByteTable[nCount];
                    bTemp1 = bTemp1 << nStep;
                    bTemp2 = bResult & m_aryByteTable[nCount + nStep];
                    bTemp2 = bTemp2 >> nStep;
                    if (bTemp1 != 0)
                        bResult = bResult | bTemp1;
                    else
                    {
                        bMark = m_aryByteTableMark[nCount + nStep];
                        bResult = bResult & bMark | bTemp1;
                    }

                    if (bTemp2 != 0)
                        bResult = bResult | bTemp2;
                    else
                    {
                        bMark = m_aryByteTableMark[nCount];
                        bResult = bResult & bMark | bTemp1;
                    }
                    nCount++;
                }
                nStep = 2;
                nWhileRange = 4;
            }
            return (byte)bResult;
        }
        private readonly static byte[] m_aryByteTable = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
        private readonly static byte[] m_aryByteTableMark = { 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F };

        //        URL中的特殊字符
        //有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了。编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII(16进制)码值。例如 空格的编码值是"%20"。

        //1. +  URL 中+号表示空格 %2B
        //2. 空格 URL中的空格可以用+号或者编码 %20
        //3. /  分隔目录和子目录 %2F 
        //4. ?  分隔实际的 URL 和参数 %3F 
        //5. % 指定特殊字符 %25 
        //6. # 表示书签 %23 
        //7. & URL 中指定的参数间的分隔符 %26 
        //8. = URL 中指定参数的值 %3D
        public static string UrlEncode(string sText)
        {
            return sText.Replace("+", "%2B").
                Replace(" ", "%20").
                Replace("/", "%2F").
                Replace("?", "%3F").
                Replace("%", "%25").
                Replace("#", "%23").
                Replace("&", "%26").
                Replace("=", "%3D");
        }

 

你可能感兴趣的:(文件加密解密 URl参数加密解密)