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)); }
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); }
public static byte[] Encrypt(byte[] fileHeader, byte[] nonce, byte[] keyEncryptionKey, byte[] additionalData) { return(XChaCha20BLAKE2b.Encrypt(fileHeader, nonce, keyEncryptionKey, additionalData, TagLength.Medium)); }