public static byte[] DES_Enc(string str, string key, string vit)
 {
     //判断向量是否为空,进行默认赋值;
     if (string.IsNullOrEmpty(key)) { key = "KCCT"; }
     //判断向量是否为空,进行默认赋值;
     if (string.IsNullOrEmpty(vit)) { vit = "MNSN"; }
     try
     {
         //实例化加解密类的对象;
         using (var descsp = new System.Security.Cryptography.DESCryptoServiceProvider())
         {
             //定义字节数组,用来存储要加密的字符串;
             var data = System.Text.Encoding.UTF8.GetBytes(str);
             //实例化内存流对象;
             using (var mStream = new System.IO.MemoryStream())
             {
                 //使用内存流实例化加密流对象;
                 using (var cStream = new System.Security.Cryptography.CryptoStream(mStream, descsp.CreateEncryptor(System.Text.Encoding.Unicode.GetBytes(key), System.Text.Encoding.Unicode.GetBytes(vit)), System.Security.Cryptography.CryptoStreamMode.Write))
                 {
                     //向加密流中写入数据;
                     cStream.Write(data, 0, data.Length);
                     //释放加密流;
                     cStream.FlushFinalBlock();
                     //返回加密后的字符串;
                     return mStream.ToArray();
                 }
             }
         }
     }
     catch { return null; }
 }
