Beispiel #1
0
        public static string DecryptStringWith3DES(string data, string key, string iv)
        {
            UnicodeEncoding unicode = new UnicodeEncoding();
            Byte[] Bytes = Convert.FromBase64String(data);

            MemoryStream mem = new MemoryStream(100);
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            Byte[] KeyBytes = unicode.GetBytes(key);
            Byte[] tmpBytes = new Byte[16];
            Array.Copy(KeyBytes, tmpBytes, KeyBytes.Length < 16 ? KeyBytes.Length : 16);
            KeyBytes = tmpBytes;

            if(tdes.ValidKeySize(KeyBytes.Length*8))
                System.Diagnostics.Debug.WriteLine("Key size valid");
            if(TripleDESCryptoServiceProvider.IsWeakKey(KeyBytes))
                System.Diagnostics.Debug.WriteLine("Key weak");
            CryptoStream CrStream = new CryptoStream(mem, tdes.CreateDecryptor(KeyBytes, unicode.GetBytes(iv)), CryptoStreamMode.Write);

            for(int i = 0; i < Bytes.Length; i++)
                CrStream.WriteByte(Bytes[i]);

            CrStream.FlushFinalBlock();

            string result = unicode.GetString(mem.GetBuffer(), 0, (int)mem.Length);
            CrStream.Dispose();
            return result;
        }
 /// <summary>
 /// Encrypt or decrypt a byte array using the TripleDESCryptoServiceProvider crypto provider and Rfc2898DeriveBytes to build the key and initialization vector.
 /// </summary>
 /// <param name="password">The password String to use in encrypting or decrypting.</param>
 /// <param name="inBytes">The array of bytes to encrypt.</param>
 /// <param name="encrypt">True to encrypt, False to decrypt.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 public static byte[] CryptBytes(String password, byte[] inBytes, bool encrypt)
 {
     // Make a triple DES service provider.
     var desProvider = new TripleDESCryptoServiceProvider();
     // Find a valid key size for this provider.
     var keySize = 0;
     for (var i = 1024; i >= 1; i--)
         if (desProvider.ValidKeySize(i))
         {
             keySize = i;
             break;
         }
     // Get the block size for this provider.
     var blockSize = desProvider.BlockSize;
     // Generate the key and initialization vector.
     byte[] key = null;
     byte[] iv = null;
     byte[] salt = { 0x10, 0x20, 0x12, 0x23, 0x37, 0xA4, 0xC5, 0xA6, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
     MakeKeyAndIv(password, salt, keySize, blockSize, ref key, ref iv);
     // Make the encryptor or decryptor.
     var cryptoTransform = encrypt
                               ? desProvider.CreateEncryptor(key, iv)
                               : desProvider.CreateDecryptor(key, iv);
     byte[] result;
     // Create the output stream.
     using (var streamOut = new MemoryStream())
     {
         // Attach a crypto stream to the output stream.
         var streamCrypto = new CryptoStream(streamOut, cryptoTransform, CryptoStreamMode.Write);
         // Write the bytes into the CryptoStream.
         streamCrypto.Write(inBytes, 0, inBytes.Length);
         try
         {
             streamCrypto.FlushFinalBlock();
         }
         catch (CryptographicException)
         {
             // Ignore this one. The password is bad.
         }
         // Save the result.
         result = streamOut.ToArray();
         // Close the stream.
         try
         {
             streamCrypto.Close();
         }
         catch (CryptographicException)
         {
             // Ignore this one. The password is bad.
         }
         streamOut.Close();
     }
     return result;
 }
Beispiel #3
0
 public static bool ValidateKeySize( EncryptionAlgorithm algID, int Lenght)
 {
     switch (algID)
     {
         case EncryptionAlgorithm.DES:
             DES des = new DESCryptoServiceProvider();
             return des.ValidKeySize(Lenght);
         case EncryptionAlgorithm.Rc2:
             RC2 rc = new RC2CryptoServiceProvider();
             return rc.ValidKeySize(Lenght);
         case EncryptionAlgorithm.Rijndael:
             Rijndael rj = new RijndaelManaged();
             return rj.ValidKeySize(Lenght);
         case EncryptionAlgorithm.TripleDes:
             TripleDES tDes = new TripleDESCryptoServiceProvider();
             return tDes.ValidKeySize(Lenght);
         default:
             throw new CryptographicException("Algorithm " + algID + " Not Supported!");
     }
 }