Encrypting JWT security token supported algorithms












2















I'm trying to sign and encode my JWt with this snippet:



var claims = new Claim { new SomeClaimes() };
var scKey = Encoding.UTF8.GetBytes("SOME KEY");
var ecKey = Encoding.UTF8.GetBytes("SOME OTHER KEY");
var tokenDescriptor = new SecurityTokenDescriptor {
Subject = new ClaimsIdentity(claims),
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(
scKey),
SecurityAlgorithms.HmacSha512),
EncryptingCredentials = new EncryptingCredentials(
new SymmetricSecurityKey(
ecKey),
// I tryied all possible combination of algorithms here:
SecurityAlgorithms.XXXX,
SecurityAlgorithms.YYYY),
Issuer = "My Jwt Issuer",
Audience = "My Jwt Audience",
IssuedAt = DateTime.UtcNow,
Expires = DateTime.Now.AddDays(7),
};
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
var jwt = tokenHandler.WriteToken(token);


But when I run the code, I get error:




Encryption failed. No support for: Algorithm: '{0}', SecurityKey: '{1}'.




Which {0} and {1} are any combination of XXXX and YYYY in the code above (yes, I wrote a reflection snippet and have tried all possible combination of them). Which are supported algorithms for encoding (and decoding) a signed JWT?










