Esempio n. 1
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));
        }
Esempio n. 2
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));
        }
Esempio n. 3
0
        public ApiResponse Decrypt(EncryptDemo dto)
        {
            var certWithPublicKey = _importExportCertificate.PemImportCertificate(dto.PublicKey);
            var privateKey        = _importExportCertificate.PemImportPrivateKey(dto.PrivateKey);

            var cert = _importExportCertificate.CreateCertificateWithPrivateKey(
                certWithPublicKey, privateKey);

            var key = _asymmetricEncryptDecrypt.Decrypt(dto.Key,
                                                        Utils.CreateRsaPrivateKey(cert));

            var IV = _asymmetricEncryptDecrypt.Decrypt(dto.IV,
                                                       Utils.CreateRsaPrivateKey(cert));

            var text = _symmetricEncryptDecrypt.Decrypt(dto.EncryptedText, IV, key);

            return(new ApiResponse("New record has been created in the database", text, StatusCodes.Status201Created));
        }
Esempio n. 4
0
 static void EncryptFile()
 {
     EncryptDemo.T();
 }