CreateEncryptor() private method

private CreateEncryptor ( ) : ICryptoTransform
return ICryptoTransform
コード例 #1
1
ファイル: Encryption64.cs プロジェクト: hugohcn/sge_webapp
        public static string Encrypt( string strEncryptString, string strEncryptionKey)
        {
            byte[] inputByteArray;

            try
            {
                key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);

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

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

                throw eX;
            }
        }
コード例 #2
1
ファイル: CommonFunction.cs プロジェクト: SuperRay/MoMoWeb
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">需要加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中

                //原来使用的UTF8编码,我改成Unicode编码了,不行
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

                //建立加密对象的密钥和偏移量

                //使得输入密码必须输入英文文本
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {

            }

            return "";
        }
コード例 #3
1
ファイル: CryptHelper.cs プロジェクト: MrNor/WxMp
        public static string Encode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));

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

            MemoryStream stream = new MemoryStream();

            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);

            stream2.Write(bytes, 0, bytes.Length);

            stream2.FlushFinalBlock();

            StringBuilder builder = new StringBuilder();

            foreach (byte num in stream.ToArray())
            {

                builder.AppendFormat("{0:X2}", num);

            }

            stream.Close();

            return builder.ToString();
        }
コード例 #4
0
        public string EncryptField(string fieldValue)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(fieldValue)) {
                try {
                    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

                    MemoryStream memoryStream = new MemoryStream();

                    CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(this.key, this.key), CryptoStreamMode.Write);

                    StreamWriter writer = new StreamWriter(cryptoStream);

                    writer.Write(fieldValue);

                    writer.Flush();

                    cryptoStream.FlushFinalBlock();

                    writer.Flush();

                    result = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                } catch (Exception ex) {
                    /*LogManager.GetCurrentClassLogger().Error(
                        errorMessage => errorMessage("Could not encrypt value: {0}", fieldValue), ex);*/

                    throw new ApplicationException("Could not encrypt value: " + fieldValue, ex);
                }
            }

            return result;
        }
コード例 #5
0
ファイル: Sym_DES.cs プロジェクト: RuhuiCheng/Talent
        /// <summary>
        /// Do Encry for plainString.
        /// </summary>
        /// <param name="plainString">Plain string.</param>
        /// <returns>Encrypted string as base64.</returns>
        public string Encrypt(string plainString)
        {
            // Create the file streams to handle the input and output files.
            MemoryStream fout = new MemoryStream(200);
            fout.SetLength(0);

            // Create variables to help with read and write.
            byte[] bin = System.Text.Encoding.Unicode.GetBytes(plainString);
            DES des = new DESCryptoServiceProvider();

            // des.KeySize=64;
            CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
            encStream.Write(bin, 0, bin.Length);
            encStream.FlushFinalBlock();

            fout.Flush();
            fout.Seek(0, SeekOrigin.Begin);

            // read all string
            byte[] bout = new byte[fout.Length];
            fout.Read(bout, 0, bout.Length);
            encStream.Close();
            fout.Close();

            return Convert.ToBase64String(bout, 0, bout.Length);
        }
コード例 #6
0
ファイル: DesFile.cs プロジェクト: Fun33/code
    /// <summary>
    /// 加密文件
    /// </summary>
    public static 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;
        }
    }