Exemplo n.º 2
0
Arquivo: Des.cs Projeto: Fun33/code
        /// <summary>
        /// 加密文件
        /// </summary>
        public void EncryptFile(string Value, string OuputFileName)
        {
            try
            {
                // Must be 64 bits, 8 bytes.
                // Distribute this key to the user who will decrypt this file.
                //Get the Key for the file to Encrypt.
                string sKey = GenerateKey();

                byte[] b = ASCIIEncoding.ASCII.GetBytes(Value);

                System.IO.FileStream fsEncrypted = new System.IO.FileStream(OuputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV  = ASCIIEncoding.ASCII.GetBytes(sKey);

                System.Security.Cryptography.ICryptoTransform desencrypt = DES.CreateEncryptor();
                System.Security.Cryptography.CryptoStream     cs         = new System.Security.Cryptography.CryptoStream(fsEncrypted, desencrypt, System.Security.Cryptography.CryptoStreamMode.Write);

                cs.Write(b, 0, b.Length);
                cs.Close();

                fsEncrypted.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
                //static Logger logger = SimpleLogger.setLogger("Common");
                #region 暗号化関係
                /// <summary>
                /// 文字列を暗号化する
                /// </summary>
                /// <param name="str">暗号化する文字列</param>
                /// <param name="key">パスワード</param>
                /// <returns>暗号化された文字列</returns>
                public static string EncryptString(string str, string key)
                {
                    //文字列をバイト型配列にする
                    byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

                    //DESCryptoServiceProviderオブジェクトの作成
                    System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

                    //共有キーと初期化ベクタを決定
                    //パスワードをバイト配列にする
                    byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);
                    //共有キーと初期化ベクタを設定
                    des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
                    des.IV  = ResizeBytesArray(bytesKey, des.IV.Length);

                    //暗号化されたデータを書き出すためのMemoryStream
                    MemoryStream msOut = new System.IO.MemoryStream();

                    //DES暗号化オブジェクトの作成
                    System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor();
                    //書き込むためのCryptoStreamの作成
                    System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msOut, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Write);
                    //書き込む
                    cryptStreem.Write(bytesIn, 0, bytesIn.Length);
                    cryptStreem.FlushFinalBlock();
                    //暗号化されたデータを取得
                    byte[] bytesOut = msOut.ToArray();

                    //閉じる
                    cryptStreem.Close();
                    msOut.Close();

                    //Base64で文字列に変更して結果を返す
                    return(System.Convert.ToBase64String(bytesOut).Replace("=", ""));
                }
Exemplo n.º 4
0
        /// <summary>
        /// DES加密,向量随机生成
        /// </summary>
        /// <param name="plaintext">明文</param>
        /// <returns></returns>
        public static string DesEncrypt(string plaintext)
        {
            //随机生成8位向量
            string iv = GenerateRandom();

            byte[] rgbKey = System.Text.Encoding.ASCII.GetBytes("SolinArt");
            byte[] rgbIV  = System.Text.Encoding.ASCII.GetBytes(iv);
            byte[] _data  = System.Text.Encoding.UTF8.GetBytes(plaintext);

            System.Security.Cryptography.DESCryptoServiceProvider _des     = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.Security.Cryptography.ICryptoTransform         _encrypt = _des.CreateEncryptor(rgbKey, rgbIV);
            byte[] _result = _encrypt.TransformFinalBlock(_data, 0, _data.Length);
            _encrypt.Dispose();
            _des.Clear();

            string result = BitConverter.ToString(_result);

            result = iv + result.Replace("-", "");

            //首尾各加16位共计32位的MD5校验码
            #region 32位的MD5校验码
            //string _md5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(result, "MD5");
            string _md5 = Md5(result);
            result = string.Format("{0}{1}{2}"
                                   , _md5.Substring(0, 16)
                                   , result
                                   , _md5.Substring(16)
                                   );
            #endregion

            return(result);
        }
Exemplo n.º 5
0
                //static Logger logger = SimpleLogger.setLogger("Common");

                #region �Í����֌W

                /// <summary>
                /// ��������������
                /// </summary>
                /// <param name="str">�Í������镶����</param>
                /// <param name="key">�p�X���[�h</param>
                /// <returns>�Í������ꂽ������</returns>
                public static string EncryptString(string str, string key)
                {
                    //�������o�C�g�^�z��ɂ���
                    byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

                    //DESCryptoServiceProvider�I�u�W�F�N�g�̍쐬
                    System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

                    //���L�L�[�Ə������x�N�^�����
                    //�p�X���[�h��o�C�g�z��ɂ���
                    byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);
                    //���L�L�[�Ə������x�N�^��ݒ�
                    des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
                    des.IV = ResizeBytesArray(bytesKey, des.IV.Length);

                    //�Í������ꂽ�f�[�^������o�����߂�MemoryStream
                    MemoryStream msOut = new System.IO.MemoryStream();
                    //DES�Í����I�u�W�F�N�g�̍쐬
                    System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateEncryptor();
                    //�������ނ��߂�CryptoStream�̍쐬
                    System.Security.Cryptography.CryptoStream cryptStreem = new System.Security.Cryptography.CryptoStream(msOut, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Write);
                    //��������
                    cryptStreem.Write(bytesIn, 0, bytesIn.Length);
                    cryptStreem.FlushFinalBlock();
                    //�Í������ꂽ�f�[�^��擾
                    byte[] bytesOut = msOut.ToArray();

                    //�‚���
                    cryptStreem.Close();
                    msOut.Close();

                    //Base64�ŕ�����ɕύX���Č��ʂ�Ԃ�
                    return System.Convert.ToBase64String(bytesOut).Replace("=", "");
                }
Exemplo n.º 6
0
        public static byte[] E1(byte[] data)
        {
            var stream = new MemoryStream();
            var des    = new System.Security.Cryptography.DESCryptoServiceProvider();

            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            var cs = new System.Security.Cryptography.CryptoStream(stream, des.CreateEncryptor(ES.Key, ES.IV), System.Security.Cryptography.CryptoStreamMode.Write);

            cs.Write(data, 0, (int)data.Length);
            cs.FlushFinalBlock();
            return(stream.ToArray());
        }
