private string DecryptString(MemoryStream text, string pass) { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { using (MemoryStream a = new System.IO.MemoryStream()) { byte[] r = pgp.Decrypt(text.ToArray(), File.OpenRead("Sample_Pri.asc"), pass); MemoryStream ms = new MemoryStream(r); StreamReader reader = new StreamReader(ms); return(reader.ReadToEnd()); } } }
private static void Decrypt() { using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { using (Stream input = File.OpenRead("SampleData.PGP")) { using (Stream output = File.OpenWrite("SampleData.OUT")) { pgp.Decrypt(input, output, "Sample_Pri.asc", "Test123"); } } Console.WriteLine("PGP Decryption done."); } }
public async Task <(FileCrypt fileDetails, MemoryStream memoryStream)> DecryptAndDownloadFileAsync(long fileCryptId) { MemoryStream memoryStream = new MemoryStream(); MemoryStream decryptMemoryStream = new MemoryStream(); Assembly asm = Assembly.GetExecutingAssembly(); string path = System.IO.Path.GetDirectoryName(asm.Location); string privateKeyFileFullPath = path + @"\" + GlobalAppConfigurations.Instance.GetValue("PGPPrivateKeyFileFullPath").ToString(); string publicKeyFileFullPath = path + @"\" + GlobalAppConfigurations.Instance.GetValue("PGPPublicKeyFileFullPath").ToString(); string pgpKeyPassword = GlobalAppConfigurations.Instance.GetValue("PGPKeyPassword").ToString(); FileCrypt fileCrypt = await this._fileCryptRepository.GetEncryptedFileDetailsAsync(fileCryptId); await this._fileCryptRepository.SaveFileDecryptionHistoryAsync(fileCrypt); using (var stream = new FileStream(fileCrypt.EncryptedFileFullPath, FileMode.Open)) { await stream.CopyToAsync(memoryStream); } memoryStream.Position = 0; using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt()) { try { pgp.Decrypt(memoryStream, decryptMemoryStream, privateKeyFileFullPath, pgpKeyPassword); } catch (Exception ex) { throw ex; } } decryptMemoryStream.Position = 0; return(fileCrypt, decryptMemoryStream); }