예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var serviceProvider = new ServiceCollection()
                                  .AddCertificateManager()
                                  .BuildServiceProvider();

            var cc = serviceProvider.GetService <CreateCertificates>();

            var cert2048 = CreateRsaCertificates.CreateRsaCertificate(cc, 2048);

            var text = "I have a big dog. You've got a cat. We all love animals!";

            Console.WriteLine("-- Encrypt Decrypt asymmetric --");
            Console.WriteLine("");

            var asymmetricEncryptDecrypt = new AsymmetricEncryptDecrypt();

            var encryptedText = asymmetricEncryptDecrypt.Encrypt(text,
                                                                 Utils.CreateRsaPublicKey(cert2048));

            Console.WriteLine("");
            Console.WriteLine("-- Encrypted Text --");
            Console.WriteLine(encryptedText);

            var decryptedText = asymmetricEncryptDecrypt.Decrypt(encryptedText,
                                                                 Utils.CreateRsaPrivateKey(cert2048));

            Console.WriteLine("-- Decrypted Text --");
            Console.WriteLine(decryptedText);
        }
예제 #2
0
        public ApiResponse Encrypt()
        {
            var dto = new EncryptDemo();

            var identityRsaCert3072 = CreateRsaCertificates.CreateRsaCertificate(_createCertificates, 3072);
            var publicKeyPem        = _importExportCertificate.PemExportPublicKeyCertificate(identityRsaCert3072);
            var privateKeyPem       = _importExportCertificate.PemExportRsaPrivateKey(identityRsaCert3072);

            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));

            dto.PublicKey     = publicKeyPem;
            dto.PrivateKey    = privateKeyPem;
            dto.Key           = encryptedKey;
            dto.IV            = encryptedIV;
            dto.EncryptedText = encryptedText;


            return(new ApiResponse(dto, StatusCodes.Status200OK));
        }
예제 #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var identityRsaCert3072 = CreateRsaCertificates.CreateRsaCertificate(_createCertificates, 3072);
                var publicKeyPem        = _importExportCertificate.PemExportPublicKeyCertificate(identityRsaCert3072);
                var privateKeyPem       = _importExportCertificate.PemExportPfxFullCertificate(identityRsaCert3072,
                                                                                               _configuration["PemPasswordExportImport"]);

                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());
        }
예제 #4
0
        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));
        }