示例#1
0
        private static string Decrypt(Part[] parts, object key)
        {
            Part header       = parts[0];
            Part encryptedCek = parts[1];
            Part iv           = parts[2];
            Part cipherText   = parts[3];
            Part authTag      = parts[4];

            var jwtHeader = JsonObject.Parse(header.Utf8);
            var alg       = jwtHeader["alg"].GetString();
            var enc       = jwtHeader["enc"].GetString();

            if (!encryptors.ContainsKey(enc))
            {
                throw new Exception(string.Format("JoseRT.Jwt.Decrypt(): unknown or unsupported encryption:{0}.", enc));
            }

            if (!algorithms.ContainsKey(alg))
            {
                throw new Exception(string.Format("JoseRT.Jwt.Decrypt(): unknown or unsupported algorithm:{0}.", alg));
            }

            IJwaAlgorithm keys       = algorithms[alg];
            IJweEncryptor encryption = encryptors[enc];

            byte[] cek = keys.Unwrap(encryptedCek.Bytes, key, encryption.KeySize, jwtHeader);           //TODO part?

            byte[] aad = Encoding.UTF8.GetBytes(Compact.Serialize(header));                             //TODO: Part.New(Compact...)

            byte[] plainText = encryption.Decrypt(aad, cek, iv.Bytes, cipherText.Bytes, authTag.Bytes); //TODO: all parts, return part?

            return(Encoding.UTF8.GetString(plainText, 0, plainText.Length));
        }
示例#2
0
        public static string Encode(string payload, string keyManagementAlg, string encryption, object key)
        {
            Ensure.IsNotEmpty(payload, "JoseRT.Jwt.Encode(): payload expected to be not empty, whitespace or null.");

            if (!encryptors.ContainsKey(encryption))
            {
                throw new Exception(string.Format("JoseRT.Jwt.Encode(): unknown or unsupported encryption algorithm:{0}.", encryption));
            }

            if (!algorithms.ContainsKey(keyManagementAlg))
            {
                throw new Exception(string.Format("JoseRT.Jwt.Encode(): unknown or unsupported key management algorithm:{0}.", keyManagementAlg));
            }

            IJweEncryptor encryptor = encryptors[encryption];
            IJwaAlgorithm algorithm = algorithms[keyManagementAlg];

            var jwtHeader = new JsonObject
            {
                { "enc", JsonValue.CreateStringValue(encryption) },
                { "alg", JsonValue.CreateStringValue(keyManagementAlg) }
            };

            Part[] keys         = algorithm.WrapNewKey(encryptor.KeySize, key, jwtHeader);
            Part   cek          = keys[0];
            Part   encryptedCek = keys[1];
            Part   header       = Part.New(jwtHeader.Stringify());

            byte[] plainText = Encoding.UTF8.GetBytes(payload);

            byte[] aad      = Encoding.UTF8.GetBytes(Compact.Serialize(header));
            Part[] encParts = encryptor.Encrypt(aad, plainText, cek.Bytes);

            return(Compact.Serialize(header, encryptedCek, encParts[0], encParts[1], encParts[2]));
        }
示例#3
0
 public static void RegisterJwa(IJwaAlgorithm algorithm)
 {
     algorithms[algorithm.Name] = algorithm;
 }