コード例 #1
0
        /// <summary>
        /// DES + Base64 ¼ÓÃÜ
        /// </summary>
        /// <param name="input">Ã÷ÎÄ×Ö·û´®</param>
        /// <returns>ÒѼÓÃÜ×Ö·û´®</returns>
        public static string DesBase64Encrypt(string input, string decryptKey)
        {
            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
            des.Mode = System.Security.Cryptography.CipherMode.ECB;
            ICryptoTransform ct;
            MemoryStream     ms;
            CryptoStream     cs;

            byte[] byt;
            byte[] Key = Encoding.UTF8.GetBytes(decryptKey);
            byte[] IV  = Encoding.UTF8.GetBytes(decryptKey);

            ct = des.CreateEncryptor(Key, IV);

            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //¸ù¾Ý GB2312 ±àÂë¶Ô×Ö·û´®´¦Àí£¬×ª»»³É byte Êý×é

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

            cs.Close();

            byte[] answer = ms.ToArray();
            for (int j = 0; j < answer.Length; j++)
            {
                Console.Write(answer[j].ToString() + " ");
            }
            Console.WriteLine();
            return(Convert.ToBase64String(ms.ToArray())); // ½«¼ÓÃÜµÄ byte Êý×éÒÀÕÕ Base64 ±àÂëת»»³É×Ö·û´®
        }
コード例 #2
0
        /// <summary>
        /// DES + Base64 加密
        /// </summary>
        /// <param name="input">明文字符串</param>
        /// <returns>已加密字符串</returns>
        public static string DesBase64EncryptForID5(string input)
        {
            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
            des.Mode = System.Security.Cryptography.CipherMode.CBC;
            ICryptoTransform ct;
            MemoryStream     ms;
            CryptoStream     cs;

            byte[] byt;
            byte[] Key = new byte[8] {
                56, 50, 55, 56, 56, 55, 49, 49
            };
            byte[] IV = new byte[8] {
                56, 50, 55, 56, 56, 55, 49, 49
            };

            ct = des.CreateEncryptor(Key, IV);

            byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组

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

            cs.Close();

            byte[] answer = ms.ToArray();
            for (int j = 0; j < answer.Length; j++)
            {
                Console.Write(answer[j].ToString() + " ");
            }
            Console.WriteLine();
            return(Convert.ToBase64String(ms.ToArray())); // 将加密的 byte 数组依照 Base64 编码转换成字符串
        }
コード例 #3
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="strData"></param>
		/// <param name="des"></param>
		/// <returns></returns>
		public byte[] EncryptString(string strData, DES des)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(des != null, "des");

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

			MemoryStream mStream = new MemoryStream();

			try
			{
				CryptoStream encStream = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write);

				try
				{
					encStream.Write(bytes, 0, bytes.Length);
				}
				finally
				{
					encStream.Close();
				}

				return mStream.ToArray();
			}
			finally
			{
				mStream.Close();
			}
		}
コード例 #4
0
        /// <summary>
        /// 加密byte[]
        /// </summary>
        /// <param name="plainText">原内容</param>
        /// <param name="encryptKey">密码密钥</param>
        /// <returns></returns>
        public static byte[] EncryptBuffer(byte[] plainText, string encryptKey)
        {
            //cipherkey = TextUtility.CutLeft(cipherkey, 8); cipherkey.PadRight(8, ' ');
            encryptKey = DES.GetPassword(encryptKey);
            using System.Security.Cryptography.DES myAes = System.Security.Cryptography.DES.Create();             //DESCryptoServiceProvider aesCryptoServiceProvider = new();
            using ICryptoTransform cryptoTransform       = myAes.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), DES.Keys);

            return(cryptoTransform.TransformFinalBlock(plainText, 0, plainText.Length));
        }
コード例 #5
0
        public void GenerateKeys()
        {
            des.GenerateKey();
            if (des.Mode == CipherMode.CBC)
            {
                des.GenerateIV();
            }

            encrypt = des.CreateEncryptor();
            decrypt = des.CreateDecryptor();
        }
コード例 #6
0
        private DESCipher(System.Security.Cryptography.DES des)
        {
            this.des.Mode      = des.Mode;
            this.des.KeySize   = des.KeySize;
            this.des.BlockSize = des.BlockSize;

            this.des.Key = des.Key;
            this.des.IV  = des.IV;

            encrypt = des.CreateEncryptor();
            decrypt = des.CreateDecryptor();
        }
コード例 #7
0
        public string Encrypt(string value)
        {
            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();
            byte[]           tmp = Encoding.UTF8.GetBytes(value);
            Byte[]           encryptoData;
            ICryptoTransform encryptor = des.CreateEncryptor(key, iv);

            using (MemoryStream memoryStream = new MemoryStream()) {
                using (var cs = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
                    using (StreamWriter writer = new StreamWriter(cs)) {
                        writer.Write(value);
                        writer.Flush();
                    }
                }
                encryptoData = memoryStream.ToArray();
            }
            des.Clear();
            return(Convert.ToBase64String(encryptoData));
        }
コード例 #8
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="encryptKey">加密密钥</param>
        /// <returns></returns>
        public string Encrypt(string plainText, string encryptKey)
        {
            if (!string.IsNullOrEmpty(encryptKey))
            {
                this.key    = encryptKey;
                mCrypto.Key = GetLegalKey();
            }

            byte[] arrPlainText = Encoding.UTF8.GetBytes(plainText);
            using (MemoryStream stream = new MemoryStream())
            {
                ICryptoTransform encryptor = mCrypto.CreateEncryptor();
                using (CryptoStream cStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
                {
                    cStream.Write(arrPlainText, 0, arrPlainText.Length);
                    cStream.FlushFinalBlock();
                }
                byte[] outputBytes = stream.ToArray();
                return(Convert.ToBase64String(outputBytes));
            }
        }
コード例 #9
0
 public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor();
コード例 #10
0
ファイル: DesForm.cs プロジェクト: ewin66/wholePivas
        private string RunEsOrDs(string ValueString, string Key, bool EsOrDs)
        {
            string RET = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(ValueString) && !string.IsNullOrEmpty(Key))
                {
                    Key = Key + Key.Length;
                    string k = string.Empty;
                    using (System.Security.Cryptography.MD5 md = System.Security.Cryptography.MD5.Create())
                    {
                        k = BitConverter.ToString(md.ComputeHash(Encoding.UTF8.GetBytes(Key))).Replace("-", string.Empty);
                    }
                    byte[] inputByteArray = EsOrDs ? System.Text.Encoding.UTF8.GetBytes(ValueString) : System.Convert.FromBase64String(ValueString);
                    byte[] rgbKey         = System.Text.Encoding.UTF8.GetBytes(k.Substring(0, 8));
                    byte[] rgbIV          = System.Text.Encoding.UTF8.GetBytes(k.Substring(k.Length - 8, 8));
                    using (System.Security.Cryptography.DES DCSP = System.Security.Cryptography.DES.Create())
                    {
                        using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
                        {
                            using (System.Security.Cryptography.CryptoStream cStream = new System.Security.Cryptography.CryptoStream(mStream, EsOrDs ? DCSP.CreateEncryptor(rgbKey, rgbIV) : DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write))
                            {
                                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                                cStream.FlushFinalBlock();
                                RET = EsOrDs ? System.Convert.ToBase64String(mStream.ToArray()) : System.Text.Encoding.UTF8.GetString(mStream.ToArray());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(RET);
        }