Encrypting JWT security token supported algorithms
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
add a comment |
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
add a comment |
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
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
c# .net-core jwt encryption-symmetric netcoreapp2.1
asked Nov 26 '18 at 18:46
javad amiryjavad amiry
15.3k84698
15.3k84698
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.
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
add a comment |
HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.
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
add a comment |
HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.
HmacSha512 use just one key to sign or verify token, try algorithm like RsaSha256 to public / private key encryption.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 27 '18 at 17:24
answered Nov 27 '18 at 17:13
javad amiryjavad amiry
15.3k84698
15.3k84698
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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