/// <summary> /// Same function as EncryptMessage, but signs the message first /// </summary> /// <param name="message">Message to be encrypted</param> /// <param name="publicKeyString">Public key of the recipient</param> /// <param name="password">Password needed to use the user's private key for the signature process</param> /// <returns> /// The formatted encrypted message with all the non-secure data added to it, in addition to the digital /// signature /// </returns> public static string EncryptAndSignMessage(string message, string publicKeyString, string password) { string sign; RSAParameters key = KeyVault.RetrieveRSAParams(password); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(key); using (StringWriter sw = new StringWriter()) { sw.WriteLine(signatureHeader); sw.WriteLine(Convert.ToBase64String(DigitalSignature.SignMessage(key, message))); sw.WriteLine(signatureTail); sw.WriteLine(pubKeyHeader); sw.WriteLine(KeyVault.SerializeRSAParams(rsa.ExportParameters(false))); sw.WriteLine(pubKeyTail); sign = sw.ToString(); } return(EncryptMessage(message, publicKeyString) + sign); }
public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams, DigitalSignature digitalSignature) { var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey); using (var hmac = new HMACSHA256(decryptedSessionKey)) { var hmacToCheck = hmac.ComputeHash(encryptedPacket.EncryptedData); if (!Compare(encryptedPacket.Hmac, hmacToCheck)) { throw new CryptographicException("HMAC for encryption does not match with encrypted packet"); } if (!digitalSignature.VerifySignature(encryptedPacket.Hmac, hmacToCheck)) { throw new CryptographicException("Digital Signature can not be verified"); } } var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey, encryptedPacket.Iv); return(decryptedData); }
/// <summary> /// Decrypts a given message using the user's password-protected private key /// </summary> /// <param name="cipherMessage">The message to be decrypted</param> /// <param name="password">The user's password used to retrieve the user's private key</param> /// <returns>Decrypted message</returns> public static string DecryptMessage(string cipherMessage, string password) { bool isSigned = false; string encryptedCryptKey; string encryptedAuthKey; string message; string signature; string pubKey = null; using (StringReader sr = new StringReader(cipherMessage)) { string line; while ((line = sr.ReadLine()) != null) { if (line == greeting) { break; } } while ((line = sr.ReadLine()) != null) { if (line == cryptKeyHeader) { break; } } encryptedCryptKey = sr.ReadLine(); while ((line = sr.ReadLine()) != null) { if (line == authKeyHeader) { break; } } encryptedAuthKey = sr.ReadLine(); while ((line = sr.ReadLine()) != null) { if (line == messageHeader) { break; } } message = sr.ReadLine(); while ((line = sr.ReadLine()) != null) { if (line == signatureHeader) { isSigned = true; break; } } signature = sr.ReadLine(); while ((line = sr.ReadLine()) != null) { if (line == pubKeyHeader) { while ((line = sr.ReadLine()) != pubKeyTail) { pubKey += line; } } } } while (encryptedAuthKey.Length % 4 != 0) { encryptedAuthKey += "="; } while (signature != null && signature.Length % 4 != 0) { signature += "="; } while (encryptedCryptKey.Length % 4 != 0) { encryptedCryptKey += "="; } while (message.Length % 4 != 0) { message += "="; } RSAParameters keys = KeyVault.RetrieveRSAParams(password); byte[] encryptedCryptKeyBytes = Convert.FromBase64String(encryptedCryptKey); byte[] encryptedAuthKeyBytes = Convert.FromBase64String(encryptedAuthKey); byte[] cryptKey = RSAEncryption.DecryptRSA(encryptedCryptKeyBytes, keys); byte[] authKey = RSAEncryption.DecryptRSA(encryptedAuthKeyBytes, keys); string plainTextMessage = AESThenHMAC.Decrypt(message, cryptKey, authKey); if (isSigned) { RSAParameters pubkey = KeyVault.DeserializeRSAParams(pubKey); if (!DigitalSignature.VerifySignature(pubkey, plainTextMessage, signature)) { string warning = "WARNING: this message appears to have a wrong digital signature.\r\n"; plainTextMessage = warning + plainTextMessage; } else { string success = "This message is digitally signed properly.\r\n"; plainTextMessage = success + plainTextMessage; } } return(plainTextMessage); }
public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams, DigitalSignature signature) { var sessionKey = _aes.GenerateRandomNumber(32); var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) }; encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv); encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey); using (var hmac = new HMACSHA256(sessionKey)) { encryptedPacket.Hmac = hmac.ComputeHash(encryptedPacket.EncryptedData); } encryptedPacket.Signature = signature.SignData(encryptedPacket.Hmac); return(encryptedPacket); }