/// <summary> /// Extracts the archive. /// </summary> /// <param name="extractionLocation">The extraction location.</param> /// <param name="fullPathToArchive">The full path to archive.</param> /// <param name="crypto">The crypto.</param> /// <returns> /// An <see cref="OperationResult" /> containing the result of /// the operation. /// </returns> /// <externalUnit/> /// <revision revisor="dev13" date="11/17/2009" version="1.1.3.5"> /// Added documentation header /// </revision> public static OperationResult ExtractArchive( string extractionLocation, string fullPathToArchive, SequoiaCryptoProvider crypto) { OperationResult result = new OperationResult(); using (var sa = new EncryptedArchive( extractionLocation, fullPathToArchive, crypto)) { byte[] decryptedBytes = sa.Decrypt( fullPathToArchive); if (decryptedBytes != null) { sa.OpenRead(decryptedBytes); result = sa.ExtractAllFiles(false); } else { result.Succeeded = false; result.Details = "Failed to decrypt archive."; } } return(result); }
/// <summary> /// Extracts the archive to zip. /// </summary> /// <param name="extractionLocation">The extraction location.</param> /// <param name="fullPathToArchive">The full path to archive.</param> /// <param name="crypto">The crypto.</param> /// <returns> /// An <see cref="OperationResult" /> containing the results /// of the operation. /// </returns> /// <externalUnit/> /// <revision revisor="dev13" date="11/17/2009" version="1.1.3.5"> /// Added documentation header /// </revision> public static OperationResult ExtractArchiveToZip( string extractionLocation, string fullPathToArchive, SequoiaCryptoProvider crypto) { OperationResult result = new OperationResult(); string decryptedArchiveName = Path.ChangeExtension( Path.GetFileName(fullPathToArchive), "zip"); using (var sa = new EncryptedArchive( extractionLocation, decryptedArchiveName, crypto)) { bool decrypted = sa.DecryptToFile( fullPathToArchive, Path.Combine(extractionLocation, decryptedArchiveName), crypto); if (decrypted) { sa.OpenRead(); result = sa.ExtractAllFiles(false); } else { result.Succeeded = false; result.Details = "Failed to decrypt archive."; } } return(result); }
/// <summary> /// Creates the signature. /// </summary> /// <param name="fileName">Name of the file.</param> /// <externalUnit/> /// <revision revisor="dev13" date="2/27/2009" version="1.0.8.1001"> /// Member Created /// </revision> /// <revision revisor="dev05" date="03/02/09" version="1.0.8.13"> /// Pass full pathname to SequoiaCryptoProvider. /// </revision> /// <revision revisor="dev05" date="03/06/09" version="1.0.8.17"> /// Tabulator private key now lives in "PrivateKey.xml". /// </revision> private void CreateSignature(string fileName) { // create signature var sequoiaCryptoProvider = new SequoiaCryptoProvider(); sequoiaCryptoProvider.CreateSignature( Path.Combine(this.pathToFile, fileName)); }
/// <summary> /// Initializes a new instance of the <see cref="EncryptedArchive"/> class. /// </summary> /// <param name="dataPath">The data path.</param> /// <param name="fileName">Name of the file.</param> /// <param name="crypto">The crypto.</param> /// <externalUnit/> /// <revision revisor="dev06" date="4/16/2009" version="1.0.11.6"> /// Member Created /// </revision> public EncryptedArchive( string dataPath, string fileName, SequoiaCryptoProvider crypto) { this.dataPath = dataPath; this.archiveName = Path.Combine(dataPath, fileName); this.sequoiaProvider = crypto; }
/// <summary> /// Initializes a new instance of the <see cref="SignedArchive"/> class. /// </summary> /// <param name="dataPath">Path where archive and files live.</param> /// <param name="fileName">Archive file name, without path.</param> /// <param name="shortKey">whether or not to use the short key.</param> /// <externalUnit/> /// <revision revisor="dev05" date="03/06/09" version="1.0.8.17"> /// Method created. /// </revision> public SignedArchive( string dataPath, string fileName, bool shortKey) { this.dataPath = dataPath; this.archiveName = Path.Combine(dataPath, fileName); this.sequoiaProvider = new SequoiaCryptoProvider(shortKey); }
/// <summary> /// Gets the public key. /// </summary> /// <param name="shortKey">if set to <c>true</c> [short key].</param> /// <returns>A string containing the public key</returns> /// <externalUnit/> /// <revision revisor="dev01" date="4/23/2009" version="1.0.11.10"> /// Member Created /// </revision> public static string GetPublicKey(bool shortKey) { string publicKey = string.Empty; using (var crypto = new SequoiaCryptoProvider(shortKey)) { publicKey = crypto.GetPrivateKey(); } return(publicKey); }
/// <summary> /// Creates the encrypted archive. /// </summary> /// <param name="selectedFolder">The selected folder.</param> /// <param name="archiveName">Name of the archive.</param> /// <externalUnit/> /// <revision revisor="dev13" date="11/18/2009" version="1.1.3.6"> /// Added documentation header /// </revision> private void CreateEncryptedArchive( string selectedFolder, string archiveName) { try { string publicKey = string.Empty; string archiveFileName = string.IsNullOrEmpty(archiveName) ? "TestEncryptedArchive" : archiveName; string fullArchiveFilename = archiveFileName + ".zip"; using (var encryptedArchive = new EncryptedArchive( selectedFolder, fullArchiveFilename, new SequoiaCryptoProvider())) { encryptedArchive.OpenWrite(DateTime.Now, true); foreach ( string pathName in Directory.GetFiles(selectedFolder)) { string fileName = System.IO.Path.GetFileName(pathName); if (fileName == fullArchiveFilename) { continue; } // encryptedArchive.AddFile(fileName); byte[] fileData = File.ReadAllBytes(pathName); encryptedArchive.AddFile(fileData, fileName); } } var sequoiaProvider = new SequoiaCryptoProvider(); sequoiaProvider.CreateSignature( Path.Combine( selectedFolder, Path.ChangeExtension(fullArchiveFilename, "enc"))); } catch (Exception exception) { MessageBox.Show( string.Format( "Error creating encrypted archive: {0}", exception.Message), "Encrypted Archive Creation Failure", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Encrypts the specified string to Encrypt. /// </summary> /// <param name="bytesToEncrypt">The string to Encrypt.</param> /// <param name="shortKey">if set to <c>true</c> [short key].</param> /// <returns> /// A <see cref="EncryptedItem"/> containing details about /// the Encrypted string. /// </returns> /// <externalUnit cref="EncryptedItem"/> /// <externalUnit cref="SequoiaCryptoProvider"/> /// <revision revisor="dev01" date="1/28/2009" version="1.0.5.9"> /// Member Created /// </revision> public static EncryptedItem Encrypt( byte[] bytesToEncrypt, bool shortKey) { // create parm for return var encryptedItem = new EncryptedItem(); using (var crypto = new SequoiaCryptoProvider(shortKey)) { encryptedItem.Data = crypto.Encrypt( bytesToEncrypt); } return(encryptedItem); }
/// <summary> /// DeEncryptes the specified Encrypted item. /// </summary> /// <param name="encryptedItem">The Encrypted item.</param> /// <param name="shortKey">if set to <c>true</c> [short key].</param> /// <returns> /// The unencrypted value of the Encrypted data. /// </returns> /// <externalUnit cref="EncryptedItem"/> /// <externalUnit cref="SequoiaCryptoProvider"/> /// <revision revisor="dev01" date="1/28/2009" version="1.0.5.9"> /// Member Created /// </revision> public static byte[] Decrypt( EncryptedItem encryptedItem, bool shortKey) { // Review: should this be in Try/catch block? // Create return param byte[] results = null; using (var crypto = new SequoiaCryptoProvider(shortKey)) { results = crypto.Decrypt(encryptedItem.Data); } return(results); }
public void TestforIOExceptionWithNoOverrideOption() { string testPath = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\TestData\Encryption"); string publicKey = File.ReadAllText(Path.Combine(testPath, "PublicKey.xml")); string privateKey = File.ReadAllText(Path.Combine(testPath, "PrivateKey.xml")); SequoiaCryptoProvider pro = new SequoiaCryptoProvider(false); string originalFile = Path.Combine( testPath, "Results.xml"); string encryptedFile = Path.Combine( testPath, "Results.xml.enc"); string decryptedFile = Path.Combine( testPath, "DecryptedResults.xml"); pro.EncryptFile( originalFile, encryptedFile, publicKey, false); Assert.IsTrue(File.Exists(encryptedFile)); pro = new SequoiaCryptoProvider(false); pro.DecryptFile( encryptedFile, decryptedFile, privateKey, true); pro.DecryptFile( encryptedFile, decryptedFile, privateKey, false); }
public void WrongDataHashTest() { byte[] encryptedData = null; string testString = "普選正式選票2008年11月4日,星期二伊利諾州芝加哥市"; string testPath = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\TestData\Encryption"); string publicKey = File.ReadAllText(Path.Combine(testPath, "PublicKey.xml")); var plainData = Encoding.Unicode.GetBytes(testString); var crypto = new SequoiaCryptoProvider(); var hasher = new SHA512Managed(); var hash = hasher.ComputeHash(plainData); // Change one byte of the hash plainData[0] = (byte)0; //Calculate the hash and append it to the data byte[] dataPlusHash = SequoiaCryptoProvider.AppendArrays( hash, plainData); // Symmetrically Encrypt data and hash with random key and IV byte[] encryptedDataPlusHash = crypto.SymmetricallyEncryptContent(dataPlusHash); // Create an Encrypted transport Key var transKey = new TransportKey(crypto); // Add transport Key to ecnrypted data encryptedData = SequoiaCryptoProvider.AppendArrays( transKey.GenerateTransportKey(publicKey), encryptedDataPlusHash); EncryptedItem item = new EncryptedItem(); item.Data = encryptedData; var result = EncryptionHelper.Decrypt(item, false); }
public void TestSignatureUsingEmbeddedKeys() { this.CleanTempPath(true); this.PopulateTempPath(); //this.MakeEncryptedArchive(rsa); this.MakeEncryptedArchive(); // should now have archive.enc and archive.enc.sig at temppath // let's verify the sig file var crypto = new SequoiaCryptoProvider(false); string pathToArchive = Path.Combine(this.tempPath, "archive.enc"); string pathToSig = Path.Combine(this.tempPath, "archive.enc.sig"); bool verified = crypto.VerifySignature(pathToSig, pathToArchive); Assert.IsTrue(verified); }
/// <summary> /// Verifies the archive signature. /// </summary> /// <param name="selectedArchivePath">The selected archive path.</param> /// <externalUnit/> /// <revision revisor="dev13" date="11/18/2009" version="1.1.3.6"> /// Added documentation header /// </revision> private void VerifyArchiveSignature(string selectedArchivePath) { string pathToSigFile = selectedArchivePath + ".sig"; if (!File.Exists(pathToSigFile)) { MessageBox.Show( "Please select a valid file which has a signature file " + "of the same name, plus '.sig.'", "No signature file present", MessageBoxButton.OK, MessageBoxImage.Error); } else { var sequoiaCryptoProvider = new SequoiaCryptoProvider(); bool verified = sequoiaCryptoProvider.VerifySignature( pathToSigFile, selectedArchivePath); if (verified) { MessageBox.Show( "The file matches the signature!", "File Signature Test", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show( "the file does NOT match the signature.", "File Signature Test", MessageBoxButton.OK, MessageBoxImage.Error); } } }
/// <summary> /// Decrypts to file. /// </summary> /// <param name="fullPathToArchive">The full path to archive.</param> /// <param name="pathToDecryptedFile">The path to decrypted file.</param> /// <param name="crypto">The crypto.</param> /// <returns> /// <c>true</c> if decrypted; otherwise, <c>false</c>. /// </returns> /// <externalUnit/> /// <revision revisor="dev13" date="11/17/2009" version="1.1.3.5"> /// Added documentation header /// </revision> private bool DecryptToFile( string fullPathToArchive, string pathToDecryptedFile, SequoiaCryptoProvider crypto) { bool decrypted = false; try { // decrypt bytes so we have the zip stream again this.sequoiaProvider.DecryptFile( fullPathToArchive, pathToDecryptedFile, crypto.GetPrivateKey(), true); decrypted = File.Exists(pathToDecryptedFile); } catch (Exception) { } return(decrypted); }