public async Task <IActionResult> Register([FromBody] EmailPasswordModel model) { if (!model.Email.IsValidEmail()) { return(BadRequest()); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (!result.Succeeded) { var errorMessages = result.Errors.Select(e => e.Description).Aggregate((en, enn) => en + ", " + enn); return(Conflict(new { Status = "Error", Message = errorMessages })); } //await SendEmailConfirmationAsync(model, user); TODO send email confirmation var refreshToken = AesCryptor.EncryptStringAes(user.Id, RefreshtokenKey.Value, RefreshtokenKey.IV); var jwtToken = JwtTokenizer.GenerateJwtToken(user.Id, user.Email); //CreateAuthenticatedCookie(jwtToken); return(Ok(new { userId = user.Id, Token = jwtToken, refreshtoken = refreshToken })); }
public async Task <IActionResult> Login([FromBody] EmailPasswordModel model) { if (!ModelState.IsValid) { return(BadRequest("Invalid user name or password")); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { return(Conflict("Bad user name password combination")); } if (!await _userManager.CheckPasswordAsync(user, model.Password)) { return(Conflict("Bad user name password combination")); } //TODO: implement user account lockout to avoid guess password with brute force var refreshToken = AesCryptor.EncryptStringAes(user.Id, RefreshtokenKey.Value, RefreshtokenKey.IV); var jwtToken = JwtTokenizer.GenerateJwtToken(user.Id, user.Email); //CreateAuthenticatedCookie(jwtToken); return(Ok(new { userId = user.Id, Token = jwtToken, refreshtoken = refreshToken })); }
private static SymmetricCryptor CreateSymmetricCryptor(int crypto) { SymmetricCryptor cryptor = null; switch (crypto) { case (int)SymmetricCryptoName.Aes: cryptor = new AesCryptor(); break; case (int)SymmetricCryptoName.DES: cryptor = new DESCryptor(); break; case (int)SymmetricCryptoName.RC2: cryptor = new RC2Cryptor(); break; case (int)SymmetricCryptoName.Rijndael: cryptor = new RijndaelCryptor(); break; case (int)SymmetricCryptoName.TripleDES: cryptor = new TripleDESCryptor(); break; } return(cryptor); }
private Session() { var key = new byte[AesBlock.BLOCKSIZE]; var iv = new byte[AesBlock.BLOCKSIZE]; Randomizer.GetBytes(key); Randomizer.GetBytes(iv); var block = new AesBlock(key); mCryptor = new AesCryptor(block, iv); }
public static void Decrypt(Options options, byte[] data) { string[] keyFile = File.ReadAllLines(options.WithKey); byte[] key = Convert.FromBase64String(keyFile[0]); byte[] initializationVector = Convert.FromBase64String(keyFile[1]); AesCryptorGuarder.GuardKeys(key, initializationVector); byte[] decryptedData = AesCryptor.Decrypt(data, key, initializationVector); File.WriteAllBytes(options.Output, decryptedData); }
public static void EncryptWithKey(Options options, byte[] data) { AesEncryptionOutput output; string[] keyFile = File.ReadAllLines(options.WithKey); byte[] key = Convert.FromBase64String(keyFile[0]); byte[] initializationVector = Convert.FromBase64String(keyFile[1]); AesCryptorGuarder.GuardKeys(key, initializationVector); output = AesCryptor.EncryptWithExistingKey(data, key, initializationVector); SaveEncryptedFile(options, output); }
/// <summary> /// The main method. /// </summary> public static void Main() { var databaseConnections = new List <DbConnection> { new DbConnection { ConnectionString = "cloud.asdf.org:user:password", Name = "ASDF cloud" }, new DbConnection { ConnectionString = "cloud.asdf2.org:user:password", Name = "ASDF cloud2" } }; using var aesCryptor = new AesCryptor(); // File Test.txt.aes created with the encrypted data types. aesCryptor.EncryptDbConnectionsToFile("Test.txt", databaseConnections, "TestPW"); var databaseConnectionsLoaded = aesCryptor.DecryptFileToDatabaseConnections("Test.txt", "TestPW"); // databaseConnectionsLoaded contains the stored data types in the file. }
public ICryptor Create(string algorithm) { ICryptor cryptor; switch (algorithm.Sanitize()) { case "aes": cryptor = new AesCryptor(); break; case "des": cryptor = new DesCryptor(); break; case "3des": cryptor = new TDesCryptor(); break; default: throw new Exception(); } return(cryptor); }
public static void EncryptWithoutKey(Options options, byte[] data) { AesEncryptionOutput output = AesCryptor.Encrypt(data); SaveEncryptedFile(options, output); }