示例#1
0
        public async Task <string> EncryptAsync(string keyId, byte[] bytesToEncrypt)
        {
            AesKey aesKey = await _keyStore.GetKeyAsync(keyId) ?? await _keyStore.CreateKeyAsync(keyId);

            Byte[] bkey = Encoding.UTF8.GetBytes(aesKey.Key);
            Byte[] bIV  = Encoding.UTF8.GetBytes(aesKey.IV);

            Byte[] encryptData = null; // encrypted data

            using (Aes Aes = Aes.Create())
            {
                using (MemoryStream Memory = new MemoryStream())
                {
                    using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bkey, bIV), CryptoStreamMode.Write))
                    {
                        Encryptor.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);
                        Encryptor.FlushFinalBlock();
                        encryptData = Memory.ToArray();
                    }
                }

                string base64Enc = Convert.ToBase64String(encryptData);
                return(base64Enc);
            }
        }
示例#2
0
        public async Task CreateKeyAsync_should_add_key_to_cache()
        {
            Guid keyId = Guid.NewGuid();

            await _sut.CreateKeyAsync(keyId.ToString());

            Assert.Equal(1, GetCacheCount());
        }
示例#3
0
        public async Task <T> CreateKeyAsync(string keyId)
        {
            T key = await _keystore.CreateKeyAsync(keyId);

            byte[] keyInBytes = MessagePackSerializer.Typeless.Serialize(key);

            try
            {
                await _cache.SetAsync(keyId, keyInBytes);
            }
            catch (Exception)
            {
            }

            return(key);
        }
示例#4
0
        public async Task <string> EncryptAsync(string keyId, byte[] bytesToEncrypt)
        {
            AsymmetricCipherKeyPair keys = await _keyStore.GetKeyAsync(keyId) ?? await _keyStore.CreateKeyAsync(keyId);

            SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keys.Public);

            RsaKeyParameters publicKey = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKeyInfo);

            using (RSA rsaa = RSA.Create())
            {
                RSAParameters rsaParameters = new RSAParameters();
                rsaParameters.Modulus  = publicKey.Modulus.ToByteArrayUnsigned();
                rsaParameters.Exponent = publicKey.Exponent.ToByteArrayUnsigned();
                rsaa.ImportParameters(rsaParameters);

                byte[] enc       = rsaa.Encrypt(bytesToEncrypt, _rsaEncryptionPadding);
                string base64Enc = Convert.ToBase64String(enc);
                return(base64Enc);
            }
        }