コード例 #7
0
ファイル: Des.cs プロジェクト: sclynton/CrazyBull
        /// <summary>
        /// DES 加密算法
        /// </summary>
        /// <param name="str">待加密字符串</param>
        /// <returns>加密后字符串</returns>
        public static string EncodeStr(string str)
        {
            try
            {
                byte[] temp = Encoding.UTF8.GetBytes(str);

                using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
                {
                    desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(temp, 0, temp.Length);

                            cs.FlushFinalBlock();
                        }

                        StringBuilder sb = new StringBuilder();

                        foreach (byte b in ms.ToArray())
                        {
                            sb.AppendFormat("{0:X2}", b);
                        }
                        return sb.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #8
0
 /// <summary>
 /// 加密字符串
 /// </summary>
 /// <param name="encryptString"></param>
 /// <returns></returns>
 public static string Encrypt(string encryptString)
 {
     System.IO.MemoryStream mStream = null;
     System.Security.Cryptography.CryptoStream cStream = null;
     try
     {
         EncryptUtil des            = new EncryptUtil();
         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();
         mStream = new System.IO.MemoryStream();
         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);
     }
     finally
     {
         if (mStream != null)
         {
             mStream.Close(); mStream.Dispose();
         }
         if (cStream != null)
         {
             cStream.Close(); cStream.Dispose();
         }
     }
 }
コード例 #9
0
 /// <summary>
 /// DES加密
 /// </summary>
 /// <param name="str"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string DESEncrypt(string str, string key)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     //把字符串放到byte数组中 
     byte[] inputByteArray = Encoding.Default.GetBytes(str);
     //建立加密对象的密钥和偏移量  
     //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
     //使得输入密码必须输入英文文本  
     des.Key = ASCIIEncoding.ASCII.GetBytes(key);
     des.IV = ASCIIEncoding.ASCII.GetBytes(key);
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     //Write  the  byte  array  into  the  crypto  stream  
     //(It  will  end  up  in  the  memory  stream)  
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
     StringBuilder ret = new StringBuilder();
     foreach (byte b in ms.ToArray())
     {
         //Format  as  hex  
         ret.AppendFormat("{0:X2}", b);
     }
     ret.ToString();
     return ret.ToString();
 }
コード例 #10
0
ファイル: Encryption.cs プロジェクト: chrisUsick/BankOfBIT
        /// <summary>
        /// encrypt a file
        /// </summary>
        /// <param name="unencryptedFileName">name of the plain text file</param>
        /// <param name="encryptedFileName">name of the encrypted file to create and write to</param>
        /// <param name="key">the key to use to encrypt the file</param>
        public static void Encrypt(string unencryptedFileName, string encryptedFileName, string key)
        {
            // create file IO streams
            FileStream openStream = new FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read);
            FileStream writeStream = new FileStream(encryptedFileName, FileMode.Create, FileAccess.Write);

            // create the crypto service provider
            DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();

            // set the key and initialization vector
            byte[] keyBytes = ASCIIEncoding.ASCII.GetBytes(key);
            crypto.Key = keyBytes;
            crypto.IV = keyBytes;

            // create the CtryptoStream object to transform plain text to encrypted and write it to disk
            ICryptoTransform transform = crypto.CreateEncryptor();
            CryptoStream cryptoStream = new CryptoStream(writeStream, transform, CryptoStreamMode.Write);
            byte[] unencryptedBytes = new byte[openStream.Length];
            openStream.Read(unencryptedBytes, 0, unencryptedBytes.Length);

            // write to disk
            cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);

            // close connections
            openStream.Close();
            cryptoStream.Close();
            writeStream.Close();
        }
コード例 #11
0
 //The function used to encrypt the text
 private static string Encrypt(string strText, string strEncrKey)
 {
     byte[] byKey =
     {
     };
     byte[] IV =
     {
         0x12,
         0x34,
         0x56,
         0x78,
         0x90,
         0xab,
         0xcd,
         0xef
     };
     try
     {
         byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8));
         System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
         byte[]       inputByteArray = Encoding.UTF8.GetBytes(strText);
         MemoryStream ms             = new MemoryStream();
         CryptoStream cs             = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         return(Convert.ToBase64String(ms.ToArray()));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
コード例 #12
0
ファイル: Security.cs プロジェクト: oisbel/Lotto
        public void EncryptFile(string sInputFilename,string sOutputFilename,string sKey)
        {
            var fsInput = new FileStream(sInputFilename,
                FileMode.Open,
                FileAccess.Read);

            var fsEncrypted = new FileStream(sOutputFilename,
                            FileMode.Create,
                            FileAccess.Write);
            var DES = new DESCryptoServiceProvider();
            
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            ICryptoTransform desencrypt = DES.CreateEncryptor();
            var cryptostream = new CryptoStream(fsEncrypted,
                                desencrypt,
                                CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();

        }
コード例 #13
0
        public static string Edes(string encryptString, string encryptKey = "VavicApp")
        {
            try
            {

                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));

                //rgbIV与rgbKey可以不一样,这里只是为了简便,读者可以自行修改

                byte[] rgbIV = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));

                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();

                MemoryStream mStream = new MemoryStream();

                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

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

                cStream.FlushFinalBlock();

                return Convert.ToBase64String(mStream.ToArray());

            }

            catch
            {

                return encryptString;

            }
        }
