예제 #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 编码转换成字符串
        }
예제 #2
0
        public void GenerateKeys()
        {
            des.GenerateKey();
            if (des.Mode == CipherMode.CBC)
            {
                des.GenerateIV();
            }

            encrypt = des.CreateEncryptor();
            decrypt = des.CreateDecryptor();
        }
예제 #3
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();
        }
예제 #4
0
        /// <summary>
        /// 解密byte[]
        /// </summary>
        /// <param name="plainText">密文内容</param>
        /// <param name="encryptKey">密码密钥</param>
        /// <returns></returns>
        public static byte[] DecryptBuffer(byte[] plainText, string encryptKey)
        {
            byte[] result;
            try
            {
                //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 DESCryptoServiceProvider();
                using ICryptoTransform cryptoTransform       = myAes.CreateDecryptor(Encoding.UTF8.GetBytes(encryptKey), DES.Keys);

                result = cryptoTransform.TransformFinalBlock(plainText, 0, plainText.Length);
            }
            catch
            {
                result = null;
            }
            return(result);
        }
예제 #5
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="cryptoText">密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <returns></returns>
        public string Decrypt(string cryptoText, string decryptKey)
        {
            if (!string.IsNullOrEmpty(decryptKey))
            {
                this.key    = decryptKey;
                mCrypto.Key = GetLegalKey();
            }

            byte[] arrCryptoText = Convert.FromBase64String(cryptoText);
            using (MemoryStream stream = new MemoryStream(arrCryptoText, 0, arrCryptoText.Length))
            {
                ICryptoTransform decryptor = mCrypto.CreateDecryptor();
                using (CryptoStream cStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
                {
                    StreamReader reader = new StreamReader(cStream);
                    return(reader.ReadToEnd());
                }
            }
        }
예제 #6
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);
            }
        }
예제 #7
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);
        }
예제 #8
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 ±àÂëת»»³É×Ö·û´®
        }
 public override ICryptoTransform CreateDecryptor() => _impl.CreateDecryptor();
예제 #10
0
		/// <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;
		}
예제 #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);
        }