Exemplo n.º 7
0
        private static readonly byte[] IV  = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }; // 默认密钥向量
        #endregion

        #region DES加密
        public static string Encrypt(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return("");
            }
            byte[] byteArray = Encoding.UTF8.GetBytes(text);
            using (System.Security.Cryptography.DESCryptoServiceProvider desCSP = new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                {
                    System.Security.Cryptography.ICryptoTransform iCryptoTransform = desCSP.CreateEncryptor(KEY, IV);
                    using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, iCryptoTransform, System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(byteArray, 0, byteArray.Length);
                        cryptoStream.FlushFinalBlock();
                    }
                    return(BitConverter.ToString(memoryStream.ToArray()).Replace("-", ""));
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 使用DES加密
        /// </summary>
        /// <param name="originalValue">待加密的字符串</param>
        /// <param name="key">密钥(最大长度8)</param>
        /// <param name="IV">初始化向量(最大长度8)</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string originalValue, string key)
        {
            try
            {
                //将key和IV处理成8个字符
                key += "12345678";
                string IV = key + "12345678";
                key = key.Substring(0, 8);
                IV  = IV.Substring(0, 8);

                System.Security.Cryptography.SymmetricAlgorithm sa;
                System.Security.Cryptography.ICryptoTransform   ct;
                System.IO.MemoryStream ms;
                System.Security.Cryptography.CryptoStream cs;
                byte[] byt;

                sa     = new System.Security.Cryptography.DESCryptoServiceProvider();
                sa.Key = System.Text.Encoding.UTF8.GetBytes(key);
                sa.IV  = System.Text.Encoding.UTF8.GetBytes(IV);
                ct     = sa.CreateEncryptor();

                byt = System.Text.Encoding.UTF8.GetBytes(originalValue);

                ms = new System.IO.MemoryStream();
                cs = new System.Security.Cryptography.CryptoStream(ms, ct, System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();

                cs.Close();

                return(Convert.ToBase64String(ms.ToArray()));
            }
            catch
            {
            }

            return("");
        }
Exemplo n.º 9
0
        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="toEncryptString"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncryptString)
        {
            string r = string.Empty;

            if (toEncryptString.IsNullOrWhiteSpace())
            {
                return(string.Empty);
            }

            System.Security.Cryptography.DESCryptoServiceProvider provider = initProvider();

            // System.Security.Cryptography.ICryptoTransform cryptoTransform = provider.CreateEncryptor(sRgbKey, sRgbIV); // 为了兼容其他语言的Des加密算法 不使用 sRgbIV
            System.Security.Cryptography.ICryptoTransform cryptoTransform = provider.CreateEncryptor();

            byte[] bytes = Encoding.UTF8.GetBytes(toEncryptString);

            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
                using
                (
                    System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream
                                                                             (
                        stream: memoryStream,
                        transform: cryptoTransform,
                        mode: System.Security.Cryptography.CryptoStreamMode.Write
                                                                             )
                )
                {
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    r = Convert.ToBase64String(memoryStream.ToArray());
                }
            }

            return(r);
        }
