public IActionResult Index() { var identityRsaCert3072 = CreateRsaCertificates.CreateRsaCertificate(_createCertificates, 3072); var publicKeyPem = _importExportCertificate.PemExportPublicKeyCertificate(identityRsaCert3072); var privateKeyPem = _importExportCertificate.PemExportRsaPrivateKey(identityRsaCert3072); var dicEntity = new EncryptDemo(); dicEntity.PublicKey = publicKeyPem; dicEntity.PrivateKey = privateKeyPem; Mock.dics.Add("demo", dicEntity); //string temp = $"public key:{publicKeyPem}, private key:{privateKeyPem}"; #region 加密 var(Key, IVBase64) = _symmetricEncryptDecrypt.InitSymmetricEncryptionKeyIV(); var encryptedText = _symmetricEncryptDecrypt.Encrypt(_origin, IVBase64, Key); var targetUserPublicCertificate = _importExportCertificate.PemImportCertificate(publicKeyPem); var encryptedKey = _asymmetricEncryptDecrypt.Encrypt(Key, Utils.CreateRsaPublicKey(targetUserPublicCertificate)); var encryptedIV = _asymmetricEncryptDecrypt.Encrypt(IVBase64, Utils.CreateRsaPublicKey(targetUserPublicCertificate)); var encryptedDto = new EncryptedDto { EncryptedText = encryptedText, Key = encryptedKey, IV = encryptedIV }; #endregion #region 解密 var certWithPublicKey = _importExportCertificate.PemImportCertificate(publicKeyPem); var privateKey = _importExportCertificate.PemImportPrivateKey(privateKeyPem); var cert = _importExportCertificate.CreateCertificateWithPrivateKey( certWithPublicKey, privateKey); var key = _asymmetricEncryptDecrypt.Decrypt(encryptedDto.Key, Utils.CreateRsaPrivateKey(cert)); var IV = _asymmetricEncryptDecrypt.Decrypt(encryptedDto.IV, Utils.CreateRsaPrivateKey(cert)); var text = _symmetricEncryptDecrypt.Decrypt(encryptedDto.EncryptedText, IV, key); #endregion return(Content(text)); }
public IActionResult OnPost() { if (!ModelState.IsValid) { // Something failed. Redisplay the form. return(OnGet()); } var(Key, IVBase64) = _symmetricEncryptDecrypt.InitSymmetricEncryptionKeyIV(); var encryptedText = _symmetricEncryptDecrypt.Encrypt(Message, IVBase64, Key); var targetUserPublicCertificate = GetCertificateWithPublicKeyForIdentity(TargetUserEmail); var encryptedKey = _asymmetricEncryptDecrypt.Encrypt(Key, Utils.CreateRsaPublicKey(targetUserPublicCertificate)); var encryptedIV = _asymmetricEncryptDecrypt.Encrypt(IVBase64, Utils.CreateRsaPublicKey(targetUserPublicCertificate)); var encryptedDto = new EncryptedDto { EncryptedText = encryptedText, Key = encryptedKey, IV = encryptedIV }; string jsonString = JsonSerializer.Serialize(encryptedDto); EncryptedMessage = $"{jsonString}"; // Redisplay the form. return(OnGet()); }
static void Main(string[] args) { var text = "I have a big dog. You've got a cat. We all love animals!"; Console.WriteLine("-- Encrypt Decrypt symmetric --"); Console.WriteLine(""); var symmetricEncryptDecrypt = new SymmetricEncryptDecrypt(); var(Key, IVBase64) = symmetricEncryptDecrypt.InitSymmetricEncryptionKeyIV(); var encryptedText = symmetricEncryptDecrypt.Encrypt(text, IVBase64, Key); Console.WriteLine("-- Key --"); Console.WriteLine(Key); Console.WriteLine("-- IVBase64 --"); Console.WriteLine(IVBase64); Console.WriteLine(""); Console.WriteLine("-- Encrypted Text --"); Console.WriteLine(encryptedText); var decryptedText = symmetricEncryptDecrypt.Decrypt(encryptedText, IVBase64, Key); Console.WriteLine("-- Decrypted Text --"); Console.WriteLine(decryptedText); }