public override byte[] EncryptKey(byte[] rgbWrappedKeyData)
        {
            byte[] buffer1 = this.Key.Key;
            SHA1   sha1    = SHA1.Create();

            byte[] buffer2 = sha1.ComputeHash(rgbWrappedKeyData);
            RNGCryptoServiceProvider rngProvider = new RNGCryptoServiceProvider();

            byte[] buffer3 = new byte[8];
            rngProvider.GetBytes(buffer3);
            byte[] buffer4 = new byte[rgbWrappedKeyData.Length + 8];
            System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
            tdes.Padding = PaddingMode.None;
            ICryptoTransform transform = tdes.CreateEncryptor(buffer1, buffer3);

            Buffer.BlockCopy(rgbWrappedKeyData, 0, buffer4, 0, rgbWrappedKeyData.Length);
            Buffer.BlockCopy(buffer2, 0, buffer4, rgbWrappedKeyData.Length, 8);
            byte[] buffer5 = transform.TransformFinalBlock(buffer4, 0, buffer4.Length);
            byte[] buffer6 = new byte[buffer3.Length + buffer5.Length];
            Buffer.BlockCopy(buffer3, 0, buffer6, 0, buffer3.Length);
            Buffer.BlockCopy(buffer5, 0, buffer6, buffer3.Length, buffer5.Length);
            Array.Reverse(buffer6);
            transform = tdes.CreateEncryptor(buffer1, TripleDESKeyExchangeFormatter.s_rgbTripleDES_KW_IV);
            return(transform.TransformFinalBlock(buffer6, 0, buffer6.Length));
        }
Ejemplo n.º 2
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            m_triDes = new TripleDESCryptoServiceProvider();
            m_triDes.Mode = CipherMode.CBC;
            m_triDes.Padding = PaddingMode.None;

            byte[] tmp;
            if (iv.Length > m_ivsize)
            {
                tmp = new byte[m_ivsize];
                Array.Copy(iv, 0, tmp, 0, tmp.Length);
                iv = tmp;
            }
            if (key.Length > m_bsize)
            {
                tmp = new byte[m_bsize];
                Array.Copy(key, 0, tmp, 0, tmp.Length);
                key = tmp;
            }

            try
            {
                m_cipher = (mode == ENCRYPT_MODE
                    ? m_triDes.CreateEncryptor(key, iv)
                    : m_triDes.CreateDecryptor(key, iv)
                    );
            }
            catch (Exception)
            {
                m_cipher = null;
            }
        }
Ejemplo n.º 3
0
        public static string Encryption(string PlainText)
        {
            System.Security.Cryptography.TripleDES des = CreateDES(key);
            ICryptoTransform ct = des.CreateEncryptor();

            byte[] input  = Encoding.Unicode.GetBytes(PlainText);
            byte[] buffer = ct.TransformFinalBlock(input, 0, input.Length);
            return(Convert.ToBase64String(buffer));
        }
Ejemplo n.º 4
0
        public static string EncryptTripleDES(string textKey, string content)
        {
            byte[] key  = Encoding.GetEncoding(1252).GetBytes(textKey);
            byte[] iv   = new byte[8];
            byte[] data = Encoding.GetEncoding(1252).GetBytes(content);
            byte[] enc  = new byte[0];
            System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
            tdes.IV      = iv;
            tdes.Key     = key;
            tdes.Mode    = CipherMode.CBC;
            tdes.Padding = PaddingMode.Zeros;
            ICryptoTransform ict = tdes.CreateEncryptor();

            enc = ict.TransformFinalBlock(data, 0, data.Length);
            return(Encoding.GetEncoding(1252).GetString(enc));
        }
Ejemplo n.º 5
0
        public string Encrypt(string value)
        {
            Byte[] tmp     = null;
            Byte[] tmpData = Encoding.UTF8.GetBytes(value);
            System.Security.Cryptography.TripleDES tripleDes = System.Security.Cryptography.TripleDES.Create();
            ICryptoTransform encryptor = tripleDes.CreateEncryptor(key, iv);

            using (MemoryStream ms = new MemoryStream()) {
                using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                    StreamWriter writer = new StreamWriter(cs);
                    writer.Write(value);
                    writer.Flush();//这句很重要,在对流操作结束后必须用这句话强制将缓冲区中的数据全部写入到目标对象中
                }
                tmp = ms.ToArray();
            }
            return(Convert.ToBase64String(tmp));
        }
Ejemplo n.º 6
0
 public override string Encrypt(string plainText)
 {
     try
     {
         System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
         byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
         this.Key = Convert.ToBase64String(tdes.Key).Replace("-", "");
         this.IV  = Convert.ToBase64String(tdes.IV).Replace("-", "");
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, tdes.CreateEncryptor(), CryptoStreamMode.Write);
         cs.Write(inputBytes, 0, inputBytes.Length);
         cs.FlushFinalBlock();
         return(Convert.ToBase64String(ms.ToArray()));
     }
     catch (Exception e)
     {
         return("\nERROR: " + e.Message);
     }
 }
Ejemplo n.º 7
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            if (mode != ENCRYPT_MODE && mode != DECRYPT_MODE) throw new ArgumentOutOfRangeException();
            ms = new PipedMemoryStream();
            desm = TripleDES.Create();
            desm.KeySize = BlockSize * 8;
            desm.Padding = PaddingMode.None;
            ICryptoTransform ict;
            if (mode == ENCRYPT_MODE)
            {
                ict = desm.CreateEncryptor(key, iv);
            }
            else
            {
                ict = desm.CreateDecryptor(key, iv);
            }

            cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
        }
Ejemplo n.º 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));
            }
        }
 public override ICryptoTransform CreateEncryptor() => _impl.CreateEncryptor();
Ejemplo n.º 10
0
        public override void init(int mode, byte[] key, byte[] iv)
        {
            triDes = new TripleDESCryptoServiceProvider();
            triDes.Mode = CipherMode.CBC;
            triDes.Padding = PaddingMode.None;
            //String pad="NoPadding";
            //if(padding) pad="PKCS5Padding";
            byte[] tmp;
            if (iv.Length > ivsize)
            {
                tmp = new byte[ivsize];
                Array.Copy(iv, 0, tmp, 0, tmp.Length);
                iv = tmp;
            }
            if (key.Length > bsize)
            {
                tmp = new byte[bsize];
                Array.Copy(key, 0, tmp, 0, tmp.Length);
                key = tmp;
            }

            try
            {
                //      cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad);
                /*
                      // The following code does not work on IBM's JDK 1.4.1
                      SecretKeySpec skeySpec = new SecretKeySpec(key, "DESede");
                      cipher.init((mode==ENCRYPT_MODE?
                           javax.crypto.Cipher.ENCRYPT_MODE:
                           javax.crypto.Cipher.DECRYPT_MODE),
                          skeySpec, new IvParameterSpec(iv));
                */
                //      DESedeKeySpec keyspec=new DESedeKeySpec(key);
                //      SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
                //      SecretKey _key=keyfactory.generateSecret(keyspec);
                //      cipher.init((mode==ENCRYPT_MODE?
                //		   javax.crypto.Cipher.ENCRYPT_MODE:
                //		   javax.crypto.Cipher.DECRYPT_MODE),
                //		  _key, new IvParameterSpec(iv));
                cipher = (mode == ENCRYPT_MODE ?
                                                   triDes.CreateEncryptor(key, iv) :
                                                                                       triDes.CreateDecryptor(key, iv));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                cipher = null;
            }
        }