Exemplo n.º 10
0
 //EncryptFile
 public static void EncryptFile(string inputfilename, string outputfilename, string key)
 {
     try
     {
         System.IO.FileStream filestream_input     = new System.IO.FileStream(inputfilename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
         System.IO.FileStream filestream_encrypted = new System.IO.FileStream(outputfilename, System.IO.FileMode.Create, System.IO.FileAccess.Write);
         System.Security.Cryptography.DESCryptoServiceProvider desCtoSP = new System.Security.Cryptography.DESCryptoServiceProvider();
         desCtoSP.Key = ASCIIEncoding.ASCII.GetBytes(key);
         desCtoSP.IV  = ASCIIEncoding.ASCII.GetBytes(key);
         System.Security.Cryptography.ICryptoTransform ICtoTf = desCtoSP.CreateEncryptor();
         System.Security.Cryptography.CryptoStream     CtoS   = new System.Security.Cryptography.CryptoStream(filestream_encrypted, ICtoTf, System.Security.Cryptography.CryptoStreamMode.Write);
         byte[] bytearrayinput = new byte[filestream_input.Length];
         filestream_input.Read(bytearrayinput, 0, bytearrayinput.Length);
         CtoS.Write(bytearrayinput, 0, bytearrayinput.Length);
         CtoS.Close();
         filestream_input.Close();
         System.IO.File.Delete(inputfilename);
         filestream_encrypted.Close();
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show("Connect error 008: " + ex.Message, "Notifications", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// DES加密字符串
 /// </summary>
 /// <param name="encryptString">待加密的字符串</param>
 /// <param name="encryptKey">加密密钥,要求为8位</param>
 /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
 public string EncryptDes(string encryptString, string encryptKey)
 {
     try
     {
         byte[] rgbKey         = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
         byte[] rgbIV          = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
         byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(encryptString);
         System.Security.Cryptography.DESCryptoServiceProvider dCSP = new System.Security.Cryptography.DESCryptoServiceProvider();
         System.IO.MemoryStream mStream = new System.IO.MemoryStream();
         System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write);
         cStream.Write(inputByteArray, 0, inputByteArray.Length);
         cStream.FlushFinalBlock();
         return(Convert.ToBase64String(mStream.ToArray()));
     }
     catch
     {
         return(encryptString);
     }
 }
Exemplo n.º 12
0
        public string Cifrar(string Cadena)
        {
            byte[] PlainText;
            PlainText = Encoding.ASCII.GetBytes(Cadena);

            MemoryStream memdata = new MemoryStream();

            System.Security.Cryptography.DESCryptoServiceProvider DES          = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.Security.Cryptography.CryptoStream             cryptostream = new System.Security.Cryptography.CryptoStream(memdata,
                                                                                                                               DES.CreateEncryptor(Encoding.ASCII.GetBytes(Default8Key), Encoding.ASCII.GetBytes(Default8VI)),
                                                                                                                               System.Security.Cryptography.CryptoStreamMode.Write);

            cryptostream.Write(PlainText, 0, PlainText.Length);
            cryptostream.FlushFinalBlock();
            cryptostream.Close();

            return(Convert.ToBase64String(memdata.ToArray()));
        }
        public static string Encrypt(string data, string seckey)
        {
            byte[] byKey = System.Text.Encoding.ASCII.GetBytes(seckey);
            byte[] by_IV = System.Text.Encoding.ASCII.GetBytes(seckey);
            System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            int i = cryptoProvider.KeySize;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cst = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, by_IV), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(cst);
            sw.Write(data);
            sw.Flush();
            cst.FlushFinalBlock();
            sw.Flush();
            return(System.Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length));
        }
Exemplo n.º 14
0
        /// <summary>
        /// 字符串加密
        /// </summary>
        /// <param name="strText">字符串</param>
        /// <param name="strEncrKey">密钥8位数字或字母</param>
        /// <returns></returns>
        public string DesEncryptString(string strText, string strEncrKey)
        {
            byte[] rgbKey = null;
            string SIv    = "inputvec";

            try
            {
                rgbKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, strEncrKey.Length));
                System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
                provider.Key  = rgbKey;
                provider.IV   = System.Text.Encoding.UTF8.GetBytes(SIv);
                provider.Mode = System.Security.Cryptography.CipherMode.ECB;

                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strText);
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream stream2 = new System.Security.Cryptography.CryptoStream(stream, provider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                return(Convert.ToBase64String(stream.ToArray()));
            }
            catch (Exception exception)
            {
                //return ("error:" + exception.Message + "\r");
                throw exception;
            }
        }
Exemplo n.º 15
0
        //-----------------------------------------------Encryption&Decryption------------------------------------------------

        private string Encrypt(string str)
        {
            string EncrptKey = "0806;[pnuLIT)lOFRUtEChnologieS";

            byte[] byKey = { };
            byte[] IV    = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8));
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[]       inputByteArray = System.Text.Encoding.UTF8.GetBytes(str);
            MemoryStream ms             = new System.IO.MemoryStream();

            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(Convert.ToBase64String(ms.ToArray()));
        }
 public WsgFileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share) : base(path, mode, access, share)
 {
     try
     {
         byte[] key = { 77, 7, 77, 7, 77, 7, 77, 7 };
         byte[] iv  = { 7, 77, 7, 77, 7, 77, 7, 77 };
         DES = new System.Security.Cryptography.DESCryptoServiceProvider();
         if (this.CanRead)
         {
             CryptoReadStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateDecryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Read);
         }
         if (this.CanWrite)
         {
             CryptoWriteStream = new System.Security.Cryptography.CryptoStream(this, DES.CreateEncryptor(key, iv), System.Security.Cryptography.CryptoStreamMode.Write);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// 字符串加密
        /// </summary>
        /// <param name="password">欲加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string PasswordEncrypt(string password)
        {
            string strCharKey     = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string strPasswordKey = "";

            for (int intIndexKey = 1; intIndexKey <= 8; intIndexKey++)
            {
                strPasswordKey += strCharKey.Substring(GetRnd(0, strCharKey.Length - 1), 1);
            }

            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] bytInput = System.Text.Encoding.Default.GetBytes(password);
            des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey);
            des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strPasswordKey);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(bytInput, 0, bytInput.Length);
            cs.FlushFinalBlock();

            System.Text.StringBuilder sbRet = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                sbRet.AppendFormat("{0:X2}", b);
            }

            return(sbRet.ToString() + strPasswordKey);
        }
