132 lines
5.7 KiB
C#
132 lines
5.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using LCU.Api.Internal.FileOperations.Services.FileActionServices;
|
|
using LCU.Library.Internal.FileOperationsModels.Models;
|
|
using LCU.Library.Internal.LCUServiceFactory.Configurations;
|
|
using Org.BouncyCastle.Bcpg;
|
|
using Org.BouncyCastle.Bcpg.OpenPgp;
|
|
using Org.BouncyCastle.Crypto;
|
|
using Org.BouncyCastle.Crypto.Generators;
|
|
using Org.BouncyCastle.Crypto.Prng;
|
|
using Org.BouncyCastle.Security;
|
|
|
|
namespace LCU.API.Internal.FileOperations.IntegrationTests
|
|
{
|
|
class FileDataEncryptionTests
|
|
{
|
|
private const string TestPassword = "password";
|
|
private const string PrivateKeyFile = "TestPrivateKey";
|
|
private const string PublicKeyFile = "TestPublicKey";
|
|
private const string TestFile = "TestTextFile.txt";
|
|
private const string TestFileContents = "This text will be saved to a temporary test file for encryption/comparison to resulting files from this integration test";
|
|
private const string TestEncryptedFile = "TestTextFile.pgp";
|
|
private const string TestDecryptedFile = "TestTextFile-Decrypted";
|
|
private readonly string _basePath;
|
|
private readonly EncryptFileDataService _encryptFileDataService;
|
|
private readonly DecryptFileDataService _decryptFileDataService;
|
|
private readonly ServicesFactoryConfiguration _servicesFactoryConfiguration;
|
|
|
|
FileDataEncryptionTests()
|
|
{
|
|
_encryptFileDataService = new EncryptFileDataService(_servicesFactoryConfiguration);
|
|
_decryptFileDataService = new DecryptFileDataService(_servicesFactoryConfiguration);
|
|
_basePath = Path.Combine(Directory.GetCurrentDirectory(), "TempFileFolder");
|
|
Directory.CreateDirectory(_basePath);
|
|
GenerateTestKeys("[email protected]", TestPassword, Path.Combine(_basePath, PrivateKeyFile), Path.Combine(_basePath, PublicKeyFile));
|
|
File.WriteAllText(Path.Combine(_basePath, TestFile), TestFileContents);
|
|
}
|
|
|
|
static void Main()
|
|
{
|
|
var fileDataEncryptionTests = new FileDataEncryptionTests();
|
|
|
|
var encryptFileDataRequest = new EncryptFileDataRequest
|
|
{
|
|
SourcePath = fileDataEncryptionTests._basePath,
|
|
SourceFile = TestFile,
|
|
KeySourcePath = fileDataEncryptionTests._basePath,
|
|
KeySourceFile = PublicKeyFile,
|
|
DestinationPath = fileDataEncryptionTests._basePath
|
|
};
|
|
|
|
var decryptFileDataRequest = new DecryptFileDataRequest
|
|
{
|
|
SourcePath = fileDataEncryptionTests._basePath,
|
|
SourceFile = TestEncryptedFile,
|
|
KeySourcePath = fileDataEncryptionTests._basePath,
|
|
KeySourceFile = PrivateKeyFile,
|
|
DestinationPath = fileDataEncryptionTests._basePath,
|
|
DestinationFile = TestDecryptedFile,
|
|
PassPhrase = TestPassword
|
|
};
|
|
|
|
var encryptResult = fileDataEncryptionTests._encryptFileDataService.PerformRequest(encryptFileDataRequest);
|
|
var decryptResult = fileDataEncryptionTests._decryptFileDataService.PerformRequest(decryptFileDataRequest);
|
|
|
|
Console.WriteLine($"Encrypted Request Is Valid: {encryptResult.IsSuccessful}");
|
|
Console.WriteLine($"Decrypted Request Is Valid: {decryptResult.IsSuccessful}");
|
|
|
|
var encryptedFileContents = File.ReadAllText(Path.Combine(fileDataEncryptionTests._basePath, TestEncryptedFile));
|
|
var decryptedFileContents = File.ReadAllText(Path.Combine(fileDataEncryptionTests._basePath, TestDecryptedFile));
|
|
|
|
Console.WriteLine($"Test File Contents: {TestFileContents}");
|
|
Console.WriteLine($"Encrypted File Contents: {encryptedFileContents}");
|
|
Console.WriteLine($"Decrypted File Contents: {decryptedFileContents}");
|
|
|
|
Console.WriteLine("Complete");
|
|
}
|
|
|
|
private static void GenerateTestKeys(string identity, string passPhrase, string secretKeyOutputPath, string publicKeyOutputPath)
|
|
{
|
|
var keyPairGenerator = new RsaKeyPairGenerator();
|
|
keyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), 1024));
|
|
var keyPair = keyPairGenerator.GenerateKeyPair();
|
|
|
|
using var secretOut = new FileInfo(secretKeyOutputPath).OpenWrite();
|
|
using var publicOut = new FileInfo(publicKeyOutputPath).OpenWrite();
|
|
ExportGeneratedKeyPair(secretOut, publicOut, keyPair.Public, keyPair.Private, identity, passPhrase.ToCharArray(), true);
|
|
}
|
|
|
|
private static void ExportGeneratedKeyPair(
|
|
Stream secretOut,
|
|
Stream publicOut,
|
|
AsymmetricKeyParameter publicKey,
|
|
AsymmetricKeyParameter privateKey,
|
|
string identity,
|
|
char[] passPhrase,
|
|
bool armor)
|
|
{
|
|
if (armor)
|
|
{
|
|
secretOut = new ArmoredOutputStream(secretOut);
|
|
}
|
|
|
|
var pgpSecretKey = new PgpSecretKey
|
|
(
|
|
PgpSignature.DefaultCertification,
|
|
PublicKeyAlgorithmTag.RsaGeneral,
|
|
publicKey,
|
|
privateKey,
|
|
DateTime.Now,
|
|
identity,
|
|
SymmetricKeyAlgorithmTag.Cast5,
|
|
passPhrase,
|
|
null,
|
|
null,
|
|
new SecureRandom()
|
|
);
|
|
|
|
pgpSecretKey.Encode(secretOut);
|
|
secretOut.Close();
|
|
|
|
if (armor)
|
|
{
|
|
publicOut = new ArmoredOutputStream(publicOut);
|
|
}
|
|
|
|
var pgpPublicKey = pgpSecretKey.PublicKey;
|
|
pgpPublicKey.Encode(publicOut);
|
|
publicOut.Close();
|
|
}
|
|
}
|
|
} |