コード例 #14
0
ファイル: DESHelper.cs プロジェクト: JodenSoft/JodenSoft
        /// <summary>
        /// 用给定的Key进行加密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="encryptString">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string encryptString, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _Key;
            else
                key += _Key;

            if (string.IsNullOrWhiteSpace(encryptString))
                encryptString = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //Get the data back from the memory stream, and into a string
                StringBuilder ret = new StringBuilder();
                foreach (byte b in mStream.ToArray())
                {
                    //Format as hex
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
        }
コード例 #15
0
ファイル: login.cs プロジェクト: rzguo/fpNew
        /// <summary>
        /// 对流进行加密或解密
        /// </summary>
        /// <param name="inS">输入流</param>
        /// <param name="outS">输出流</param>
        /// <param name="desKey">密钥</param>
        /// <param name="desIV">向量</param>
        /// <param name="isEncrypt">是否加密</param>
        /// <returns>成功返回true,失败返回false</returns>
        private static bool cryptIt(Stream inS, Stream outS, byte[] desKey, byte[] desIV, bool isEncrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                ICryptoTransform mode = null;
                if (isEncrypt)
                    mode = des.CreateEncryptor(desKey, desIV);
                else
                    mode = des.CreateDecryptor(desKey, desIV);
                CryptoStream cs = new CryptoStream(inS, mode, CryptoStreamMode.Read);

                byte[] bin = new byte[100];
                int len;
                while ((len = cs.Read(bin, 0, bin.Length)) != 0)
                {
                    outS.Write(bin, 0, len);
                }

                //流位置重置
                inS.Position = 0;
                outS.Position = 0;
                return true;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "错误");
                return false;
            }
        }
コード例 #16
0
        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="key">The key that will be used to encrypt the data</param>
        /// <param name="data">The string you wish to encrypt</param>
        /// <returns>Encrypted data</returns>
        public static string Encrypt(string key, string data)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(data))
            {
                byte[] m_Key = new byte[8];
                byte[] m_IV = new byte[8];

                InitKey(key, ref m_Key, ref m_IV);

                DESCryptoServiceProvider csprov = new DESCryptoServiceProvider();
                MemoryStream memstream = new MemoryStream();
                CryptoStream crstream = new CryptoStream(memstream, csprov.CreateEncryptor(m_Key, m_IV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(crstream);

                sw.Write(data);
                sw.Flush();
                crstream.FlushFinalBlock();
                memstream.Flush();

                result = Convert.ToBase64String(memstream.GetBuffer(), 0, Convert.ToInt32(memstream.Length));

                sw.Close();
                crstream.Close();
                memstream.Close();
            }

            return result;
        }
コード例 #17
0
ファイル: Crypto.cs プロジェクト: CoEST/TraceLab
        public static bool EncryptFile (Stream streamToEncrypt, string sOutputFilename) {           
            try {
                FileStream fsEncrypted = new FileStream (sOutputFilename,
                                                         FileMode.Create,
                                                         FileAccess.Write);
                
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider ();
                DES.Key = ASCIIEncoding.ASCII.GetBytes (key);
                DES.IV = ASCIIEncoding.ASCII.GetBytes (key);
                ICryptoTransform desencrypt = DES.CreateEncryptor ();
                
                CryptoStream cryptostream = new CryptoStream (fsEncrypted,
                                                              desencrypt,
                                                              CryptoStreamMode.Write);
                MemoryStream outputStream = streamToEncrypt as MemoryStream;
                
                // Set the position to the beginning of the stream.
                outputStream.Seek (0, SeekOrigin.Begin);
                byte[] byteArray = new byte[outputStream.Length];
                int count = 0; 
                
                while (count < outputStream.Length) {
                    byteArray [count++] = Convert.ToByte (outputStream.ReadByte ());
                }
                
                cryptostream.Write (byteArray, 0, byteArray.Length);
                cryptostream.Close ();
                fsEncrypted.Close ();

            } catch (Exception ex) {
                return false;
            } 
     
            return true;
        }
コード例 #18
0
ファイル: BaseCryption.cs プロジェクト: DesmondNgW/Web
 public static ICryptoTransform CreateEncryptor(string key, CryptionType type)
 {
     ICryptoTransform transform;
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
     switch (type)
     {
         case CryptionType.Aes:
             var aes = Rijndael.Create();
             aes.Mode = CipherMode.CBC;
             transform = aes.CreateEncryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
             aes.Clear();
             break;
         case CryptionType.Des:
             var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = des.CreateEncryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
             des.Clear();
             break;
         default:
             var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = tripleDes.CreateEncryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
             tripleDes.Clear();
             break;
     }
     return transform;
 }
コード例 #19
0
ファイル: Cryptography.cs プロジェクト: Oman/Maleos
        /// <summary>
        /// 使用DES以及固定的密码加密字符串
        /// </summary>
        /// <param name="pToEncrypt">待加密字符串</param>
        /// <returns></returns>
        public static string EncryptString(string pToEncrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

                des.Key = Encoding.Default.GetBytes(DESKEY);
                des.IV = Encoding.Default.GetBytes(DESKEY);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
                    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);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch
            {
                return pToEncrypt;
            }
            finally
            {
                des = null;
            }
        }