Exemplo n.º 18
0
        public static string DesEncrypt(string strText, string encryptKey)
        {
            #region
            byte[] byKey = null;
            byte[] IV    = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey);
                System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
                byte[] inputByteArray     = System.Text.Encoding.UTF8.GetBytes(strText);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                strText = Convert.ToBase64String(ms.ToArray());
            }
            catch
            {
                throw;
            }
            return(strText);

            #endregion
        }
Exemplo n.º 19
0
        private static readonly String strDesKey = "gaoguanj";//加密所需8位密匙
        ///
        /// DES加密
        ///

        /// 要加密字符串
        /// 返回加密后字符串
        public static String Encrypt_DES(String str)
        {
            if (str.Trim() == "")
            {
                return("");
            }
            try
            {
                System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
                Byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(str);
                des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
                des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (Byte b in ms.ToArray())
                {
                    sb.AppendFormat("{0:X2}", b);
                }
                return(sb.ToString());
            }
            catch (System.Exception ex)
            {
                return("");
            }
        }
Exemplo n.º 20
0
    public static string Encrypt(string toEncrypt, string key)
    {
        var des = new System.Security.Cryptography.DESCryptoServiceProvider();
        var ms = new System.IO.MemoryStream();

        VerifyKey(ref key);

        des.Key = HashKey(key, des.KeySize / 8);
        des.IV = HashKey(key, des.KeySize / 8);
        byte[] inputBytes = Encoding.UTF8.GetBytes(toEncrypt);

        var cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        cs.Write(inputBytes, 0, inputBytes.Length);
        cs.FlushFinalBlock();

        return HttpServerUtility.UrlTokenEncode(ms.ToArray());
    }
Exemplo n.º 21
0
 /// <summary>
 /// 取得加密
 /// </summary>
 /// <param name="encryptString"></param>
 /// <returns></returns>
 public static string Encrypt(string encryptString)
 {
     try
     {
         Des    des            = new Des();
         byte[] rgbKey         = Encoding.UTF8.GetBytes(des.encryptKey.Substring(0, 8));
         byte[] rgbIV          = Encoding.UTF8.GetBytes(des.encryptIV.Substring(0, 8));
         byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
         System.Security.Cryptography.DESCryptoServiceProvider dCSP = new System.Security.Cryptography.DESCryptoServiceProvider();
         System.IO.MemoryStream mStream = new System.IO.MemoryStream();
         System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write);
         cStream.Write(inputByteArray, 0, inputByteArray.Length);
         cStream.FlushFinalBlock();
         return(Convert.ToBase64String(mStream.ToArray()));
     }
     catch (Exception)
     {
         return(encryptString);
     }
 }
