//Decrypt the envelope and return the output array. public byte[] DecryptEnvelope() { //Create the RSAWrapper from a string representation of its name. rsaAlgorithm = new RSAWrapper(rsaName); //Initialize the RSA algorithm key. rsaAlgorithm.RSAalg.FromXmlString(rsaKey); //Create the CipherMode from a string representation of its name. cipherMode = HelperExtensions.StringToCipherMode(cipherName); symmetricAlgorithm = HelperExtensions.StringToSymAlg(symmetricAlgorithmName, cipherMode, cipherName); symmetricAlgorithm.algorithm.IV = initializationVector; symmetricAlgorithm.algorithm.Key = rsaAlgorithm.RSADecrypt(DigitalEnvelopeEncrypted.EncryptedSymmetricKey, true); return(symmetricAlgorithm.Decrypt(DigitalEnvelopeEncrypted.EncryptedMessage)); }
//Return a SymmetricAlgorithmWrapper object given the symmetric algorithm name (including key size), CipherMode and cipher name. public static SymmetricAlgorithmWrapper StringToSymAlg(string input, CipherMode cipherMode, string cipherName) { SymmetricAlgorithmWrapper symmetricAlgorithm = input switch { "AES 128" => new SymmetricAlgorithmWrapper("Aes", cipherName, 128, cipherMode), "AES 192" => new SymmetricAlgorithmWrapper("Aes", cipherName, 192, cipherMode), "AES 256" => new SymmetricAlgorithmWrapper("Aes", cipherName, 256, cipherMode), "TripleDES 128" => new SymmetricAlgorithmWrapper("TripleDES", cipherName, 128, cipherMode), "TripleDES 192" => new SymmetricAlgorithmWrapper("TripleDES", cipherName, 192, cipherMode), _ => new SymmetricAlgorithmWrapper("Aes", cipherName, 128, cipherMode), }; return(symmetricAlgorithm); }
//Create a DigitalEnvelope instance which will be used to create an envelope. public DigitalEnvelope(SymmetricAlgorithmWrapper SYMAlgorithm, RSAWrapper rsaAlgorithm, string inputFilePath) { this.symmetricAlgorithm = SYMAlgorithm; this.rsaAlgorithm = rsaAlgorithm; LoadInputFile(inputFilePath); }