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 async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var identityRsaCert2048 = CreateRsaCertificates.CreateRsaCertificate(_createCertificates, 2048); var publicKeyPem = _importExportCertificate.PemExportPublicKeyCertificate(identityRsaCert2048); var privateKeyPem = _importExportCertificate.PemExportRsaPrivateKey(identityRsaCert2048); var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, PemPrivateKey = privateKeyPem, PemPublicKey = publicKeyPem }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }