Я следовал коду в здесь , чтобы создать консольное приложение, которое шифрует несколько файлов одновременно.
С помощью приложения я шифрую большое количество файлов (макс. 4). Я заметил, что после 2-го или 3-го шифрования файла программа закрывается без ошибок. Это произошло как в среде Debug, так и в Release.
Я не мог понять почему. У кого-нибудь есть идеи?
Я реализовал вспомогательный класс поверх него, и он читается как:
namespace PgPSignAndEncryption.Helper
{
public class PgPHelperClass
{
private string JKSBPublicKey = PgPSignAndEncryptionForCiti.Properties.Settings.Default.PgPPublicKeyPath;
private string JKSBPrivateKey = PgPSignAndEncryptionForCiti.Properties.Settings.Default.PgPPrivateKeyPath;
private string JKSBSecretKey = "MySecretKey";
private string _encryptedFileName = string.Empty;
private string _decryptedFileName = string.Empty;
public string EncryptedFileName
{
get { return _encryptedFileName; }
set { _encryptedFileName = value; }
}
public string DecryptedFileName
{
get { return _decryptedFileName; }
set { _decryptedFileName = value; }
}
public void KeyGeneration()
{
#region PublicKey and Private Key Generation
PgPSignAndEncryption.KeyGeneration.KeysForPGPEncryptionDecryption.GenerateKey("Me", "MySecretKeys", @"C:\Keys\");
//Console.WriteLine("Keys Generated Successfully");
#endregion
}
public void Encryption()
{
try
{
#region PGP Encryption
PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(this.JKSBPublicKey, this.JKSBPrivateKey, this.JKSBSecretKey);
PgpEncrypt encrypter = new PgpEncrypt(encryptionKeys);
var path = System.IO.Path.GetDirectoryName(this.DecryptedFileName);
var fileName = System.IO.Path.GetFileNameWithoutExtension(this.DecryptedFileName);
var ext = System.IO.Path.GetExtension(this.DecryptedFileName);
this.EncryptedFileName = string.Format("{0}\\{1}{2}.gpg", path, fileName, ext);
using (Stream outputStream = File.Create(this.EncryptedFileName))
{
encrypter.EncryptAndSign(outputStream, new FileInfo(this.DecryptedFileName));
}
//EncryptedFileName = this.EncryptedFileName;
#endregion
} //<<<<<<<<<------ It crashes after this point!
catch (Exception er)
{
EncryptedFileName = er.Message;
}
}
public void Decryption(out string DecryptedFileName)
{
try
{
#region PGP Decryption
var path = System.IO.Path.GetDirectoryName(this.EncryptedFileName);
var fileName = System.IO.Path.GetFileNameWithoutExtension(this.EncryptedFileName);
var ext = string.Empty;
//try
//{
// var aExt = fileName.Split('.');
// fileName = aExt.First();
// ext = aExt.Last();
//}
//catch (Exception)
//{
// ext = System.IO.Path.GetExtension(this.EncryptedFileName);
//}
this.DecryptedFileName = string.Format("{0}\\{1}.out", path, fileName);
PGPDecrypt.Decrypt(this.EncryptedFileName, this.JKSBPrivateKey, this.JKSBSecretKey, this.DecryptedFileName);
DecryptedFileName = this.DecryptedFileName;
#endregion
}
catch (Exception er)
{
DecryptedFileName = er.Message;
}
}
}
}
Это то, как я вызываю в моем консольном приложении
private static void BulkFileEncryption()
{
if (Directory.Exists(PickUpFolderPath))
{
var files = Directory.GetFiles(DropFolderPath);
foreach (var file in files)
{
//Encryption and Decryption
helper.DecryptedFileName = file;
helper.Encryption();
Console.WriteLine(string.Format("File Encrypted: {0}", file));
}
}
}