コード例 #20
0
 /// <summary>
 /// 加密字符串
 /// </summary>
 /// <param name="inputStr">输入字符串</param>
 /// <param name="keyStr">密码,可以为“”</param>
 /// <returns>输出加密后字符串</returns>
 public string EncryptString(string inputStr, string keyStr)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (keyStr == "")
         keyStr = key;
     byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
     byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
     SHA1 ha = new SHA1Managed();
     byte[] hb = ha.ComputeHash(keyByteArray);
     sKey = new byte[8];
     sIV = new byte[8];
     for (int i = 0; i < 8; i++)
         sKey[i] = hb[i];
     for (int i = 8; i < 16; i++)
         sIV[i - 8] = hb[i];
     des.Key = sKey;
     des.IV = sIV;
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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);
     }
     cs.Close();
     ms.Close();
     return ret.ToString();
 }
コード例 #21
0
ファイル: Encryptor.cs プロジェクト: ChanahC/CSharpTrain
 public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) {
     FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
     /* The statement above initializes the stream intended to read the input file under the passed variable
     sInputFilename with action Open and Access Type reading
     */
     FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
     /*
     The statement above is intended to create a stream that will create the output file
     */
     DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
     DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
     //Set secret key For DES algorithm.
     DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
     //Set initialization vector.
     ICryptoTransform desencrypt = DES.CreateEncryptor();
     //Create a DES encryptor from the DES instance.
     CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); 
     //Create crypto stream set to read and do a 
     //DES encryption transform on incoming bytes.
     byte[] bytearrayinput = new byte[fsInput.Length];
     fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); //reads the content of the input file and tranfers it to the bytearrayinput
     cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); //writes an encrypted form of the input to the encrypted file
     cryptostream.Close();
     fsInput.Close();
     fsEncrypted.Close();
 }
コード例 #22
0
        public static string Encrypt(string p_data)
        {
            // 암호화 알고리즘중 RC2 암호화를 하려면 RC를
            // DES알고리즘을 사용하려면 DESCryptoServiceProvider 객체를 선언한다.
            //RC2 rc2 = new RC2CryptoServiceProvider();
            DESCryptoServiceProvider rc2 = new DESCryptoServiceProvider();

            // 대칭키 배치
            rc2.Key = Skey;
            rc2.IV = Skey;

            // 암호화는 스트림(바이트 배열)을
            // 대칭키에 의존하여 암호화 하기때문에 먼저 메모리 스트림을 생성한다.
            MemoryStream ms = new MemoryStream();

            //만들어진 메모리 스트림을 이용해서 암호화 스트림 생성 
            CryptoStream cryStream =
                              new CryptoStream(ms, rc2.CreateEncryptor(), CryptoStreamMode.Write);

            // 데이터를 바이트 배열로 변경
            byte[] data = Encoding.UTF8.GetBytes(p_data.ToCharArray());

            // 암호화 스트림에 데이터 씀
            cryStream.Write(data, 0, data.Length);
            cryStream.FlushFinalBlock();

            // 암호화 완료 (string으로 컨버팅해서 반환)
            return Convert.ToBase64String(ms.ToArray());
        }
コード例 #23
0
ファイル: Utilities.cs プロジェクト: DF-thangld/web_game
        public static string Encrypt(string text)
        {

            byte[] encodedkey;
            byte[] iv = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x78, 0xA7 };
            byte[] bytes;

            encodedkey = Encoding.UTF8.GetBytes(EncryptKey);
            DESCryptoServiceProvider csp = new DESCryptoServiceProvider();

            bytes = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();

            try
            {
                CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(encodedkey, iv), CryptoStreamMode.Write);

                cs.Write(bytes, 0, bytes.Length);
                cs.FlushFinalBlock();

            }
            catch (Exception ex)
            {
                return "";
            }

            return Convert.ToBase64String(ms.ToArray());

        }
コード例 #24
0
        public static string Encrypt(string text)
        {
            try
            {
                key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] byteArray = Encoding.UTF8.GetBytes(text);

                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream,
                    des.CreateEncryptor(key, IV), CryptoStreamMode.Write);

                cryptoStream.Write(byteArray, 0, byteArray.Length);
                cryptoStream.FlushFinalBlock();

                return Convert.ToBase64String(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                // Handle Exception Here
            }

            return string.Empty;
        }
コード例 #25
0
        // Encrypt the content
        public static string DESEncrypt(string stringToEncrypt)
        {
            byte[] key;
            byte[] IV;

            byte[] inputByteArray;
            try
            {

                key = Convert2ByteArray(DESKey);

                IV = Convert2ByteArray(DESIV);

                inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);

                cs.FlushFinalBlock();

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

            catch (System.Exception ex)
            {

                throw ex;
            }
        }
コード例 #26
0
        //Criptografa o Cookie
        public static string Criptografar(string valor)
        {
            DESCryptoServiceProvider des;
            MemoryStream ms;
            CryptoStream cs; byte[] input;

            try
            {
                using (des = new DESCryptoServiceProvider())
                {

                    using (ms = new MemoryStream())
                    {

                        input = Encoding.UTF8.GetBytes(valor);
                        chave = Encoding.UTF8.GetBytes(chaveCriptografia.Substring(0, 8));

                        using (cs = new CryptoStream(ms, des.CreateEncryptor(chave, iv), CryptoStreamMode.Write))
                        {
                            cs.Write(input, 0, input.Length);
                            cs.FlushFinalBlock();
                        }

                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #27
0
        public static string EncryptString(string _inputString,
           string sKey)
        {
            string sInputFilename = string.Empty;
            string sOutputFilename = string.Empty;
            FileStream fsInput = new FileStream(_inputString,
               FileMode.Open,
               FileAccess.Read);

            FileStream fsEncrypted = new FileStream(sOutputFilename,
               FileMode.Create,
               FileAccess.Write);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted,
               desencrypt,
               CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[_inputString.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();
            return bytearrayinput.ToString();
        }
コード例 #28
0
ファイル: DES.cs プロジェクト: FuChuan7/WeiXin-ASP.NET-MVC3
        public static string DESEnCode(string pToEncrypt, string sKey)
        {
            // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);   
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

            //建立加密对象的密钥和偏移量    
            //原文使用ASCIIEncoding.ASCII方法的GetBytes方法    
            //使得输入密码必须输入英文文本    
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), 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);
            }
            ret.ToString();
            return ret.ToString();
        }
コード例 #29
0
        /// <summary> 
        /// �������� 
        /// </summary> 
        /// <param name="Text">ԭ��</param> 
        /// <param name="sKey">��Կ</param> 
        /// <returns>����</returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(Text);
            byte[] keyByteArray = Encoding.Default.GetBytes(sKey);

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(keyByteArray);

            desKey.Key = HalveByteArray(md5.Hash);
            desKey.IV = HalveByteArray(md5.Hash);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desKey.CreateEncryptor(), CryptoStreamMode.Write);

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

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

            return result.ToString();
        }
コード例 #30
0
        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                return string.Empty;
            }
            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), 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);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #31
0
ファイル: StringEncryptor.cs プロジェクト: jb223cp/ProjektAK1
 public static string Encrypt(string sourceData)
 {
     // set key and initialization vector values
     byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
     byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
     try
     {
         // convert data to byte array
         byte[] sourceDataBytes =
         System.Text.ASCIIEncoding.ASCII.GetBytes(sourceData);
         // get target memory stream
         MemoryStream tempStream = new MemoryStream();
         // get encryptor and encryption stream
         DESCryptoServiceProvider encryptor =
         new DESCryptoServiceProvider();
         CryptoStream encryptionStream =
         new CryptoStream(tempStream,
         encryptor.CreateEncryptor(key, iv),
         CryptoStreamMode.Write);
         // encrypt data
         encryptionStream.Write(sourceDataBytes, 0,
         sourceDataBytes.Length);
         encryptionStream.FlushFinalBlock();
         // put data into byte array
         byte[] encryptedDataBytes = tempStream.GetBuffer();
         // convert encrypted data into string
         return Convert.ToBase64String(encryptedDataBytes, 0,
         (int)tempStream.Length);
     }
     catch
     {
         throw new StringEncryptorException(
         "Unable to encrypt data.");
     }
 }
コード例 #32
0
ファイル: MySecurity.cs プロジェクト: devworker55/Mammatus
 public bool EncryptFile(string filePath, string savePath, string keyStr)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (keyStr == "")
         keyStr = key;
     FileStream fs = File.OpenRead(filePath);
     byte[] inputByteArray = new byte[fs.Length];
     fs.Read(inputByteArray, 0, (int)fs.Length);
     fs.Close();
     byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
     SHA1 ha = new SHA1Managed();
     byte[] hb = ha.ComputeHash(keyByteArray);
     sKey = new byte[8];
     sIV = new byte[8];
     for (int i = 0; i < 8; i++)
         sKey[i] = hb[i];
     for (int i = 8; i < 16; i++)
         sIV[i - 8] = hb[i];
     des.Key = sKey;
     des.IV = sIV;
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     fs = File.OpenWrite(savePath);
     foreach (byte b in ms.ToArray())
     {
         fs.WriteByte(b);
     }
     fs.Close();
     cs.Close();
     ms.Close();
     return true;
 }
コード例 #33
0
ファイル: SimpleZip.cs プロジェクト: CyberFoxHax/PCSXBonus
 private static ICryptoTransform GetDesTransform(byte[] key, byte[] iv, bool decrypt)
 {
     using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
     {
         return (decrypt ? provider.CreateDecryptor(key, iv) : provider.CreateEncryptor(key, iv));
     }
 }
コード例 #34
0
ファイル: Gclass0.cs プロジェクト: salwanbrother/salwan
 public static byte[] smethod_21(byte[] byte_0, string string_0)
 {
     byte[] array = byte_0;
     System.Array.Resize <byte>(ref array, array.Length + 1);
     array[array.Length - 1] = (byte)new System.Random().Next(0, 255);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     dESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     dESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     dESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = dESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
     dESCryptoServiceProvider.Clear();
     return(result);
 }
コード例 #35
0
        /// <summary>
        /// 文字列を暗号化する
        /// </summary>
        /// <param name="str">暗号化する文字列</param>
        /// <returns>暗号化された文字列</returns>
        public static string EncryptString(string str)
        {
            //文字列をバイト型配列にする
            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
            System.IO.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));
        }
