Inheritance: SymmetricAlgorithm
Beispiel #1
0
        /// <summary>
        /// DES + Base64 解密
        /// </summary>
        /// <param name="input">密文字符串</param>
        /// <returns>解密字符串</returns>
        public static string DesBase64DecryptForID5(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.CreateDecryptor(Key, IV);
            byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组

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

            cs.Close();

            return(Encoding.GetEncoding("GB2312").GetString(ms.ToArray())); // 将 明文 以 GB2312 编码转换成字符串
        }
		/// <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();
			}
		}
Beispiel #3
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 ±àÂëת»»³É×Ö·û´®
        }
Beispiel #4
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 编码转换成字符串
        }
Beispiel #5
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));
        }
Beispiel #6
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">矢量</param>
        public DES(string key, string iv)
        {
            this.key = key;
            this.iv  = iv;

            mCrypto         = new DESCryptoServiceProvider();
            mCrypto.Key     = GetLegalKey();
            mCrypto.IV      = GetLegalIV();
            mCrypto.Mode    = CipherMode.CBC;//CBC模式加密
            mCrypto.Padding = PaddingMode.PKCS7;
        }
Beispiel #7
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();
        }
Beispiel #8
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));
        }
Beispiel #9
0
        public void Modern_DESTest_Kryptos()
        {
            byte[] key = new byte[8];

            ulong baseKey = (ulong)'S' | (ulong)'O' << 8 | (ulong)'T' << 16 | (ulong)'P' << 24 | (ulong)'Y' << 32 | (ulong)'R' << 40 | (ulong)'K' << 48;
            //ulong baseKey = (ulong)'K' | (ulong)'R' << 8 | (ulong)'Y' << 16 | (ulong)'P' << 24 | (ulong)'T' << 32 | (ulong)'O' << 40 | (ulong)'S' << 48;
            ulong expandedKey = baseKey & 0x7F;

            expandedKey |= ((baseKey >> 7) & 0x7F) << 8;
            expandedKey |= ((baseKey >> 14) & 0x7F) << 16;
            expandedKey |= ((baseKey >> 21) & 0x7F) << 24;
            expandedKey |= ((baseKey >> 28) & 0x7F) << 32;
            expandedKey |= ((baseKey >> 35) & 0x7F) << 40;
            expandedKey |= ((baseKey >> 42) & 0x7F) << 48;
            expandedKey |= ((baseKey >> 49) & 0x7F) << 56;

            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

            des.Key  = BitConverter.GetBytes(expandedKey);
            des.Mode = System.Security.Cryptography.CipherMode.ECB;

            System.Security.Cryptography.ICryptoTransform xform = des.CreateDecryptor();

            byte[] data = new byte[8];
            data[0] = (byte)'O';
            data[1] = (byte)'K';
            data[2] = (byte)'R';
            data[3] = (byte)'U';
            data[4] = (byte)'O';
            data[5] = (byte)'X';
            data[6] = (byte)'O';

            byte[] output = new byte[8];
            xform.TransformBlock(data, 0, 8, output, 0);

            for (int i = 0; i < 8; i++)
            {
                output[i] = (byte)(output[i] % 26);
            }
        }
Beispiel #10
0
        public bool Decrypt(ref string value)
        {
            string resultData = string.Empty;

            Byte[] tmpData = Convert.FromBase64String(value);
            System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

            ICryptoTransform decryptor = des.CreateDecryptor(key, iv);

            using (var memoryStream = new MemoryStream(tmpData)) {
                try {
                    using (var cs = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
                        StreamReader reader = new StreamReader(cs);
                        resultData = reader.ReadLine();
                    }
                } catch (System.Security.Cryptography.CryptographicException ex) {
                    value = ex.Message;
                    return(false);
                }
            }
            value = resultData;
            return(true);
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        /**/
        /// <summary>
        /// DES + Base64 ½âÃÜ
        /// </summary>
        /// <param name="input">ÃÜÎÄ×Ö·û´®</param>
        /// <returns>½âÃÜ×Ö·û´®</returns>
        public static string DesBase64Decrypt(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.CreateDecryptor(Key, IV);
            byt = Convert.FromBase64String(input); // ½« ÃÜÎÄ ÒÔ Base64 ±àÂëת»»³É byte Êý×é

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

            cs.Close();

            return(Encoding.GetEncoding("GB2312").GetString(ms.ToArray())); // ½« Ã÷ÎÄ ÒÔ GB2312 ±àÂëת»»³É×Ö·û´®
        }
Beispiel #13
0
 public DES()
 {
     this._des = System.Security.Cryptography.DES.Create();
 }
Beispiel #14
0
 /// <summary>Creates an instance of a cryptographic object to perform the Data Encryption Standard (<see cref="T:System.Security.Cryptography.DES" />) algorithm.</summary>
 /// <returns>A cryptographic object.</returns>
 /// <PermissionSet>
 ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
 /// </PermissionSet>
 public new static DES Create()
 {
     return(DES.Create("System.Security.Cryptography.DES"));
 }
 public DESCryptoServiceProvider() : base()
 {
     // This class wraps DES
     _impl = DES.Create();
     _impl.FeedbackSize = 8;
 }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="encryptedData"></param>
		/// <param name="des"></param>
		/// <returns></returns>
		public string DecryptString(byte[] encryptedData, DES des)
		{
			ExceptionHelper.FalseThrow<ArgumentNullException>(des != null, "des");

			string strResult = string.Empty;

			MemoryStream mStream = new MemoryStream();

			try
			{
				mStream.Write(encryptedData, 0, encryptedData.Length);
				mStream.Seek(0, SeekOrigin.Begin);

				CryptoStream cryptoStream = new CryptoStream(mStream,
					des.CreateDecryptor(),
					CryptoStreamMode.Read);

				try
				{
					strResult = (new StreamReader(cryptoStream, Encoding.UTF8)).ReadToEnd();
				}
				finally
				{
					cryptoStream.Close();
				}
			}
			finally
			{
				mStream.Close();
			}

			return strResult;
		}
Beispiel #17
0
        private string key = "b4anseitc"; //密钥

        #endregion Fields

        #region Constructors

        /// <summary>
        /// 构造函数
        /// </summary>
        public DESEncrypt()
        {
            des = new DESCryptoServiceProvider();
        }