예제 #1
0
      internal static string EncryptValue(string value)
      {
         if (String.IsNullOrWhiteSpace(value))
         {
            return String.Empty;
         }

         using (var symmetricProvider = new Symmetric(SymmetricProvider.Rijndael, false))
         {
            string result;

            try
            {
               symmetricProvider.IntializationVector = IV;
               result = symmetricProvider.Encrypt(new Data(value), SymmetricKey).Bytes.ToBase64();
            }
            catch (CryptographicException)
            {
               // return the value as is
               result = value;
            }

            return result;
         }
      }
예제 #2
0
        /// <summary>
        /// test using auto-generated keys
        /// </summary>
        private static string SymmetricLoopback(SymmetricProvider p, string targetString)
        {
            Data decryptedData;
             using (var sym = new Symmetric(p))
             {
            Data encryptedData = sym.Encrypt(new Data(targetString));
            using (var sym2 = new Symmetric(p))
            {
               decryptedData = sym2.Decrypt(encryptedData, sym.Key);
            }
             }

             ////-- the data will be padded to the encryption blocksize, so we need to trim it back down.
             //return decryptedData.ToString().Substring(0, _TargetData.Bytes.Length);

             return decryptedData.ToString();
        }
예제 #3
0
        /// <summary>
        /// test using user-provided keys and init vectors
        /// </summary>
        private static string SymmetricWithKey(SymmetricProvider p, string targetString)
        {
            var keyData = new SymmetricKeyData("MySecretPassword");
             var ivData = new SymmetricKeyData("MyInitializationVector");

             Data encryptedData;
             using (var sym = new Symmetric(p, false))
             {
            sym.IntializationVector = ivData;
            encryptedData = sym.Encrypt(new Data(targetString), keyData);
             }

             Data decryptedData;
             using (var sym2 = new Symmetric(p, false))
             {
            sym2.IntializationVector = ivData;
            decryptedData = sym2.Decrypt(encryptedData, keyData);
             }

             ////-- the data will be padded to the encryption blocksize, so we need to trim it back down.
             //return decryptedData.ToString().Substring(0, _TargetData.Bytes.Length);

             return decryptedData.ToString();
        }
예제 #4
0
        private static string SymmetricFilePrivate(SymmetricProvider p, string fileName, string key)
        {
            string encryptedFilePath = Path.GetFileNameWithoutExtension(fileName) + ".encrypted";
             string decryptedFilePath = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(fileName)) + "-decrypted" + Path.GetExtension(fileName);

             // encrypt the file to memory
             Data encryptedData;
             using (var sym = new Symmetric(p))
             {
            sym.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(fileName))
            {
               encryptedData = sym.Encrypt(stream);
            }
             }

             // write encrypted data to a new binary file
             using (var stream = File.Open(encryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(encryptedData.Bytes);
             }

             // decrypt this binary file
             Data decryptedData;
             using (var sym2 = new Symmetric(p))
             {
            sym2.Key = new SymmetricKeyData(key);
            using (var stream = File.OpenRead(encryptedFilePath))
            {
               decryptedData = sym2.Decrypt(stream);
            }
             }

             // write decrypted data to a new binary file
             using (var stream = File.Open(decryptedFilePath, FileMode.Create))
             using (var bw = new BinaryWriter(stream))
             {
            bw.Write(decryptedData.Bytes);
             }

             // get the MD5 hash of the returned data
             using (var h = new Hash(HashProvider.MD5))
             {
            return h.Calculate(decryptedData).Bytes.ToHex();
             }
        }