コード例 #36
0
ファイル: Securer.cs プロジェクト: snoways/CSharpFramework
            public static string securerEncryption_des(string text, string key = "WangZhi")
            {
                string result = text;

                try
                {
                    byte[] rgbkey         = Encoding.UTF8.GetBytes(key.PadRight(8, 'v').Substring(0, 8));
                    byte[] rgbIV          = Keys;
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(text);
                    System.Security.Cryptography.DESCryptoServiceProvider dCSP = new System.Security.Cryptography.DESCryptoServiceProvider();

                    using (MemoryStream mStream = new MemoryStream())
                    {
                        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey: rgbkey, rgbIV: rgbIV), CryptoStreamMode.Write);
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        result = Convert.ToBase64String(mStream.ToArray());
                    }
                }
                catch (Exception ex)
                {
                }
                return(result);
            }
コード例 #37
0
 //加密的代码:
 /// <summary>
 /// DES加密
 /// </summary>
 /// <param name="str">要加密字符串</param>
 /// <returns>返回加密后字符串</returns>
 public static String Encrypt_DES(String str)
 {
     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(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strDesKey, "md5").Substring(0, 8)); //加密对象的密钥
     des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strDesKey, "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();
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     foreach (Byte b in ms.ToArray())
     {
         sb.AppendFormat("{0:X2}", b);
     }
     return(sb.ToString());
 }
コード例 #38
0
            public static string Encrypt(string Text, string sKey)
            {
                string result;

                if (string.IsNullOrEmpty(Text))
                {
                    result = "";
                }
                else
                {
                    string text = SecretCommon.Md5Hex.Encrypt(sKey, false);
                    System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
                    byte[] bytes = System.Text.Encoding.Default.GetBytes(Text);
                    dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(text.Substring(8, 8));
                    dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(text.Substring(0, 8));
                    MemoryStream memoryStream = new MemoryStream();
                    System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                    byte[] array = memoryStream.ToArray();
                    for (int i = 0; i < array.Length; i++)
                    {
                        byte b = array[i];
                        stringBuilder.AppendFormat("{0:X2}", b);
                    }
                    result = stringBuilder.ToString();
                }
                return(result);
            }
コード例 #39
0
            public static string Encrypt(string Text, string sKey)
            {
                System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
                byte[] bytes = System.Text.Encoding.Default.GetBytes(Text);
                dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                MemoryStream memoryStream = new MemoryStream();

                System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cryptoStream.Write(bytes, 0, bytes.Length);
                cryptoStream.FlushFinalBlock();
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                byte[] array = memoryStream.ToArray();
                for (int i = 0; i < array.Length; i++)
                {
                    byte b = array[i];
                    stringBuilder.AppendFormat("{0:X2}", b);
                }
                return(stringBuilder.ToString());
            }
コード例 #40
0
        private const string IV_64  = "5C83B05C"; //"#CoolSc#";
        public static string EncodeStatic(string data)
        {
            byte[] bytes  = System.Text.Encoding.ASCII.GetBytes(KEY_64);
            byte[] bytes2 = System.Text.Encoding.ASCII.GetBytes(IV_64);
            System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            int keySize = dESCryptoServiceProvider.KeySize;

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, bytes2), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(cryptoStream);
            streamWriter.Write(data);
            streamWriter.Flush();
            cryptoStream.FlushFinalBlock();
            streamWriter.Flush();
            return(System.Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length));
        }
コード例 #41
0
        private static string CryKey = "Xky_Lq_Py_Hu_Lp_Jhj_Zxt";//密钥
        /// <summary>
        /// 加密字符串
        /// </summary>
        public static string Encrypt(string PlainText)
        {
            System.Security.Cryptography.DESCryptoServiceProvider key = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] bk = System.Text.Encoding.Unicode.GetBytes(CryKey);
            byte[] bs = new byte[8];
            for (int i = 0; i < bs.Length; i++)
            {
                bs[i] = bk[i];
            }
            key.Key = bs;
            key.IV  = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

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

            System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, key.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

            System.IO.StreamWriter sw = new System.IO.StreamWriter(encStream);
            sw.WriteLine(PlainText);
            sw.Close();
            encStream.Close();

            byte[] buffer = ms.ToArray();
            ms.Close();

            string s = "";

            for (int i = 0; i < buffer.Length; i++)
            {
                s += buffer[i].ToString("X2");
            }
            return(s);
        }
