Exemplo n.º 1
0
            public string Transform(IConsole console, string data)
            {
                if (_password != null)
                {
                    var alg = KeyManagementAlgorithm.Pbes2HS256A128KW;
                    var enc = EncryptionAlgorithm.A128CbcHS256;
                    console.Verbose(
                        $@"Encrypting the JWK...
Algorithm: {alg}
Encryption algorithm: {enc}
Password derivation iteration count: {_iterationCount}
Password derivation salt size: {_saltSize} bits");
                    var encryptionKey = PasswordBasedJwk.FromPassphrase(_password, iterationCount: _iterationCount, saltSizeInBytes: _saltSize);
                    var writer        = new JwtWriter();
                    var descriptor    = new PlaintextJweDescriptor(encryptionKey, alg, enc)
                    {
                        Payload = data
                    };

                    console.Verbose("JWK encrypted.");
                    return(writer.WriteTokenString(descriptor));
                }

                return(data);
            }
Exemplo n.º 2
0
        static void Main()
        {
            // Creates a symmetric key for encryption
            var encryptionKey = SymmetricJwk.FromBase64Url("R9MyWaEoyiMYViVWo8Fk4T");

            // Creates a JWE descriptor with all its properties
            var descriptor = new PlaintextJweDescriptor(encryptionKey, KeyManagementAlgorithm.A128KW, EncryptionAlgorithm.A128CbcHS256)
            {
                Payload = "Life long and prosper."
            };

            // Generates the UTF-8 string representation of the JWT
            var writer = new JwtWriter();
            var token  = writer.WriteTokenString(descriptor);

            Console.WriteLine("The JWT is:");
            Console.WriteLine(descriptor);
            Console.WriteLine();
            Console.WriteLine("Its compact form is:");
            Console.WriteLine(token);
        }
Exemplo n.º 3
0
        public void Write_Utf8ToEscape()
        {
            var plaintext = "Live long and prosper!€";

            var descriptor = new PlaintextJweDescriptor(plaintext);

            descriptor.EncryptionKey       = RsaKey;
            descriptor.EncryptionAlgorithm = EncryptionAlgorithm.Aes128CbcHmacSha256;
            descriptor.Algorithm           = KeyManagementAlgorithm.RsaPkcs1;

            JwtWriter writer = new JwtWriter();
            var       value  = writer.WriteToken(descriptor);

            var reader = new JwtReader(RsaKey);
            var result = reader.TryReadToken(value, TokenValidationPolicy.NoValidation);

            Assert.Equal(TokenValidationStatus.Success, result.Status);

            var jwt = result.Token;

            Assert.Equal(plaintext, jwt.Plaintext);
        }
        public void Write_Utf8ToEscape()
        {
            var plaintext = "Live long and prosper!€";

            var descriptor = new PlaintextJweDescriptor(RsaKey, KeyManagementAlgorithm.Rsa1_5, EncryptionAlgorithm.A128CbcHS256);

            descriptor.Payload = plaintext;

            JwtWriter writer = new JwtWriter();
            var       value  = writer.WriteToken(descriptor);

            var policy = new TokenValidationPolicyBuilder()
                         .WithDecryptionKey(RsaKey)
                         .IgnoreSignatureByDefault()
                         .Build();

            var result = Jwt.TryParse(value, policy, out var jwt);

            Assert.True(result);

            Assert.Equal(plaintext, jwt.Plaintext);
        }
Exemplo n.º 5
0
        static void Main()
        {
            // Creates a symmetric key for encryption
            var encryptionKey = new SymmetricJwk("R9MyWaEoyiMYViVWo8Fk4T");

            // Creates a JWE descriptor with all its properties
            var payload    = "Life long and prosper.hello.world";
            var descriptor = new PlaintextJweDescriptor(payload)
            {
                EncryptionKey       = encryptionKey,
                EncryptionAlgorithm = EncryptionAlgorithm.Aes128CbcHmacSha256,
                Algorithm           = KeyManagementAlgorithm.Aes128KW,
            };

            // Generates the UTF-8 string representation of the JWT
            var writer = new JwtWriter();
            var token  = writer.WriteTokenString(descriptor);

            Console.WriteLine("The JWT is:");
            Console.WriteLine(descriptor);
            Console.WriteLine();
            Console.WriteLine("Its compact form is:");
            Console.WriteLine(token);
        }