Exemplo n.º 22
0
        /// <summary>
        /// 根据密钥进行字符串加密.
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>机密后的字符串</returns>
        public static string EncryptString(string str, string key)
        {
            try
            {
                //根据UTF8编码规则取得二进制流..
                byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

                //访问数据加密标准 (DES) 算法的加密服务提供程序.
                System.Security.Cryptography.DESCryptoServiceProvider des =
                    new System.Security.Cryptography.DESCryptoServiceProvider();

                //对密钥进行编码.
                byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);

                //初始化机密适配器..
                des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
                des.IV = ResizeBytesArray(bytesKey, des.IV.Length);

                System.IO.MemoryStream msOut = new System.IO.MemoryStream();

                //基本的加密转换运算.
                System.Security.Cryptography.ICryptoTransform desdecrypt =
                    des.CreateEncryptor();

                //定义将数据流链接到加密转换的流.
                System.Security.Cryptography.CryptoStream cryptStreem =
                    new System.Security.Cryptography.CryptoStream(msOut,
                    desdecrypt,
                    System.Security.Cryptography.CryptoStreamMode.Write);

                //二进制流输出.
                cryptStreem.Write(bytesIn, 0, bytesIn.Length);
                cryptStreem.FlushFinalBlock();

                //取得加密后的二进制流取得.
                byte[] bytesOut = msOut.ToArray();

                //关闭流.
                cryptStreem.Close();
                msOut.Close();

                //生成由以 64 为基的二进制数组.
                return System.Convert.ToBase64String(bytesOut);
            }
            catch (Exception e)
            {
                throw (e);
                //				return "";
            }
        }
Exemplo n.º 23
0
 public static string Encrypt_Des(this object value, string desKey, string desIV)
 {
     string originalString = value as string;
         byte[] Key = GetBytes(desKey);
         byte[] IV = GetBytes(desIV);
         if (String.IsNullOrEmpty(originalString))
             throw new ArgumentNullException("plainText");
         if (Key == null || Key.Length <= 0)
             throw new ArgumentNullException("Key");
         if (IV == null || IV.Length <= 0)
             throw new ArgumentNullException("IV");
         System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
         MemoryStream memoryStream = new MemoryStream();
         System.Security.Cryptography.CryptoStream cryptoStream
             = new System.Security.Cryptography.CryptoStream(memoryStream,
                                                             cryptoProvider.CreateEncryptor(Key, IV),
                                                             System.Security.Cryptography.CryptoStreamMode.Write);
         StreamWriter writer = new StreamWriter(cryptoStream);
         writer.Write(originalString);
         writer.Flush();
         cryptoStream.FlushFinalBlock();
         writer.Flush();
         return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
 }
Exemplo n.º 24
0
        /// <summary>   
        /// 加密数据   
        /// </summary>   
        /// <param name="Text"></param>   
        /// <param name="sKey"></param>   
        /// <returns></returns>   
        private static string Encrypt(string Text, string sKey)
        {
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();

            byte[] inputByteArray;

            inputByteArray = Encoding.Default.GetBytes(Text);

            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);

            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            foreach (byte b in ms.ToArray())
            {

                ret.AppendFormat("{0:X2}", b);

            }
            return ret.ToString();
        }
Exemplo n.º 25
0
        public static string Encrypt(string txt, string sKey)
        {
            System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] inputByteArr = Encoding.Default.GetBytes(txt);
#pragma warning disable CS0618 // 'FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)' is obsolete: 'The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.'
            dESCryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
#pragma warning restore CS0618 // 'FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)' is obsolete: 'The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.'
#pragma warning disable CS0618 // 'FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)' is obsolete: 'The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.'
            dESCryptoServiceProvider.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
#pragma warning restore CS0618 // 'FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)' is obsolete: 'The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.'
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            //加密器
            System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cryptoStream.Write(inputByteArr, 0, inputByteArr.Length);
            cryptoStream.FlushFinalBlock();
            StringBuilder strBuilder = new StringBuilder();
            foreach (byte b in memoryStream.ToArray())
            {
                strBuilder.AppendFormat("{0:X2}", b);
            }
            return(strBuilder.ToString());
        }
Exemplo n.º 26
0
        public static string EncryptStatic(string encryptString)
        {
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            string result;

            try
            {
                byte[] bytes  = System.Text.Encoding.UTF8.GetBytes(SimpleSecurityHelper.KeyValue.Substring(0, 8));
                byte[] keys   = SimpleSecurityHelper.Keys;
                byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(encryptString);
                System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.Security.Cryptography.CryptoStream             cryptoStream             = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write);
                cryptoStream.Write(bytes2, 0, bytes2.Length);
                cryptoStream.FlushFinalBlock();
                System.Text.Encoding uTF = System.Text.Encoding.UTF8;
                result = System.Convert.ToBase64String(uTF.GetBytes(System.Convert.ToBase64String(memoryStream.ToArray())));
            }
            catch
            {
                result = "Encrypt Failed!";
            }
            finally
            {
                memoryStream.Close();
            }
            return(result);
        }
