public void RijndaelDecryptValidPassword() { ICryptoHelper crytographyHelper = CryptoFactory.Create(CryptographyAlgorithm.Rijndael); crytographyHelper.Entropy = ENCRYPTION_VALID_PASSWORD; string encryptedString = crytographyHelper.Encrypt(ENCRYPTION_TEST_STRING, StringEncodingType.Hex); ICryptoHelper crytographyHelper2 = CryptoFactory.Create(CryptographyAlgorithm.Rijndael); crytographyHelper2.Entropy = ENCRYPTION_VALID_PASSWORD; string decriptedString = crytographyHelper2.Decrypt(encryptedString, StringEncodingType.Hex); Assert.AreEqual(ENCRYPTION_TEST_STRING, decriptedString); }
public void Encrypt_string() { // arrange const string text = "AB"; // act var encrypted = _crypto.Encrypt(text); var decrypted = _crypto.Decrypt(encrypted); // assert decrypted .Should() .Be("AB"); encrypted .Should() .NotBe("AB", "Crypto?"); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (context.ActionArguments["query"] is RefreshToken.Query refreshTokenQuery) { TokenValidationParameters tokenValidationParameters = JWTTokenHelper.InitTokenValidationParameters(_configuration, false); var tokenHandler = new JwtSecurityTokenHandler(); ClaimsPrincipal principal = tokenHandler.ValidateToken(refreshTokenQuery.Token, tokenValidationParameters, out var securityToken); if (securityToken is JwtSecurityToken jwtSecurityToken && jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha512, StringComparison.InvariantCultureIgnoreCase)) { string encryptedUserName = principal.Claims.FirstOrDefault(c => c.Type == Constants.CLAIM_UNAME)?.Value; refreshTokenQuery.UserName = _cryptoHelper.Decrypt <string>(_configSettings.DataProtectionKey, encryptedUserName); await next(); return; } throw new SecurityTokenException("Invalid Token"); } _logger.LogError("Missing query parameter!"); throw new CustomException(HttpStatusCode.NotFound, new { MissingParameter = "Missing query parameter!" }); }