Esempio n. 1
0
        /// <summary>
        /// 加密字符串返回加密后的字节
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        public static byte[] EncryptPlainText(string plainText)
        {
            TripleDES ta = TripleDES.Create();

            ta.KeySize = 128;
            ta.GenerateKey();
            ta.GenerateIV();
            ta.Mode    = CipherMode.ECB;
            ta.Padding = PaddingMode.PKCS7;
            byte[] head = new byte[4];
            new Random().NextBytes(head);
            byte[] key         = ta.Key;
            byte[] cipherbytes = null;
            try
            {
                MemoryStream ms         = new MemoryStream();
                CryptoStream cs         = new CryptoStream(ms, ta.CreateEncryptor(), CryptoStreamMode.Write);
                byte[]       plainbytes = Encoding.Default.GetBytes(plainText);
                cs.Write(plainbytes, 0, plainbytes.Length);
                cs.Close();
                byte[] cipherText = ms.ToArray();
                ms.Close();
                int len = cipherText.Length + 20;
                cipherbytes = new byte[len];
                head.CopyTo(cipherbytes, 0);
                key.CopyTo(cipherbytes, 4);
                cipherText.CopyTo(cipherbytes, 20);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(cipherbytes);
        }
Esempio n. 2
0
        public static string Encrypt(string key, string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                return("");
            }

            data = data.Trim();

            byte[] keydata   = Encoding.ASCII.GetBytes(key);
            string md5String = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(keydata)).Replace("-", "").ToLower();

            byte[] tripleDesKey = Encoding.ASCII.GetBytes(md5String.Substring(0, 24));

            TripleDES tripdes = TripleDESCryptoServiceProvider.Create();

            tripdes.Mode = CipherMode.ECB;
            tripdes.Key  = tripleDesKey;
            tripdes.GenerateIV();

            MemoryStream ms        = new MemoryStream();
            CryptoStream encStream = new CryptoStream(ms, tripdes.CreateEncryptor(), CryptoStreamMode.Write);

            encStream.Write(Encoding.ASCII.GetBytes(data), 0, Encoding.ASCII.GetByteCount(data));
            encStream.FlushFinalBlock();
            byte[] cryptoByte = ms.ToArray();
            ms.Close();
            encStream.Close();

            return(Convert.ToBase64String(cryptoByte, 0, cryptoByte.GetLength(0)).Trim());
        }
Esempio n. 3
0
        public void Key()
        {
            TripleDES algo = TripleDES.Create();

            algo.GenerateKey();
            algo.GenerateIV();
            Assert.AreEqual(192, algo.KeySize, "Key Size");
            Assert.AreEqual(24, algo.Key.Length, "Key Length");
            Assert.AreEqual(8, algo.IV.Length, "IV Length");
        }
Esempio n. 4
0
        /// <summary>
        /// 获得初始向量 IV.
        /// </summary>
        /// <returns>初试向量 IV.</returns>
        private byte[] GetLegalIV()
        {
            string sTemp = IV;

            mydes.GenerateIV();
            byte[] bytTemp  = mydes.IV;
            int    IVLength = bytTemp.Length;

            if (sTemp.Length > IVLength)
            {
                sTemp = sTemp.Substring(0, IVLength);
            }
            else if (sTemp.Length < IVLength)
            {
                sTemp = sTemp.PadRight(IVLength, ' ');
            }
            return(ASCIIEncoding.ASCII.GetBytes(sTemp));
        }
Esempio n. 5
0
        public static string Decrypt11(string key, string dataen)
        {
            byte[] toEncryptArray = Convert.FromBase64String(dataen);
            byte[] keydata        = Encoding.ASCII.GetBytes(key);
            string md5String      = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(keydata)).Replace("-", "").ToLower();

            byte[]    tripleDesKey = Encoding.ASCII.GetBytes(md5String.Substring(0, 24));
            TripleDES tripdes      = TripleDESCryptoServiceProvider.Create();

            tripdes.Mode    = CipherMode.ECB;
            tripdes.Padding = PaddingMode.PKCS7;
            tripdes.Key     = tripleDesKey;
            tripdes.GenerateIV();
            ICryptoTransform ict = tripdes.CreateDecryptor();

            byte[] resultArray = ict.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tripdes.Clear();
            return(Encoding.ASCII.GetString(resultArray));
        }
Esempio n. 6
0
        public static string encrypt3des(string input, string key_seed)
        {
            string s = Crypto.MD5SUM(Encoding.ASCII.GetBytes(key_seed)).Substring(0, 24);

            input = input.Trim();
            ASCIIEncoding asciiEncoding = new ASCIIEncoding();
            TripleDES     tripleDes     = TripleDES.Create();

            tripleDes.Padding = PaddingMode.Zeros;
            tripleDes.Mode    = CipherMode.ECB;
            tripleDes.Key     = asciiEncoding.GetBytes(s);
            tripleDes.GenerateIV();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, tripleDes.CreateEncryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(asciiEncoding.GetBytes(input), 0, asciiEncoding.GetByteCount(input));
            cryptoStream.FlushFinalBlock();
            byte[] inArray = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return(Convert.ToBase64String(inArray, 0, inArray.GetLength(0)).Trim());
        }
Esempio n. 7
0
        /// <summary>
        /// 按照key,解密
        /// </summary>
        /// <param name="key"></param>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        private static byte[] DecryptCipherText(byte[] key, byte[] cipherText)
        {
            TripleDES ta = TripleDES.Create();

            ta.KeySize = 0x80;
            ta.Key     = key;
            ta.GenerateIV();
            ta.Mode    = CipherMode.ECB;
            ta.Padding = PaddingMode.PKCS7;
            MemoryStream ms = new MemoryStream(cipherText);
            CryptoStream cs = new CryptoStream(ms, ta.CreateDecryptor(), CryptoStreamMode.Read);

            byte[] plainbytesT = new byte[cipherText.Length];
            int    i           = 0;

            try
            {
                int b;
                while ((b = cs.ReadByte()) != -1)
                {
                    plainbytesT[i] = (byte)b;
                    i++;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            byte[] plainbytes = new byte[i];
            Array.Copy(plainbytesT, 0, plainbytes, 0, i);
            try
            {
                cs.Close();
                ms.Close();
            }
            catch { }
            return(plainbytes);
        }
Esempio n. 8
0
 public string CreateTripleDESIV()
 {
     tripleDES.GenerateIV();
     return(ByteToString(tripleDES.IV));
 }
Esempio n. 9
0
 public EncryptedString(string String)
 {
     des.GenerateIV();
     des.GenerateKey();
     Set(String);
 }