Exemplo n.º 27
0
 public string Encrypt(string stringToEncrypt, string encryptionKey)
 {
     try
     {
         _key = System.Text.Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
         using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
         {
             byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt);
             using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
             {
                 using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(_key, _iv), System.Security.Cryptography.CryptoStreamMode.Write))
                 {
                     cs.Write(inputByteArray, 0, inputByteArray.Length);
                     cs.FlushFinalBlock();
                     return(Convert.ToBase64String(ms.ToArray()));
                 }
             }
         }
     } catch
     {
         return("");
     }
 }
Exemplo n.º 28
0
        /// <summary>
        /// 文字列を暗号化する
        /// </summary>
        /// <param name="str">暗号化する文字列</param>
        /// <param name="key">パスワード</param>
        /// <returns>暗号化された文字列</returns>
        public static string EncryptString(string str, string key)
        {
            // 文字列をバイト型配列にする
            var bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

            // DESCryptoServiceProviderオブジェクトの作成
            var des = new System.Security.Cryptography.DESCryptoServiceProvider();

            // 共有キーと初期化ベクタを決定

            // パスワードをバイト配列にする
            var bytesKey = System.Text.Encoding.UTF8.GetBytes(key);

            // 共有キーと初期化ベクタを設定
            des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
            des.IV = ResizeBytesArray(bytesKey, des.IV.Length);

            // 暗号化されたデータを書き出すためのMemoryStream
            var msOut = new System.IO.MemoryStream();

            // DES暗号化オブジェクトの作成
            var desdecrypt = des.CreateEncryptor();

            // 書き込むためのCryptoStreamの作成
            var cryptStreem = new System.Security.Cryptography.CryptoStream(msOut, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Write);

            // 書き込む
            cryptStreem.Write(bytesIn, 0, bytesIn.Length);
            cryptStreem.FlushFinalBlock();

            // 暗号化されたデータを取得
            byte[] bytesOut = msOut.ToArray();

            // 閉じる
            cryptStreem.Close();
            msOut.Close();

            // Base64で文字列に変更して結果を返す
            return System.Convert.ToBase64String(bytesOut);
        }
Exemplo n.º 29
0
 /// <summary>
 /// DES加密算法
 /// </summary>
 /// <param name="Source">要加密的字符串</param>
 /// <param name="SecretKey">加密密钥(8的整数倍字节数的字符串)</param>
 /// <returns>加密后的结果字符串</returns>
 public static byte[] DES_Encode(string Source, string SecretKey)
 {
     System.Security.Cryptography.DESCryptoServiceProvider provider = new System.Security.Cryptography.DESCryptoServiceProvider();
     try
     {
         provider.Key = System.Text.Encoding.UTF8.GetBytes(SecretKey);
     }
     catch (System.ArgumentException)
     {
         byte[] buffer = new byte[0];
         return buffer;
     }
     provider.IV = System.Text.Encoding.UTF8.GetBytes(SecretKey);
     System.IO.MemoryStream stream2 = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream stream = new System.Security.Cryptography.CryptoStream(stream2, provider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     stream.Write(System.Text.Encoding.UTF8.GetBytes(Source), 0, System.Text.Encoding.UTF8.GetBytes(Source).Length);
     stream.FlushFinalBlock();
     return stream2.ToArray();
 }
Exemplo n.º 30
0
        /// <summary>
        /// 字符串加密
        /// </summary>
        /// <param name="password">欲加密的字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string PasswordEncrypt(string password, string key)
        {
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] bytInput = System.Text.Encoding.Default.GetBytes(password);
            des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(key);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream((System.IO.Stream)ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(bytInput, 0, bytInput.Length);
            cs.FlushFinalBlock();

            System.Text.StringBuilder sbRet = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                sbRet.AppendFormat("{0:X2}", b);
            }

            return(sbRet.ToString());
        }