public string UnwrapAndDecrypt(WrappedValue wrap) { var key = AsymEncryption.Decryptor(PrivateKey).Decrypt(wrap.EncryptedAESKey); var value = SymEncryption.GetWith(key, wrap.IV).Decrypt(wrap.EncryptedValue); return(Encoding.UTF8.GetString(value)); }
public WrappedValue EncryptAndWrap(string value) { var valueAsBytes = Encoding.UTF8.GetBytes(value); // generate AES key and iv byte[] key = new byte[SymetricKeySize], iv = new byte[16]; secureRandom.NextBytes(key); secureRandom.NextBytes(iv); // encrypt all the things var encryptedKey = AsymEncryption.Encryptor(PublicKey).Encrypt(key); var encryptedValue = SymEncryption.GetWith(key, iv).Encrypt(valueAsBytes); return(new WrappedValue { EncryptedAESKey = encryptedKey, IV = iv, EncryptedValue = encryptedValue }); }