share|improve this question



























    2















    I'm trying to sign and encode my JWt with this snippet:



    var claims = new Claim { new SomeClaimes() };
    var scKey = Encoding.UTF8.GetBytes("SOME KEY");
    var ecKey = Encoding.UTF8.GetBytes("SOME OTHER KEY");
    var tokenDescriptor = new SecurityTokenDescriptor {
    Subject = new ClaimsIdentity(claims),
    SigningCredentials = new SigningCredentials(
    new SymmetricSecurityKey(
    scKey),
    SecurityAlgorithms.HmacSha512),
    EncryptingCredentials = new EncryptingCredentials(
    new SymmetricSecurityKey(
    ecKey),
    // I tryied all possible combination of algorithms here:
    SecurityAlgorithms.XXXX,
    SecurityAlgorithms.YYYY),
    Issuer = "My Jwt Issuer",
    Audience = "My Jwt Audience",
    IssuedAt = DateTime.UtcNow,
    Expires = DateTime.Now.AddDays(7),
    };
    var tokenHandler = new JwtSecurityTokenHandler();
    var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
    var jwt = tokenHandler.WriteToken(token);


    But when I run the code, I get error:




    Encryption failed. No support for: Algorithm: '{0}', SecurityKey: '{1}'.




    Which {0} and {1} are any combination of XXXX and YYYY in the code above (yes, I wrote a reflection snippet and have tried all possible combination of them). Which are supported algorithms for encoding (and decoding) a signed JWT?










    share|improve this question

























      2












      2








      2


      1






      I'm trying to sign and encode my JWt with this snippet:



      var claims = new Claim { new SomeClaimes() };
      var scKey = Encoding.UTF8.GetBytes("SOME KEY");
      var ecKey = Encoding.UTF8.GetBytes("SOME OTHER KEY");
      var tokenDescriptor = new SecurityTokenDescriptor {
      Subject = new ClaimsIdentity(claims),
      SigningCredentials = new SigningCredentials(
      new SymmetricSecurityKey(
      scKey),
      SecurityAlgorithms.HmacSha512),
      EncryptingCredentials = new EncryptingCredentials(
      new SymmetricSecurityKey(
      ecKey),
      // I tryied all possible combination of algorithms here:
      SecurityAlgorithms.XXXX,
      SecurityAlgorithms.YYYY),
      Issuer = "My Jwt Issuer",
      Audience = "My Jwt Audience",
      IssuedAt = DateTime.UtcNow,
      Expires = DateTime.Now.AddDays(7),
      };
      var tokenHandler = new JwtSecurityTokenHandler();
      var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
      var jwt = tokenHandler.WriteToken(token);


      But when I run the code, I get error:




      Encryption failed. No support for: Algorithm: '{0}', SecurityKey: '{1}'.




      Which {0} and {1} are any combination of XXXX and YYYY in the code above (yes, I wrote a reflection snippet and have tried all possible combination of them). Which are supported algorithms for encoding (and decoding) a signed JWT?










      share|improve this question














      I'm trying to sign and encode my JWt with this snippet:



      var claims = new Claim { new SomeClaimes() };
      var scKey = Encoding.UTF8.GetBytes("SOME KEY");
      var ecKey = Encoding.UTF8.GetBytes("SOME OTHER KEY");
      var tokenDescriptor = new SecurityTokenDescriptor {
      Subject = new ClaimsIdentity(claims),
      SigningCredentials = new SigningCredentials(
      new SymmetricSecurityKey(
      scKey),
      SecurityAlgorithms.HmacSha512),
      EncryptingCredentials = new EncryptingCredentials(
      new SymmetricSecurityKey(
      ecKey),
      // I tryied all possible combination of algorithms here:
      SecurityAlgorithms.XXXX,
      SecurityAlgorithms.YYYY),
      Issuer = "My Jwt Issuer",
      Audience = "My Jwt Audience",
      IssuedAt = DateTime.UtcNow,
      Expires = DateTime.Now.AddDays(7),
      };
      var tokenHandler = new JwtSecurityTokenHandler();
      var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
      var jwt = tokenHandler.WriteToken(token);


      But when I run the code, I get error:




      Encryption failed. No support for: Algorithm: '{0}', SecurityKey: '{1}'.




      Which {0} and {1} are any combination of XXXX and YYYY in the code above (yes, I wrote a reflection snippet and have tried all possible combination of them). Which are supported algorithms for encoding (and decoding) a signed JWT?







      c# .net-core jwt encryption-symmetric netcoreapp2.1






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 18:46









      javad amiryjavad amiry

      15.3k84698




      15.3k84698
























          2 Answers
          2






          active

          oldest

          votes


















          0














          HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.






          share|improve this answer



















          • 1





            Could you please be more detailed in your answer? Perhaps append some code sample?

            – rudolf_franek
            Nov 26 '18 at 20:06











          • The question is not about having one or more keys. Please read the question again.

            – javad amiry
            Nov 26 '18 at 20:07











          • i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

            – Mr. Mostafavi
            Nov 26 '18 at 20:13



















          0














          Finally I found the answer:



          var claims = new Claim { new SomeClaimes() };
          var scKey = Encoding.UTF8.GetBytes("SOME KEY");
          var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");

          // Note that the ecKey should have 256 / 8 length:
          byte ecKey = new byte[256 / 8];
          Array.Copy(ecKeyTemp, ecKey, 256 / 8);

          var tokenDescriptor = new SecurityTokenDescriptor {
          Subject = new ClaimsIdentity(claims),
          SigningCredentials = new SigningCredentials(
          new SymmetricSecurityKey(
          scKey),
          SecurityAlgorithms.HmacSha512),
          EncryptingCredentials = new EncryptingCredentials(
          new SymmetricSecurityKey(
          ecKey),
          SecurityAlgorithms.Aes256KW,
          SecurityAlgorithms.Aes256CbcHmacSha512),
          Issuer = "My Jwt Issuer",
          Audience = "My Jwt Audience",
          IssuedAt = DateTime.UtcNow,
          Expires = DateTime.Now.AddDays(7),
          };
          var tokenHandler = new JwtSecurityTokenHandler();
          var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
          var jwt = tokenHandler.WriteToken(token);


          As you ca see, using SecurityAlgorithms.Aes256KW as the key encryption algorithm and SecurityAlgorithms.Aes256CbcHmacSha512 as the encryption algorithm will do the job. Note that the key used to encryption algorithm should have 256 / 8 length.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487247%2fencrypting-jwt-security-token-supported-algorithms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.






            share|improve this answer



















            • 1





              Could you please be more detailed in your answer? Perhaps append some code sample?

              – rudolf_franek
              Nov 26 '18 at 20:06











            • The question is not about having one or more keys. Please read the question again.

              – javad amiry
              Nov 26 '18 at 20:07











            • i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

              – Mr. Mostafavi
              Nov 26 '18 at 20:13
















            0














            HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.






            share|improve this answer



















            • 1





              Could you please be more detailed in your answer? Perhaps append some code sample?

              – rudolf_franek
              Nov 26 '18 at 20:06











            • The question is not about having one or more keys. Please read the question again.

              – javad amiry
              Nov 26 '18 at 20:07











            • i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

              – Mr. Mostafavi
              Nov 26 '18 at 20:13














            0












            0








            0







            HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.






            share|improve this answer













            HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 20:02









            Mr. MostafaviMr. Mostafavi

            12




            12








            • 1





              Could you please be more detailed in your answer? Perhaps append some code sample?

              – rudolf_franek
              Nov 26 '18 at 20:06











            • The question is not about having one or more keys. Please read the question again.

              – javad amiry
              Nov 26 '18 at 20:07











            • i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

              – Mr. Mostafavi
              Nov 26 '18 at 20:13














            • 1





              Could you please be more detailed in your answer? Perhaps append some code sample?

              – rudolf_franek
              Nov 26 '18 at 20:06











            • The question is not about having one or more keys. Please read the question again.

              – javad amiry
              Nov 26 '18 at 20:07











            • i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

              – Mr. Mostafavi
              Nov 26 '18 at 20:13








            1




            1





            Could you please be more detailed in your answer? Perhaps append some code sample?

            – rudolf_franek
            Nov 26 '18 at 20:06





            Could you please be more detailed in your answer? Perhaps append some code sample?

            – rudolf_franek
            Nov 26 '18 at 20:06













            The question is not about having one or more keys. Please read the question again.

            – javad amiry
            Nov 26 '18 at 20:07





            The question is not about having one or more keys. Please read the question again.

            – javad amiry
            Nov 26 '18 at 20:07













            i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

            – Mr. Mostafavi
            Nov 26 '18 at 20:13





            i am not c# specialist but HmacSha512 is not SymetricSecurityKey try new ASymmetricSecurityKey or something like that

            – Mr. Mostafavi
            Nov 26 '18 at 20:13













            0














            Finally I found the answer:



            var claims = new Claim { new SomeClaimes() };
            var scKey = Encoding.UTF8.GetBytes("SOME KEY");
            var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");

            // Note that the ecKey should have 256 / 8 length:
            byte ecKey = new byte[256 / 8];
            Array.Copy(ecKeyTemp, ecKey, 256 / 8);

            var tokenDescriptor = new SecurityTokenDescriptor {
            Subject = new ClaimsIdentity(claims),
            SigningCredentials = new SigningCredentials(
            new SymmetricSecurityKey(
            scKey),
            SecurityAlgorithms.HmacSha512),
            EncryptingCredentials = new EncryptingCredentials(
            new SymmetricSecurityKey(
            ecKey),
            SecurityAlgorithms.Aes256KW,
            SecurityAlgorithms.Aes256CbcHmacSha512),
            Issuer = "My Jwt Issuer",
            Audience = "My Jwt Audience",
            IssuedAt = DateTime.UtcNow,
            Expires = DateTime.Now.AddDays(7),
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
            var jwt = tokenHandler.WriteToken(token);


            As you ca see, using SecurityAlgorithms.Aes256KW as the key encryption algorithm and SecurityAlgorithms.Aes256CbcHmacSha512 as the encryption algorithm will do the job. Note that the key used to encryption algorithm should have 256 / 8 length.






            share|improve this answer






























              0














              Finally I found the answer:



              var claims = new Claim { new SomeClaimes() };
              var scKey = Encoding.UTF8.GetBytes("SOME KEY");
              var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");

              // Note that the ecKey should have 256 / 8 length:
              byte ecKey = new byte[256 / 8];
              Array.Copy(ecKeyTemp, ecKey, 256 / 8);

              var tokenDescriptor = new SecurityTokenDescriptor {
              Subject = new ClaimsIdentity(claims),
              SigningCredentials = new SigningCredentials(
              new SymmetricSecurityKey(
              scKey),
              SecurityAlgorithms.HmacSha512),
              EncryptingCredentials = new EncryptingCredentials(
              new SymmetricSecurityKey(
              ecKey),
              SecurityAlgorithms.Aes256KW,
              SecurityAlgorithms.Aes256CbcHmacSha512),
              Issuer = "My Jwt Issuer",
              Audience = "My Jwt Audience",
              IssuedAt = DateTime.UtcNow,
              Expires = DateTime.Now.AddDays(7),
              };
              var tokenHandler = new JwtSecurityTokenHandler();
              var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
              var jwt = tokenHandler.WriteToken(token);


              As you ca see, using SecurityAlgorithms.Aes256KW as the key encryption algorithm and SecurityAlgorithms.Aes256CbcHmacSha512 as the encryption algorithm will do the job. Note that the key used to encryption algorithm should have 256 / 8 length.






              share|improve this answer




























                0












                0








                0







                Finally I found the answer:



                var claims = new Claim { new SomeClaimes() };
                var scKey = Encoding.UTF8.GetBytes("SOME KEY");
                var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");

                // Note that the ecKey should have 256 / 8 length:
                byte ecKey = new byte[256 / 8];
                Array.Copy(ecKeyTemp, ecKey, 256 / 8);

                var tokenDescriptor = new SecurityTokenDescriptor {
                Subject = new ClaimsIdentity(claims),
                SigningCredentials = new SigningCredentials(
                new SymmetricSecurityKey(
                scKey),
                SecurityAlgorithms.HmacSha512),
                EncryptingCredentials = new EncryptingCredentials(
                new SymmetricSecurityKey(
                ecKey),
                SecurityAlgorithms.Aes256KW,
                SecurityAlgorithms.Aes256CbcHmacSha512),
                Issuer = "My Jwt Issuer",
                Audience = "My Jwt Audience",
                IssuedAt = DateTime.UtcNow,
                Expires = DateTime.Now.AddDays(7),
                };
                var tokenHandler = new JwtSecurityTokenHandler();
                var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
                var jwt = tokenHandler.WriteToken(token);


                As you ca see, using SecurityAlgorithms.Aes256KW as the key encryption algorithm and SecurityAlgorithms.Aes256CbcHmacSha512 as the encryption algorithm will do the job. Note that the key used to encryption algorithm should have 256 / 8 length.






                share|improve this answer















                Finally I found the answer:



                var claims = new Claim { new SomeClaimes() };
                var scKey = Encoding.UTF8.GetBytes("SOME KEY");
                var ecKeyTemp = Encoding.UTF8.GetBytes("SOME OTHER KEY");

                // Note that the ecKey should have 256 / 8 length:
                byte ecKey = new byte[256 / 8];
                Array.Copy(ecKeyTemp, ecKey, 256 / 8);

                var tokenDescriptor = new SecurityTokenDescriptor {
                Subject = new ClaimsIdentity(claims),
                SigningCredentials = new SigningCredentials(
                new SymmetricSecurityKey(
                scKey),
                SecurityAlgorithms.HmacSha512),
                EncryptingCredentials = new EncryptingCredentials(
                new SymmetricSecurityKey(
                ecKey),
                SecurityAlgorithms.Aes256KW,
                SecurityAlgorithms.Aes256CbcHmacSha512),
                Issuer = "My Jwt Issuer",
                Audience = "My Jwt Audience",
                IssuedAt = DateTime.UtcNow,
                Expires = DateTime.Now.AddDays(7),
                };
                var tokenHandler = new JwtSecurityTokenHandler();
                var token = tokenHandler.CreateJwtSecurityToken(tokenDescriptor);
                var jwt = tokenHandler.WriteToken(token);


                As you ca see, using SecurityAlgorithms.Aes256KW as the key encryption algorithm and SecurityAlgorithms.Aes256CbcHmacSha512 as the encryption algorithm will do the job. Note that the key used to encryption algorithm should have 256 / 8 length.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 27 '18 at 17:24

























                answered Nov 27 '18 at 17:13









                javad amiryjavad amiry

                15.3k84698




                15.3k84698






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487247%2fencrypting-jwt-security-token-supported-algorithms%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks