Inheritance: SymmetricAlgorithm
Beispiel #1
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;
            }
        }
		public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv) 
		{
			if (key == null) {
				key = GetStrongKey ();
			}
			// note: checking weak keys also checks valid key length
			if (TripleDES.IsWeakKey (key)) {
				string msg = Locale.GetText ("This is a known weak key.");
				throw new CryptographicException (msg);
			}

			byte[] key1 = new byte [8];
			byte[] key2 = new byte [8];
			byte[] key3 = new byte [8];
			DES des = DES.Create ();
			Buffer.BlockCopy (key, 0, key1, 0, 8);
			Buffer.BlockCopy (key, 8, key2, 0, 8);
			if (key.Length == 16)
				Buffer.BlockCopy (key, 0, key3, 0, 8);
			else
				Buffer.BlockCopy (key, 16, key3, 0, 8);
	
			// note: some modes (like CFB) requires encryption when decrypting
			if ((encryption) || (algo.Mode == CipherMode.CFB)) {
				E1 = new DESTransform (des, true, key1, iv);
				D2 = new DESTransform (des, false, key2, iv);
				E3 = new DESTransform (des, true, key3, iv);
			}
			else {
				D1 = new DESTransform (des, false, key3, iv);
				E2 = new DESTransform (des, true, key2, iv);
				D3 = new DESTransform (des, false, key1, iv);
			}
		}
Beispiel #3
0
 public TripleDes(CryptoKey cryptoKey = null)
 {
     _tripleDes = TripleDES.Create();
     if (cryptoKey == null) return;
     _key = cryptoKey.Key;
     _iv = cryptoKey.IV;
 }
        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));
        }
Beispiel #5
0
 public override void GenerateKey()
 {
     System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
     tdes.Padding = PaddingMode.None;
     tdes.KeySize = this.KeySize;
     tdes.GenerateKey();
     this._key = tdes;
 }
Beispiel #6
0
 //create key and IV(vector) values for encryption
 public static byte[] Create3DESKey()
 {
     System.Security.Cryptography.TripleDES tDES = System.Security.Cryptography.TripleDES.Create();
     tDES.KeySize = 128;
     tDES.GenerateKey();
     tDES.Mode = CipherMode.ECB;
     return(tDES.Key);
 }
Beispiel #7
0
        public static string Decryption(string CypherText)
        {
            byte[] b = Convert.FromBase64String(CypherText);
            System.Security.Cryptography.TripleDES des = CreateDES(key);
            ICryptoTransform ct = des.CreateDecryptor();

            byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
            return(Encoding.Unicode.GetString(output));
        }
 public override void init(int mode, byte[] key, byte[] iv)
 {
     desm = TripleDES.Create();
     //desm.BlockSize = blockSize*8;
     desm.KeySize = BlockSize * 8;
     desm.Key = key;
     desm.IV = iv;
     ms = new MemoryStream(BlockSize);
 }
Beispiel #9
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));
        }
Beispiel #10
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">矢量</param>
        public TripleDES(string key, string iv)
        {
            this.key = key;
            this.iv  = iv;

            mCrypto         = new TripleDESCryptoServiceProvider();
            mCrypto.Key     = GetLegalKey();
            mCrypto.IV      = GetLegalIV();
            mCrypto.Mode    = CipherMode.CBC;//CBC模式加密
            mCrypto.Padding = PaddingMode.PKCS7;
        }
 public MACTripleDES()
 {
     base.KeyValue = new byte[0x18];
     Utils.StaticRandomNumberGenerator.GetBytes(base.KeyValue);
     this.des = TripleDES.Create();
     base.HashSizeValue = this.des.BlockSize;
     this.m_bytesPerBlock = this.des.BlockSize / 8;
     this.des.IV = new byte[this.m_bytesPerBlock];
     this.des.Padding = PaddingMode.Zeros;
     this.m_encryptor = null;
 }
Beispiel #12
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;
            }
        }
Beispiel #13
0
        //
        // public constructors
        //

        public MACTripleDES() {
            KeyValue = new byte[24];
            Utils.StaticRandomNumberGenerator.GetBytes(KeyValue);

            // Create a TripleDES encryptor
            des = TripleDES.Create();
            HashSizeValue = des.BlockSize;

            m_bytesPerBlock = des.BlockSize/m_bitsPerByte;
            // By definition, MAC-CBC-3DES takes an IV=0.  C# zero-inits arrays,
            // so all we have to do here is define it.
            des.IV = new byte[m_bytesPerBlock];
            des.Padding = PaddingMode.Zeros;

            m_encryptor = null;
        }
Beispiel #14
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));
        }
Beispiel #15
0
        //create key and IV(vector) values for encryption
        public static byte[] Create3DESKey()
        {
#if MONO
            byte[] tripleKey = new byte[16];

            RandomNumberGenerator rng = RandomNumberGenerator.Create();
            rng.GetNonZeroBytes(tripleKey);

            return(tripleKey);
#else
            System.Security.Cryptography.TripleDES tDES = System.Security.Cryptography.TripleDES.Create();
            tDES.KeySize = 128;
            tDES.GenerateKey();
            tDES.Mode = CipherMode.ECB;
            return(tDES.Key);
#endif
        }
Beispiel #16
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));
        }
Beispiel #17
0
		private void Setup (TripleDES tripleDES, byte[] rgbKey) 
		{
			tdes = tripleDES ?? TripleDES.Create ();
			// default padding (as using in Fx 1.0 and 1.1)
			tdes.Padding = PaddingMode.Zeros;
			// if rgbKey is null we keep the randomly generated key
			if (rgbKey != null) {
				// this way we get the TripleDES key validation (like weak
				// and semi-weak keys)
				tdes.Key = rgbKey;
			}
			HashSizeValue = tdes.BlockSize;
			// we use Key property to get the additional validations 
			// (from KeyedHashAlgorithm ancestor)
			Key = tdes.Key;
			mac = new MACAlgorithm (tdes);
			m_disposed = false;
		}
Beispiel #18
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);
        }
Beispiel #19
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);
     }
 }
        /// <summary>
        /// Generate a new 3-DES key
        /// </summary>
        public static void GenerateKey(string keyfile, ILogging logger)
        {
            try
            {
                using (System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create())
                {
                    string key = Convert.ToBase64String(des.Key) + KEY_SPLIT_MARK + Convert.ToBase64String(des.IV);
                    key = $"{KEY_LINER}{Environment.NewLine}{key}{Environment.NewLine}{KEY_LINER}";

                    File.WriteAllText(keyfile, key);
                }
            }
            catch (Exception ex)
            {
                logger.WriteLog(ErrorCodes.TripleDES_GenerateKey,
                                string.Format(ErrorResources.TripleDES_GenerateKey, ex.Message),
                                Severity.Error, VerboseLevel.User);
            }
        }
Beispiel #21
0
 public override string Decrypt(string cipherText, string key, string iv)
 {
     try
     {
         System.Security.Cryptography.TripleDES tdes = System.Security.Cryptography.TripleDES.Create();
         byte[] inputBytes = Convert.FromBase64String(cipherText);
         tdes.Key = Convert.FromBase64String(key);
         tdes.IV  = Convert.FromBase64String(iv);
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, tdes.CreateDecryptor(), CryptoStreamMode.Write);
         cs.Write(inputBytes, 0, inputBytes.Length);
         cs.FlushFinalBlock();
         return(Encoding.UTF8.GetString(ms.ToArray()));
     }
     catch (Exception e)
     {
         return("\nERROR: " + e.Message);
     }
 }
 public MACTripleDES(string strTripleDES, byte[] rgbKey)
 {
     if (rgbKey == null)
     {
         throw new ArgumentNullException("rgbKey");
     }
     if (strTripleDES == null)
     {
         this.des = TripleDES.Create();
     }
     else
     {
         this.des = TripleDES.Create(strTripleDES);
     }
     base.HashSizeValue = this.des.BlockSize;
     base.KeyValue = (byte[]) rgbKey.Clone();
     this.m_bytesPerBlock = this.des.BlockSize / 8;
     this.des.IV = new byte[this.m_bytesPerBlock];
     this.des.Padding = PaddingMode.Zeros;
     this.m_encryptor = null;
 }
Beispiel #23
0
        public bool Decrypt(ref string value)
        {
            Byte[] tmp    = Convert.FromBase64String(value);
            string result = string.Empty;

            System.Security.Cryptography.TripleDES tripleDES = System.Security.Cryptography.TripleDES.Create();
            ICryptoTransform decryptor = tripleDES.CreateDecryptor(key, iv);

            using (MemoryStream ms = new MemoryStream(tmp)) {
                try {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
                        StreamReader reader = new StreamReader(cs);
                        result = reader.ReadLine();
                    }
                } catch (System.Security.Cryptography.CryptographicException ex) {
                    value = ex.Message;
                    return(false);
                }
            }
            tripleDES.Clear();
            value = result;
            return(true);
        }
        public override byte[] DecryptKey(byte[] rgbEncryptedWrappedKeyData)
        {
            System.Security.Cryptography.TripleDES tdes = (System.Security.Cryptography.TripleDES) this.Key;
            if (((rgbEncryptedWrappedKeyData.Length != 0x20) && (rgbEncryptedWrappedKeyData.Length != 40)) && (rgbEncryptedWrappedKeyData.Length != 0x30))
            {
                throw new CryptographicException("BadSizeForKeyToBeWrapped");
            }
            tdes.Mode    = CipherMode.CBC;
            tdes.Padding = PaddingMode.None;
            ICryptoTransform transform = tdes.CreateDecryptor(tdes.Key, TripleDESKeyExchangeFormatter.s_rgbTripleDES_KW_IV);

            byte[] buffer1 = transform.TransformFinalBlock(rgbEncryptedWrappedKeyData, 0, rgbEncryptedWrappedKeyData.Length);
            Array.Reverse(buffer1);
            byte[] buffer2 = new byte[8];
            Buffer.BlockCopy(buffer1, 0, buffer2, 0, 8);
            byte[] buffer3 = new byte[buffer1.Length - buffer2.Length];
            Buffer.BlockCopy(buffer1, 8, buffer3, 0, buffer3.Length);
            ICryptoTransform transform2 = tdes.CreateDecryptor(tdes.Key, buffer2);

            byte[] buffer4 = transform2.TransformFinalBlock(buffer3, 0, buffer3.Length);
            byte[] buffer5 = new byte[buffer4.Length - 8];
            Buffer.BlockCopy(buffer4, 0, buffer5, 0, buffer5.Length);
            SHA1 sha1 = SHA1.Create();

            byte[] buffer6 = sha1.ComputeHash(buffer5);
            int    bufLen  = buffer5.Length;

            for (int keyBufPos = 0; bufLen < buffer4.Length; keyBufPos++)
            {
                if (buffer4[bufLen] != buffer6[keyBufPos])
                {
                    throw new CryptographicException("BadSizeForKeyToBeUnwrapped");
                }
                bufLen++;
            }
            return(buffer5);
        }
Beispiel #25
0
        public MACTripleDES(String strTripleDES, byte[] rgbKey) {
            // Make sure we know which algorithm to use
            if (rgbKey == null)
                throw new ArgumentNullException("rgbKey");
            Contract.EndContractBlock();
            // Create a TripleDES encryptor
            if (strTripleDES == null) {
                des = TripleDES.Create();
            } else {
                des = TripleDES.Create(strTripleDES);
            }

            HashSizeValue = des.BlockSize;
            // Stash the key away
            KeyValue = (byte[]) rgbKey.Clone();

            m_bytesPerBlock = des.BlockSize/m_bitsPerByte;
            // By definition, MAC-CBC-3DES takes an IV=0.  C# zero-inits arrays,
            // so all we have to do here is define it.
            des.IV = new byte[m_bytesPerBlock];
            des.Padding = PaddingMode.Zeros;

            m_encryptor = null;
        }
 public TripleDESCryptoServiceProvider()
 {
     // This class wraps TripleDES
     _impl = TripleDES.Create();
 }
Beispiel #27
0
 public TDES()
 {
     des=TripleDES.Create();
 }
 public TripleDESKeyExchangeFormatter(System.Security.Cryptography.TripleDES key)
     : base(key)
 {
 }
 public TripleDESCryptoServiceProvider()
 {
     // This class wraps TripleDES
     _impl = TripleDES.Create();
     _impl.FeedbackSize = 8;
 }
Beispiel #30
0
		protected override void Dispose (bool disposing) 
		{
			if (!m_disposed) {
				// note: we ALWAYS zeroize keys (disposing or not)
	
				// clear our copy of the secret key
				if (KeyValue != null)
					Array.Clear (KeyValue, 0, KeyValue.Length);
				// clear the secret key (inside TripleDES)
				if (tdes != null)
					tdes.Clear ();
	
				if (disposing) {
					// disposed managed stuff
					KeyValue = null;
					tdes = null;
				}
				// ancestor
				base.Dispose (disposing);
				m_disposed = true;
			}
		}
 public TripleDESCryptoServiceProvider()
 {
     // This class wraps TripleDES
     _impl = TripleDES.Create();
     _impl.FeedbackSize = 8;
 }
	// Set up the TripleDES algorithm instance.
	private void SetupAlgorithm(String name)
			{
				// Create the algorithm instance.
				alg = TripleDES.Create(name);

				// Set the starting initialization vector to all-zeroes.
				alg.IV = new byte [8];

				// Set the padding mode to zeroes.
				alg.Padding = PaddingMode.Zeros;

				// We don't have a stream yet.
				stream = null;

				// Allocate the temporary block for the final hash value.
				block = new byte [8];
			}
Beispiel #33
0
 /// <summary>Creates an instance of a cryptographic object to perform the <see cref="T:System.Security.Cryptography.TripleDES" /> algorithm.</summary>
 /// <returns>An instance of a cryptographic object.</returns>
 // Token: 0x060023C9 RID: 9161 RVA: 0x000824AD File Offset: 0x000806AD
 public new static TripleDES Create()
 {
     return(TripleDES.Create("System.Security.Cryptography.TripleDES"));
 }
Beispiel #34
0
 public TripleDESEncryptionFormatter(System.Security.Cryptography.TripleDES key)
     : base(key)
 {
 }
Beispiel #35
0
 public TripleDESCryptoServiceProvider()
 {
     // This class wraps TripleDES
     _impl = TripleDES.Create();
 }
Beispiel #36
0
        //----------------------------------------------------------------------------------

        public static void SetEncryptedPrivateKey
        (
            RSACryptoServiceProvider rsa
            , byte[]                   data
            , string password
        )
        {
            byte[] OIDpkcs5PBES2  = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D };
            byte[] OIDpkcs5PBKDF2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0C };
            byte[] OIDdesEDE3CBC  = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07 };

            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    int n = br.ReadUInt16();
                    if (n == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    else
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    if ((n = br.ReadUInt16()) == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    byte[] b = br.ReadBytes(11);
                    if (!OpenSSL.CompareByteArray(b, OIDpkcs5PBES2))
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    Array.Clear(b, 0, b.Length);

                    if ((n = br.ReadUInt16()) == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    if ((n = br.ReadUInt16()) == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    b = br.ReadBytes(11);
                    if (!OpenSSL.CompareByteArray(b, OIDpkcs5PBKDF2))
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    Array.Clear(b, 0, b.Length);

                    if ((n = br.ReadUInt16()) == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    if (br.ReadByte() != 0x04)
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    byte[] salt = br.ReadBytes(br.ReadByte());

                    if (br.ReadByte() != 0x02)
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    int iterations = 0;
                    if ((n = br.ReadByte()) == 1)
                    {
                        iterations = br.ReadByte();
                    }

                    else if (n == 2)
                    {
                        iterations = 256 * br.ReadByte() + br.ReadByte();
                    }

                    else
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    if ((n = br.ReadUInt16()) == 0x8130)
                    {
                        br.ReadByte();
                    }

                    else if (n == 0x8230)
                    {
                        br.ReadInt16();
                    }

                    b = br.ReadBytes(10);
                    if (!OpenSSL.CompareByteArray(b, OIDdesEDE3CBC))
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    Array.Clear(b, 0, b.Length);

                    if (br.ReadByte() != 0x04)
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    byte[] iv = br.ReadBytes(br.ReadByte());

                    if (br.ReadByte() != 0x04)
                    {
                        throw new CryptographicException(OpenSSL.MSG_WRONG_OSSL_DATA);
                    }

                    if ((n = br.ReadByte()) == 0x81)
                    {
                        n = br.ReadByte();
                    }

                    else if (n == 0x82)
                    {
                        n = 256 * br.ReadByte() + br.ReadByte();
                    }

                    data = br.ReadBytes(n);

                    Rfc2898DeriveBytes db = new Rfc2898DeriveBytes(password, salt, iterations);
                    byte[]             bk = db.GetBytes(24);

                    using (TripleDES td = TripleDES.Create())
                        b = OpenSSL.Decrypt(td, bk, iv, data);

                    OpenSSL.SetPrivateKey(rsa, b);

                    db.Reset();
                    Array.Clear(b, 0, b.Length);
                    Array.Clear(data, 0, data.Length);
                    Array.Clear(salt, 0, salt.Length);
                    Array.Clear(iv, 0, iv.Length);
                    Array.Clear(bk, 0, bk.Length);
                }
            }
        }
Beispiel #37
0
 bool ICngSymmetricAlgorithm.IsWeakKey(byte[] key)
 {
     return(TripleDES.IsWeakKey(key));
 }
Beispiel #38
0
 public TripleDES()
 {
     this._trdes = System.Security.Cryptography.TripleDES.Create();
 }