示例#1
0
 public static byte[] Encrypt(byte[] passwordBytes, byte[] keyAlgorithm, byte[] privateKey)
 {
     byte[] salt = Generate.Salt();
     byte[] key  = Argon2.DeriveKey(passwordBytes, salt);
     CryptographicOperations.ZeroMemory(passwordBytes);
     byte[] nonce               = Generate.Nonce();
     byte[] additionalData      = Arrays.Concat(keyAlgorithm, Constants.PrivateKeyVersion);
     byte[] encryptedPrivateKey = XChaCha20BLAKE2b.Encrypt(privateKey, nonce, key, additionalData, TagLength.Medium);
     CryptographicOperations.ZeroMemory(privateKey);
     CryptographicOperations.ZeroMemory(key);
     return(Arrays.Concat(additionalData, salt, nonce, encryptedPrivateKey));
 }
示例#2
0
        private static void Encrypt(FileStream inputFile, FileStream outputFile, byte[] nonce, byte[] dataEncryptionKey, byte[] additionalData)
        {
            const int offset = 0;

            byte[] plaintextChunk = new byte[Constants.FileChunkSize];
            while (inputFile.Read(plaintextChunk, offset, plaintextChunk.Length) > 0)
            {
                byte[] ciphertextChunk = XChaCha20BLAKE2b.Encrypt(plaintextChunk, nonce, dataEncryptionKey, additionalData, TagLength.Medium);
                nonce          = Utilities.Increment(nonce);
                additionalData = ChunkHandling.GetPreviousTag(ciphertextChunk);
                outputFile.Write(ciphertextChunk, offset, ciphertextChunk.Length);
            }
            CryptographicOperations.ZeroMemory(dataEncryptionKey);
        }
示例#3
0
 public static byte[] Encrypt(byte[] fileHeader, byte[] nonce, byte[] keyEncryptionKey, byte[] additionalData)
 {
     return(XChaCha20BLAKE2b.Encrypt(fileHeader, nonce, keyEncryptionKey, additionalData, TagLength.Medium));
 }