private static string EncryptPrivateKey(UserKeyPair keys, Passphrase passphrase) { if (keys.KeyPair.PrivateKey == null) { return(String.Empty); } byte[] privateKeyPemBytes = Encoding.UTF8.GetBytes(keys.KeyPair.PrivateKey.ToString()); if (passphrase == Passphrase.Empty) { byte[] encryptedPrivateKeyBytes = New <IProtectedData>().Protect(privateKeyPemBytes, null); return(Convert.ToBase64String(encryptedPrivateKeyBytes)); } StringBuilder encryptedPrivateKey = new StringBuilder(); using (StringWriter writer = new StringWriter(encryptedPrivateKey)) { using (Stream stream = new MemoryStream(privateKeyPemBytes)) { EncryptionParameters encryptionParameters = new EncryptionParameters(Resolve.CryptoFactory.Preferred.CryptoId, passphrase); EncryptedProperties properties = new EncryptedProperties("private-key.pem"); using (MemoryStream encryptedStream = new MemoryStream()) { AxCryptFile.Encrypt(stream, encryptedStream, properties, encryptionParameters, AxCryptOptions.EncryptWithCompression, new ProgressContext()); writer.Write(Convert.ToBase64String(encryptedStream.ToArray())); } } } return(encryptedPrivateKey.ToString()); }
public static void TestLargeEncryptDecrypt() { string sourceFullName = _davidCopperfieldTxtPath; IRuntimeFileInfo sourceRuntimeFileInfo = OS.Current.FileInfo(sourceFullName); IRuntimeFileInfo destinationRuntimeFileInfo = sourceRuntimeFileInfo.CreateEncryptedName(); Passphrase passphrase = new Passphrase("laDabled@tAmeopot33"); AxCryptFile.Encrypt(sourceRuntimeFileInfo, destinationRuntimeFileInfo, passphrase, AxCryptOptions.SetFileTimes | AxCryptOptions.EncryptWithCompression, new ProgressContext()); Assert.That(destinationRuntimeFileInfo.CreationTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.CreationTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(destinationRuntimeFileInfo.LastAccessTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.LastAccessTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(destinationRuntimeFileInfo.LastWriteTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.LastWriteTimeUtc), "We're expecting file times to be set as the original from the headers."); DirectoryInfo decryptedDirectoryInfo = new DirectoryInfo(_rootPath.PathCombine("Destination", "Decrypted.txt")); string decryptedFullName = Path.Combine(decryptedDirectoryInfo.FullName, "David Copperfield.txt"); IRuntimeFileInfo decryptedRuntimeFileInfo = OS.Current.FileInfo(decryptedFullName); AxCryptFile.Decrypt(destinationRuntimeFileInfo, decryptedRuntimeFileInfo, passphrase.DerivedPassphrase, AxCryptOptions.SetFileTimes, new ProgressContext()); Assert.That(decryptedRuntimeFileInfo.CreationTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.CreationTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedRuntimeFileInfo.LastAccessTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.LastAccessTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedRuntimeFileInfo.LastWriteTimeUtc, Is.EqualTo(sourceRuntimeFileInfo.LastWriteTimeUtc), "We're expecting file times to be set as the original from the headers."); using (StreamReader sourceStreamReader = new StreamReader(sourceRuntimeFileInfo.OpenRead(), Encoding.GetEncoding("Windows-1252"))) { using (StreamReader decryptedStreamReader = new StreamReader(decryptedRuntimeFileInfo.OpenRead(), Encoding.GetEncoding("Windows-1252"))) { string source = sourceStreamReader.ReadToEnd(); string decrypted = decryptedStreamReader.ReadToEnd(); Assert.That(decrypted, Is.EqualTo(source), "Comparing original plain text with the decrypted encrypted plain text."); } } }
public static void TestSmallEncryptDecrypt() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); Assert.That(destinationFileInfo.Name, Is.EqualTo("test-txt.axx"), "Wrong encrypted file name based on the plain text file name."); AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); using (AxCryptDocument document = AxCryptFile.Document(destinationFileInfo, new Passphrase("axcrypt").DerivedPassphrase, new ProgressContext())) { Assert.That(document.PassphraseIsValid, Is.True, "The passphrase should be ok."); Assert.That(document.DocumentHeaders.FileName, Is.EqualTo("test.txt"), "Unexpected file name in headers."); Assert.That(document.DocumentHeaders.CreationTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate1Utc)); Assert.That(document.DocumentHeaders.LastAccessTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate2Utc)); Assert.That(document.DocumentHeaders.LastWriteTimeUtc, Is.EqualTo(FakeRuntimeFileInfo.TestDate3Utc)); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted test.txt")); AxCryptFile.Decrypt(document, decryptedFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); using (Stream decryptedStream = decryptedFileInfo.OpenRead()) { string decrypted = new StreamReader(decryptedStream, Encoding.UTF8).ReadToEnd(); Assert.That(decrypted, Is.EqualTo("This is a short file")); } Assert.That(decryptedFileInfo.CreationTimeUtc, Is.EqualTo(document.DocumentHeaders.CreationTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedFileInfo.LastAccessTimeUtc, Is.EqualTo(document.DocumentHeaders.LastAccessTimeUtc), "We're expecting file times to be set as the original from the headers."); Assert.That(decryptedFileInfo.LastWriteTimeUtc, Is.EqualTo(document.DocumentHeaders.LastWriteTimeUtc), "We're expecting file times to be set as the original from the headers."); } }
public async Task EncryptAsync(Stream clearIn, Stream encryptedOut, string fileName) { EncryptionParameters parameters = new EncryptionParameters(_cryptoId, _passphrase); await parameters.AddAsync(_publicKeys); EncryptedProperties properties = new EncryptedProperties(fileName); AxCryptFile.Encrypt(clearIn, encryptedOut, properties, parameters, _options, new ProgressContext()); }
public static void TestInvalidArguments() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); AxCryptDocument document = new AxCryptDocument(); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted test.txt")); AxCryptDocument nullDocument = null; IRuntimeFileInfo nullFileInfo = null; AesKey nullKey = null; ProgressContext nullProgress = null; Passphrase nullPassphrase = null; Stream nullStream = null; string nullString = null; Action <Stream> nullStreamAction = null; Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, nullPassphrase, AxCryptOptions.EncryptWithCompression, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, destinationFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(nullFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, nullStream, new AesKey(), AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), nullKey, AxCryptOptions.None, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Encrypt(sourceFileInfo, new MemoryStream(), new AesKey(), AxCryptOptions.None, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullDocument, decryptedFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, nullFileInfo, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(document, decryptedFileInfo, AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, decryptedFileInfo, new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(nullFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, nullString, new AesKey(), AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), nullKey, AxCryptOptions.SetFileTimes, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Decrypt(sourceFileInfo, Path.Combine(_rootPath, "Directory"), new AesKey(), AxCryptOptions.SetFileTimes, nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(nullFileInfo, new AesKey(), new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, nullKey, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Document(sourceFileInfo, new AesKey(), nullProgress); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(null, (Stream stream) => { }, new ProgressContext()); }); IRuntimeFileInfo fileInfo = OS.Current.FileInfo(_testTextPath); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.WriteToFileWithBackup(fileInfo, nullStreamAction, new ProgressContext()); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.MakeAxCryptFileName(nullFileInfo); }); Assert.Throws <ArgumentNullException>(() => { AxCryptFile.Wipe(nullFileInfo, new ProgressContext()); }); }
public static void TestInvalidPassphrase() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo encryptedFileInfo = sourceFileInfo.CreateEncryptedName(); Assert.That(encryptedFileInfo.Name, Is.EqualTo("test-txt.axx"), "Wrong encrypted file name based on the plain text file name."); AxCryptFile.Encrypt(sourceFileInfo, encryptedFileInfo, new Passphrase("axcrypt"), AxCryptOptions.EncryptWithCompression, new ProgressContext()); IRuntimeFileInfo decryptedFileInfo = OS.Current.FileInfo(Path.Combine(_rootPath, "decrypted.txt")); bool isPassphraseOk = AxCryptFile.Decrypt(encryptedFileInfo, decryptedFileInfo, new Passphrase("wrong").DerivedPassphrase, AxCryptOptions.None, new ProgressContext()); Assert.That(isPassphraseOk, Is.False, "The passphrase is wrong and should be wrong!"); }
private static byte[] GetSaveDataForKeys(UserKeyPair keys, string originalFileName, Passphrase passphrase) { string json = Resolve.Serializer.Serialize(keys); using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { EncryptionParameters encryptionParameters = new EncryptionParameters(Resolve.CryptoFactory.Preferred.CryptoId, passphrase); EncryptedProperties properties = new EncryptedProperties(originalFileName); using (MemoryStream exportStream = new MemoryStream()) { AxCryptFile.Encrypt(stream, exportStream, properties, encryptionParameters, AxCryptOptions.EncryptWithCompression, new ProgressContext()); return(exportStream.ToArray()); } } }
public static void TestEncryptToStream() { IRuntimeFileInfo sourceFileInfo = OS.Current.FileInfo(_testTextPath); IRuntimeFileInfo destinationFileInfo = sourceFileInfo.CreateEncryptedName(); Assert.That(destinationFileInfo.Name, Is.EqualTo("test-txt.axx"), "Wrong encrypted file name based on the plain text file name."); using (Stream destinationStream = destinationFileInfo.OpenWrite()) { AxCryptFile.Encrypt(sourceFileInfo, destinationStream, new Passphrase("axcrypt").DerivedPassphrase, AxCryptOptions.EncryptWithCompression, new ProgressContext()); } using (AxCryptDocument document = AxCryptFile.Document(destinationFileInfo, new Passphrase("axcrypt").DerivedPassphrase, new ProgressContext())) { Assert.That(document.PassphraseIsValid, Is.True, "The passphrase should be ok."); } }
private static ActiveFile CheckIfTimeToUpdate(ActiveFile activeFile, ProgressContext progress) { if (!activeFile.Status.HasMask(ActiveFileStatus.AssumedOpenAndDecrypted) || activeFile.Status.HasMask(ActiveFileStatus.NotShareable)) { return(activeFile); } if (activeFile.Key == null) { return(activeFile); } if (!activeFile.IsModified) { return(activeFile); } try { using (Stream activeFileStream = activeFile.DecryptedFileInfo.OpenRead()) { AxCryptFile.WriteToFileWithBackup(activeFile.EncryptedFileInfo, (Stream destination) => { AxCryptFile.Encrypt(activeFile.DecryptedFileInfo, destination, activeFile.Key, AxCryptOptions.EncryptWithCompression, progress); }, progress); } } catch (IOException) { if (OS.Log.IsWarningEnabled) { OS.Log.LogWarning("Failed exclusive open modified for '{0}'.".InvariantFormat(activeFile.DecryptedFileInfo.FullName)); } activeFile = new ActiveFile(activeFile, activeFile.Status | ActiveFileStatus.NotShareable); return(activeFile); } if (OS.Log.IsInfoEnabled) { OS.Log.LogInfo("Wrote back '{0}' to '{1}'".InvariantFormat(activeFile.DecryptedFileInfo.FullName, activeFile.EncryptedFileInfo.FullName)); } activeFile = new ActiveFile(activeFile, activeFile.DecryptedFileInfo.LastWriteTimeUtc, ActiveFileStatus.AssumedOpenAndDecrypted); return(activeFile); }