Exemplo de Script - Criptografar Arquivo com PGP Aberto
Este plugin C# criptografa um arquivo com criptografia Open PGP. "ascFile" é uma sequência de caracteres com o caminho completo e o nome do arquivo ASC (deve ser um arquivo fornecido para criptografia).
Caso de Uso
Isso pode ser usado quando a empresa tem um arquivo que compartilha com terceiros via MFT/SFTP e exige que ele seja criptografado para segurança adicional durante a transmissão para proteger dados PII.
Referências de Plugins
Coluna | Tipo de dados | Descrição |
---|---|---|
ascFile | String | caminho completo e nome do arquivo ASC |
InFile | String | caminho completo e nome do arquivo a ser criptografado |
OutFile | String | caminho completo e nome do arquivo desejado para o arquivo de saída criptografado (normalmente {{InFile}}||'.pgp' ou {{InFile}}||'.gpg' |
Abrir Script de Criptografia de Arquivo PGP
#r "BouncyCastle.Crypto.dll"
using System;
using System.Text;
using System.Collections;
using System.IO;
using System.Linq;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Asn1.Utilities;
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.Encoders;
// This is the path to the ASC file
string ascFile = @"c:\PGP.asc";
// Script expects 2 cells in the row action
// "InFile" - full path to file to read (unencrypted file)
// "Outfile" - full path to file to write (encrypted file)
string inFile = Row["InFile"].Value.ToString();
string outFile = Row["OutFile"].Value.ToString();
// The following is from Org.BouncyCastle.Bcpg.OpenPgp.Examples.KeyBasedFileProcessor
EncryptFile(outFile,inFile,ascFile,armor: true,withIntegrityCheck: false);
string GetAlgorithm(
PublicKeyAlgorithmTag algId)
{
switch (algId)
{
case PublicKeyAlgorithmTag.RsaGeneral:
return "RsaGeneral";
case PublicKeyAlgorithmTag.RsaEncrypt:
return "RsaEncrypt";
case PublicKeyAlgorithmTag.RsaSign:
return "RsaSign";
case PublicKeyAlgorithmTag.ElGamalEncrypt:
return "ElGamalEncrypt";
case PublicKeyAlgorithmTag.Dsa:
return "DSA";
case PublicKeyAlgorithmTag.ECDH:
return "ECDH";
case PublicKeyAlgorithmTag.ECDsa:
return "ECDSA";
case PublicKeyAlgorithmTag.ElGamalGeneral:
return "ElGamalGeneral";
case PublicKeyAlgorithmTag.DiffieHellman:
return "DiffieHellman";
}
return "unknown";
}
void EncryptFile(
string outputFileName,
string inputFileName,
string encKeyFileName,
bool armor,
bool withIntegrityCheck)
{
PgpPublicKey encKey = ReadPublicKey(encKeyFileName);
using (Stream output = File.Create(outputFileName))
{
EncryptFilePGP(output, inputFileName, encKey, armor, withIntegrityCheck);
}
}
void EncryptFilePGP(
Stream outputStream,
string fileName,
PgpPublicKey encKey,
bool armor,
bool withIntegrityCheck)
{
if (armor)
{
outputStream = new ArmoredOutputStream(outputStream);
}
try
{
byte[] bytes = CompressFile(fileName, CompressionAlgorithmTag.Zip);
PgpEncryptedDataGenerator encGen = new PgpEncryptedDataGenerator(
SymmetricKeyAlgorithmTag.Cast5, withIntegrityCheck, new SecureRandom());
encGen.AddMethod(encKey);
Stream cOut = encGen.Open(outputStream, bytes.Length);
cOut.Write(bytes, 0, bytes.Length);
cOut.Close();
if (armor)
{
outputStream.Close();
}
}
catch (PgpException e)
{
Console.Error.WriteLine(e);
Exception underlyingException = e.InnerException;
if (underlyingException != null)
{
Console.Error.WriteLine(underlyingException.Message);
Console.Error.WriteLine(underlyingException.StackTrace);
}
}
}
PgpPublicKey ReadPublicKey(string fileName)
{
using (Stream keyIn = File.OpenRead(fileName))
{
return ReadPublicKey(keyIn);
}
}
PgpPublicKey ReadPublicKey(Stream input)
{
PgpPublicKeyRingBundle pgpPub = new PgpPublicKeyRingBundle(
PgpUtilities.GetDecoderStream(input));
//
// we just loop through the collection till we find a key suitable for encryption, in the real
// world you would probably want to be a bit smarter about this.
//
foreach (PgpPublicKeyRing keyRing in pgpPub.GetKeyRings())
{
foreach (PgpPublicKey key in keyRing.GetPublicKeys())
{
if (key.IsEncryptionKey)
{
return key;
}
}
}
throw new ArgumentException("Can't find encryption key in key ring.");
}
byte[] CompressFile(string fileName, CompressionAlgorithmTag algorithm)
{
MemoryStream bOut = new MemoryStream();
PgpCompressedDataGenerator comData = new PgpCompressedDataGenerator(algorithm);
PgpUtilities.WriteFileToLiteralData(comData.Open(bOut), PgpLiteralData.Binary,
new FileInfo(fileName));
comData.Close();
return bOut.ToArray();
}