コード例 #42
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sIn"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public string Encrypt(string sIn, string sKey = "ColtSmart")
        {
            byte[] inputByteArray = System.Text.Encoding.ASCII.GetBytes(sIn);
            using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                des.Mode    = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                byte[] keyByteArray      = new byte[8];
                byte[] inputKeyByteArray = ASCIIEncoding.ASCII.GetBytes(sKey);
                for (int i = 0; i < 8; i++)
                {
                    if (inputKeyByteArray.Length > i)
                    {
                        keyByteArray[i] = inputKeyByteArray[i];
                    }
                    else
                    {
                        keyByteArray[i] = 0;
                    }
                }

                des.Key = keyByteArray;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (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();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return(str);
            }
        }
コード例 #43
0
 private const string IV_64  = "ksax123"; //"#CoolSc#";
 public static string EncodeStatic(string pToEncrypt, string sKey = KEY_64)
 {
     pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     byte[] bytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
     sKey = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8);
     dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
     dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(sKey);
     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(bytes, 0, bytes.Length);
     cryptoStream.FlushFinalBlock();
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     byte[] array = memoryStream.ToArray();
     for (int i = 0; i < array.Length; i++)
     {
         byte b = array[i];
         stringBuilder.AppendFormat("{0:X2}", b);
     }
     stringBuilder.ToString();
     return(stringBuilder.ToString());
 }
コード例 #44
0
ファイル: Encryption.cs プロジェクト: lsfv/queryreport
        private static string DESEncrypt(string MainKey, string strPlain)
        {
            string g_strDESKey = ComputeMD5HashHEX(MainKey + g_strDESIV).Substring(0, 8);
            string p_strReturn = string.Empty;

            try
            {
                string strDESKey   = g_strDESKey;
                string strDESIV    = g_strDESIV;
                byte[] bytesDESKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESKey);
                byte[] bytesDESIV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESIV);
                System.Security.Cryptography.DESCryptoServiceProvider desEncrypt = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), System.Security.Cryptography.CryptoStreamMode.Write);
                System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt);
                swEncrypt.WriteLine(strPlain);
                swEncrypt.Close();
                csEncrypt.Close();
                byte[] bytesCipher = msEncrypt.ToArray();
                msEncrypt.Close();
                p_strReturn = ConvertByteArrayToHex(bytesCipher);
            }
            catch (System.Exception) { }
            return(p_strReturn);
        }
コード例 #45
0
ファイル: Des.cs プロジェクト: Fun33/code
    ///    <summary>
    /// 字串加密
    /// </summary>
    public static string Encrypt(string pToEncrypt)
    {
        if (pToEncrypt == string.Empty)
        {
            return(string.Empty);
        }

        System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
        byte[] inputByteArray = null;
        inputByteArray = System.Text.Encoding.Default.GetBytes(pToEncrypt);
        //'建立加密對象的密鑰和偏移量
        //'原文使用ASCIIEncoding.ASCII方法的GetBytes方法
        //'使得輸入密碼必須輸入英文文本
        des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
        des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
        //'寫二進制數組到加密流
        //'(把內存流中的內容全部寫入)
        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 ret = new System.Text.StringBuilder();
        byte b = 0;

        foreach (byte b_loopVariable in ms.ToArray())
        {
            b = b_loopVariable;
            ret.AppendFormat("{0:X2}", b);
        }
        return(ret.ToString());
    }
コード例 #46
0
 public static string Encrypt(string encryptString, string encryptKey)
 {
     encryptKey = TextUtility.CutLeft(encryptKey, 8);
     encryptKey = encryptKey.PadRight(8, ' ');
     byte[] bytes  = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
     byte[] keys   = DES.Keys;
     byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(encryptString);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     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();
     return(System.Convert.ToBase64String(memoryStream.ToArray()));
 }
コード例 #47
0
 /// <summary>
 /// Encrypt(strText, "&%#@?,:*");
 /// </summary>
 /// <param name="str"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Encryptnew(this string str, string key)
 {
     byte[] byKey;
     byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
     try
     {
         byKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
         System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
         byte[] inputByteArray     = System.Text.Encoding.UTF8.GetBytes(str);
         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();
         return(Convert.ToBase64String(ms.ToArray()));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }