public DecryptedData DecryptData(EncryptedPacket encryptedPacket, int keyLength, RSAEncryption rsaParams, DigitalSignature digitalSignature, string pathToRecipientPrivateKey, string pathToSenderPublicKey) { var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey, keyLength, pathToRecipientPrivateKey); using (var hmac = new HMACSHA256(decryptedSessionKey)) { var hmacToCheck = hmac.ComputeHash(encryptedPacket.EncryptedData); if (!Compare(encryptedPacket.Hmac, hmacToCheck)) { throw new CryptographicException("HMAC for decryption does not match encrypted packet."); } if (!digitalSignature.VerifySignature(encryptedPacket.Hmac, encryptedPacket.Signature, keyLength, pathToSenderPublicKey)) { throw new CryptographicException("Digital Signature can not be verified."); } } var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey, encryptedPacket.Iv); // Need to account for encryptedPacket.Filename being null here - in case a user is decrypting a packet created with v1.0.0 var originalFilename = encryptedPacket.Filename != null?_aes.Decrypt(encryptedPacket.Filename, decryptedSessionKey, encryptedPacket.Iv) : defaultFilename; return(new DecryptedData { FileContents = decryptedData, Filename = Encoding.ASCII.GetString(originalFilename), Version1Packet = encryptedPacket.Filename == null }); }
private static DecryptedData Decrypt(EncryptedPacket encryptedPacket, int keyLength, string sendersPublicKeyPath, string recipientsPrivateKeyPath) { var encryptor = new RSAEncryption(); var digitalSigner = new DigitalSignature(); var hybrid = new HybridEncryption(); var decryptedData = hybrid.DecryptData(encryptedPacket, keyLength, encryptor, digitalSigner, recipientsPrivateKeyPath, sendersPublicKeyPath); return(decryptedData); }
public EncryptedPacket EncryptData(byte[] original, int keyLength, RSAEncryption rsaParams, DigitalSignature digitalSignature, string pathToRecipientPublicKey, string pathToSenderPrivateKey, string filename) { var sessionKey = _aes.GenerateRandomNumber(32); var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) }; encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv); encryptedPacket.Filename = _aes.Encrypt(Encoding.ASCII.GetBytes(filename), sessionKey, encryptedPacket.Iv); encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey, keyLength, pathToRecipientPublicKey); using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } encryptedPacket.Signature = digitalSignature.SignData(encryptedPacket.Hmac, keyLength, pathToSenderPrivateKey); return(encryptedPacket); }