protected virtual string DecryptName(string name) { try { byte[] bytes = Convert.FromBase64String(name.Replace('-', '/')); using (var ms = new MemoryStream(bytes)) { var header = AesHeader.Read(ms); var aesVersion = header.Version; using (var aes = CreateAes(aesVersion)) { var iv = header.IV; var key = FileSystem.ComputeKey(iv, aesVersion); using (var tranform = aes.CreateDecryptor(key, iv)) using (var outputStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(outputStream, tranform, CryptoStreamMode.Write)) { ms.CopyTo(cryptoStream); } return(Encoding.UTF8.GetString(outputStream.ToArray())); } } } } catch { return(name); } }
protected virtual string EncryptName(string name) { var bytes = Encoding.UTF8.GetBytes(name); var aesVersion = FileSystem.Version; using (var aes = CreateAes(aesVersion)) { var iv = aes.IV; var key = FileSystem.ComputeKey(iv, aesVersion); using (var tranform = aes.CreateEncryptor(key, iv)) { using (var outputStream = new MemoryStream()) { var aesHeader = new AesHeader(aesVersion, iv); aesHeader.Write(outputStream); var encryptedBytes = tranform.TransformFinalBlock(bytes, 0, bytes.Length); outputStream.Write(encryptedBytes, 0, encryptedBytes.Length); return(Convert.ToBase64String(outputStream.ToArray()).Replace('/', '-')); } } } }
public async Task <IFileInfo> CreateFileAsync(string name, Stream stream, long length, CancellationToken ct) { if (FileSystem.EncryptFileName) { name = EncryptName(name); } using (var aes = CreateAes(FileSystem.Version)) { using (var headerStream = new MemoryStream()) { var aesHeader = new AesHeader(FileSystem.Version, aes.IV); aesHeader.Write(headerStream); headerStream.Seek(0, SeekOrigin.Begin); var key = FileSystem.ComputeKey(aes.IV, FileSystem.Version); ICryptoTransform encryptor = CreateTransform(aes, key, aes.IV, AesMode.Encrypt); using (var cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Read)) using (var concat = new StreamEnumerator(new Stream[] { headerStream, cryptoStream }, true)) { var fileInfo = await _directoryInfo.CreateFileAsync(name, concat, length, ct).ConfigureAwait(false); return(new AesFileInfo(FileSystem, fileInfo)); } } } }
public async Task <Stream> OpenReadAsync(CancellationToken ct) { Stream stream = null; System.Security.Cryptography.Aes aes = null; ICryptoTransform decryptor = null; try { stream = await _fileInfo.OpenReadAsync(ct); var header = await AesHeader.ReadAsync(stream, ct); aes = CreateAes(header.Version); var key = FileSystem.ComputeKey(header.IV, header.Version); decryptor = CreateTransform(aes, key, header.IV, AesMode.Decrypt); // disposed by CryptoStream return(new CryptoStream(stream, decryptor, CryptoStreamMode.Read)); } catch { decryptor?.Dispose(); stream?.Dispose(); throw; } finally { aes?.Dispose(); } }
public static AesHeader Read(Stream stream) { var versionByte = new byte[sizeof(AesVersion)]; var iv = new byte[16]; stream.Read(versionByte, 0, versionByte.Length); stream.Read(iv, 0, iv.Length); var header = new AesHeader(); header.Version = (AesVersion)versionByte[0]; header.IV = iv; return(header); }
public static async Task <AesHeader> ReadAsync(Stream stream, CancellationToken ct) { var versionByte = new byte[sizeof(AesVersion)]; var iv = new byte[16]; await stream.ReadAsync(versionByte.AsMemory(), ct).ConfigureAwait(false); await stream.ReadAsync(iv.AsMemory(), ct).ConfigureAwait(false); var header = new AesHeader(); header.Version = (AesVersion)versionByte[0]; header.IV = iv; return(header); }