private static byte[] EncryptFileHeader(string inputFilePath, byte[] dataEncryptionKey, byte[] nonce, byte[] keyEncryptionKey) { byte[] keyCommitmentBlock = ChunkHandling.GetKeyCommitmentBlock(); long fileLength = FileHandling.GetFileLength(inputFilePath); byte[] lastChunkLength = BitConverter.GetBytes(Convert.ToInt32(fileLength % Constants.FileChunkSize)); byte[] fileNameLength = FileHeaders.GetFileNameLength(inputFilePath); byte[] fileHeader = Utilities.ConcatArrays(keyCommitmentBlock, lastChunkLength, fileNameLength, dataEncryptionKey); byte[] additionalData = HeaderEncryption.ComputeAdditionalData(fileLength); return(HeaderEncryption.Encrypt(fileHeader, nonce, keyEncryptionKey, additionalData)); }
public static byte[] Encrypt(byte[] passwordBytes, byte[] privateKey, byte[] keyAlgorithm) { byte[] salt = Generate.Salt(); byte[] key = Argon2.DeriveKey(passwordBytes, salt); Utilities.ZeroArray(passwordBytes); byte[] nonce = Generate.Nonce(); byte[] additionalData = Utilities.ConcatArrays(keyAlgorithm, Constants.PrivateKeyVersion); byte[] keyCommitmentBlock = ChunkHandling.GetKeyCommitmentBlock(); privateKey = Utilities.ConcatArrays(keyCommitmentBlock, privateKey); byte[] encryptedPrivateKey = SecretAeadXChaCha20Poly1305.Encrypt(privateKey, nonce, key, additionalData); Utilities.ZeroArray(privateKey); Utilities.ZeroArray(key); return(Utilities.ConcatArrays(additionalData, salt, nonce, encryptedPrivateKey)); }