Ejemplo n.º 1
0
 /// <summary>
 /// <para>Decrypts bytes with the initialized algorithm and key.</para>
 /// </summary>
 /// <param name="ciphertext"><para>The ciphertext in which you wish to decrypt.</para></param>
 /// <returns><para>The resulting plaintext.</para></returns>
 /// <remarks>
 /// <para>If no encryption is defined, the bytes passed in are returned.</para>
 /// </remarks>
 public byte[] Decrypt(byte[] ciphertext)
 {
     if (encrypted)
     {
         SymmetricCryptographer crypt = new SymmetricCryptographer(keyAlgorithmPair.AlgorithmTypeName, keyAlgorithmPair.Key);
         return crypt.Decrypt(ciphertext);
     }
     return ciphertext;
 }
Ejemplo n.º 2
0
        public void ExportKeyWithPassword()
        {
            string passphrase = "password123";

            this.utility.Export(this.key, Destination, passphrase);

            string contents = GetFileContents(Destination);

            byte[] contentBytes = Convert.FromBase64String(contents);
            Assert.AreEqual(ImportExportUtility.PasswordProtectedFlag, contentBytes[0], "assert first byte is plain text flag");

            byte[] encryptedKey = new byte[64];
            Buffer.BlockCopy(contentBytes, 33, encryptedKey, 0, 64); // 33 = flag(1) + pw hash(32)

            byte[] cryptoKey = GetPassphraseBytes(passphrase);
            SymmetricAlgorithm algorithm = RijndaelManaged.Create();
            SymmetricCryptographer crypto = new SymmetricCryptographer(algorithm, cryptoKey);
            byte[] decryptedKey = crypto.Decrypt(encryptedKey);

            Assert.IsTrue(CryptographyUtility.CompareBytes(this.key, decryptedKey), "key contents");
        }
Ejemplo n.º 3
0
        private string EncryptKey(byte[] key, string passphrase)
        {
            byte[] output = null;
            byte[] passphraseBytes = GetPassphraseBytes(passphrase);

            using (SymmetricAlgorithm algorithm = RijndaelManaged.Create())
            {
                SymmetricCryptographer crypto = new SymmetricCryptographer(algorithm, passphraseBytes);
                byte[] encryptedKey = crypto.Encrypt(key);
                output = AppendPasswordHash(encryptedKey, passphraseBytes, PasswordProtectedFlag);
            }

            return Convert.ToBase64String(output);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// <para>Encrypts bytes with the initialized algorithm and key.</para>
 /// </summary>
 /// <param name="plaintext"><para>The plaintext in which you wish to encrypt.</para></param>
 /// <returns><para>The resulting ciphertext.</para></returns>
 /// <remarks>
 /// <para>If no encryption is defined, the bytes passed in are returned.</para>
 /// </remarks>
 public byte[] Encrypt(byte[] plaintext)
 {
     if (encrypted)
     {
         SymmetricCryptographer crypt = new SymmetricCryptographer(keyAlgorithmPair.AlgorithmTypeName, keyAlgorithmPair.Key);
         return crypt.Encrypt(plaintext);
     }
     return plaintext;
 }
Ejemplo n.º 5
0
        private byte[] DecryptKey(byte[] contentBytes, string passphrase)
        {
            byte[] importKey = null;

            if (passwordProtected)
            {
                byte[] passHash = CheckPassword(contentBytes, passphrase);

                byte[] encryptedKey = new byte[contentBytes.Length - FlagLength - PassHashLength];
                Buffer.BlockCopy(contentBytes, FlagLength + PassHashLength, encryptedKey, 0, encryptedKey.Length);

                using (SymmetricAlgorithm algorithm = RijndaelManaged.Create())
                {
                    SymmetricCryptographer crypto = new SymmetricCryptographer(algorithm, passHash);
                    importKey = crypto.Decrypt(encryptedKey);
                }
            }
            else
            {
                importKey = new byte[contentBytes.Length - 1];
                Buffer.BlockCopy(contentBytes, 1, importKey, 0, contentBytes.Length - FlagLength);
            }

            return importKey;
        }