public static long Process( Stream data, ArchiveType archiveType, CompressionType compType, EncryptionProtocol encProtocol, EncryptionAlgorithm encAlgorithm) { string fileName = Path.GetTempFileName(); FileInfo fileInfo = new FileInfo(fileName); long compSizeB = 0; ProgressStreamReportDelegate progressCallback = (_, args) => compSizeB += args.BytesMoved; using (Stream outFileStream = fileInfo.OpenWrite()) { Action <Stream> streamWriter = (outStream) => CompressionHelper.Compress( "data.bin", data, outStream, archiveType, compType, progressCallback ); switch (encProtocol) { case EncryptionProtocol.None: streamWriter(outFileStream); break; case EncryptionProtocol.AES: EncryptionHelper.EncryptStreamAES(streamWriter, outFileStream, encAlgorithm, Const.EncKey32); break; case EncryptionProtocol.PBE: EncryptionHelper.EncryptStreamPBE(streamWriter, outFileStream, encAlgorithm, Const.EncKey32); break; case EncryptionProtocol.PGP: using (Stream pgpPubKeyStream = new MemoryStream(Encoding.ASCII.GetBytes(Const.PGPPubKey))) EncryptionHelper.EncryptStreamPGP( streamWriter, outFileStream, encAlgorithm, pgpPubKeyStream, Convert.ToInt64(Const.PGPPubKeyID, 16)); break; } } fileInfo.Delete(); return(compSizeB); }
public static IEnumerable <EncryptionAlgorithm> GetAvailableAlgorithms(this EncryptionProtocol proto) { switch (proto) { case EncryptionProtocol.None: return(new List <EncryptionAlgorithm>() { EncryptionAlgorithm.None }); case EncryptionProtocol.AES: return(new List <EncryptionAlgorithm>() { EncryptionAlgorithm.AES128_CBC, //EncryptionAlgorithm.AES128_CFB, EncryptionAlgorithm.AES256_CBC, //EncryptionAlgorithm.AES256_CFB, }); case EncryptionProtocol.PBE: return(new List <EncryptionAlgorithm>() { EncryptionAlgorithm.SHA1_TripleDes_CBC, EncryptionAlgorithm.SHA1_Twofish_CBC, EncryptionAlgorithm.SHA256_AES128_CBC, EncryptionAlgorithm.SHA256_AES256_CBC, }); case EncryptionProtocol.PGP: return(new List <EncryptionAlgorithm>() { EncryptionAlgorithm.AES128_CFB, EncryptionAlgorithm.AES256_CFB, EncryptionAlgorithm.Blowfish_CFB, EncryptionAlgorithm.Camellia128_CFB, EncryptionAlgorithm.Camellia256_CFB, EncryptionAlgorithm.Cast5_CFB, EncryptionAlgorithm.TripleDes_CFB, EncryptionAlgorithm.Twofish_CFB, }); default: throw new ArgumentException(String.Format("Unsupported EncryptionAlgorithm {0}", proto), "proto"); } }
// // Tar.* | AES | AES128-CBC //[BenchmarkCategory("Tar", "NoComp", "AES", "NoEnc")] //[Benchmark] //public void TarPlainAES128CBC() => Process(ArchiveType.Tar, CompressionType.None, EncryptionProtocol.None, EncryptionAlgorithm.None); //[BenchmarkCategory("Tar", "BZip2", "AES", "NoEnc")] //[Benchmark] //public void TarBZip2AES128CBC() => Process(ArchiveType.Tar, CompressionType.BZip2, EncryptionProtocol.None, EncryptionAlgorithm.None); //[BenchmarkCategory("Tar", "GZip", "AES", "NoEnc")] //[Benchmark] //public void TarGZipAES128CBC() => Process(ArchiveType.Tar, CompressionType.GZip, EncryptionProtocol.None, EncryptionAlgorithm.None); //[BenchmarkCategory("Tar", "LZip", "AES", "NoEnc")] //[Benchmark] //public void TarLZipAES128CBC() => Process(ArchiveType.Tar, CompressionType.LZip, EncryptionProtocol.None, EncryptionAlgorithm.None); private void Process( ArchiveType archiveType, CompressionType compType, EncryptionProtocol encProtocol, EncryptionAlgorithm encAlgorithm) { string fileName = Path.GetTempFileName(); FileInfo fileInfo = new FileInfo(fileName); using (Stream outFileStream = fileInfo.OpenWrite()) { Action <Stream> streamWriter = (outStream) => CompressionHelper.Compress( "data.bin", Data, outStream, archiveType, compType, null ); switch (encProtocol) { case EncryptionProtocol.None: streamWriter(outFileStream); break; case EncryptionProtocol.AES: EncryptionHelper.EncryptStreamAES(streamWriter, outFileStream, encAlgorithm, EncKey); break; case EncryptionProtocol.PBE: EncryptionHelper.EncryptStreamPBE(streamWriter, outFileStream, encAlgorithm, EncKey); break; } } fileInfo.Delete(); }
public static Dictionary <string, object> EncryptDynamicData(Dictionary <string, DynamicDataDescription> template, Dictionary <string, object> inputs, EncryptionProtocol protocol, string encryptionKey, bool usePublicKey = true) { if (inputs == null) { return(null); } Dictionary <string, object> decryptedData = new Dictionary <string, object>(); var toLoweredDefinitions = template.ToDictionary(entry => entry.Key.ToLower(), entry => entry.Value); foreach (var input in inputs) { if (toLoweredDefinitions[input.Key.ToLower()].Type == InputDataTypes.Secret)//&& !InputDataUtility.IsInputReference(input, out _, out _)) { switch (protocol) { case EncryptionProtocol.AES256: decryptedData.Add(input.Key, SecurityUtility.SymmetricallyEncrypt((string)input.Value, encryptionKey)); break; case EncryptionProtocol.RSA: if (usePublicKey) { decryptedData.Add(input.Key, SecurityUtility.RsaEncryptWithPublic((string)input.Value, encryptionKey)); } else { decryptedData.Add(input.Key, SecurityUtility.RsaEncryptWithPrivate((string)input.Value, encryptionKey)); } break; default: throw new InvalidEncryptionProtocolException(); } } else { decryptedData.Add(input.Key, input.Value); } } return(decryptedData); }