static Aes() { Provider = AesProvider.Create(); Provider.Mode = CipherMode.ECB; Provider.Padding = PaddingMode.None; Provider.BlockSize = BLOCK_SIZE; }
private static AesFw CreateAes() { var aes = AesFw.Create(); aes.BlockSize = 128; aes.KeySize = 256; aes.Padding = PaddingMode.Zeros; return(aes); }
static AESUtils() { AesEcb = Aes.Create(); AesEcb.Mode = CipherMode.ECB; AesEcb.Padding = PaddingMode.None; AesCbc = Aes.Create(); AesCbc.Mode = CipherMode.CBC; AesCbc.Padding = PaddingMode.None; }
static byte[] AESEncrypt(byte[] key, byte[] iv, Span <byte> dataToEncrypt) { using var result = new MemoryStream(); using var aes = Aes.Create(); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.None; using var cs = new CryptoStream(result, aes.CreateEncryptor(key, iv), CryptoStreamMode.Write); cs.Write(dataToEncrypt); cs.FlushFinalBlock(); return(result.ToArray()); }
/// <summary> /// Decrypt a byte array into a string using the Rijndael algorithm. /// </summary> /// <param name="input">Input to decrypt</param> /// <param name="key">Decrypt key</param> /// <returns>Decrypted data</returns> public static string DecryptByteArray(byte[] input, byte[] key) { string decrypted = string.Empty; using (var aes = AesEncryption.Create()) { aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; aes.Key = key; aes.IV = Enumerable.Repeat <byte>(0, 16).ToArray(); using (var memoryStream = new MemoryStream(input)) using (var crypto = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Read)) using (var sr = new StreamReader(crypto)) decrypted = sr.ReadToEnd().Trim('\0'); } return(decrypted); }
public static string Encrypt(string EncryptText) { byte[] clearBytes = Encoding.Unicode.GetBytes(EncryptText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } EncryptText = Convert.ToBase64String(ms.ToArray()); } } return(EncryptText); }
public static string Decrypt(string DecryptText) { DecryptText = DecryptText.Replace(" ", "+"); byte[] cipherBytes = Convert.FromBase64String(DecryptText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } DecryptText = Encoding.Unicode.GetString(ms.ToArray()); } } return(DecryptText); }
/// <summary> /// Encrypt data using the Rijndael algorithm. /// </summary> /// <param name="input">Input to encrypt</param> /// <param name="key">Encrypt key.</param> /// <returns>Encrypted data</returns> public static byte[] EncryptByteArray(byte[] input, byte[] key) { byte[] encrypted = null; using (var aes = AesEncryption.Create()) { aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; aes.Key = key; aes.IV = Enumerable.Repeat <byte>(0, 16).ToArray(); using (var memoryStream = new MemoryStream()) { using (var crypto = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) { crypto.Write(input, 0, input.Length); } encrypted = memoryStream.ToArray(); } } return(encrypted); }
public static string DecryptString(string key, string cipherText) { byte[] iv = new byte[16]; byte[] buffer = Convert.FromBase64String(cipherText); using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read)) { using (StreamReader streamReader = new StreamReader((Stream)cryptoStream)) { return(streamReader.ReadToEnd()); } } } } }
public Startup(IConfiguration configuration) { _configuration = configuration; _ = bool.TryParse(_configuration[ConfigurationValues.ServerRedirect], out var redirect); var allowHttp = bool.TryParse(_configuration[ConfigurationValues.AllowHttp], out var ah) && ah; var salt = _configuration["SALT"] ?? string.Empty; Func <IServiceProvider, IDataProtector>?dataProtector = !string.IsNullOrWhiteSpace(_configuration["IV"]) && !string.IsNullOrWhiteSpace(_configuration["KEY"]) ? _ => { var symmetricAlgorithm = Aes.Create(); symmetricAlgorithm.IV = Convert.FromBase64String(_configuration["IV"]); symmetricAlgorithm.Key = Convert.FromBase64String(_configuration["KEY"]); symmetricAlgorithm.Padding = PaddingMode.ISO10126; return(new SymmetricDataProtector(symmetricAlgorithm)); } : null; _options = new SimpleAuthOptions( salt, ticketLifetime: TimeSpan.FromDays(7), claimsIncludedInUserCreation: new[] { ClaimTypes.Name, ClaimTypes.Uri, ClaimTypes.Country, ClaimTypes.DateOfBirth, ClaimTypes.Email, ClaimTypes.Gender, ClaimTypes.GivenName, ClaimTypes.Locality, ClaimTypes.PostalCode, ClaimTypes.Role, ClaimTypes.StateOrProvince, ClaimTypes.StreetAddress, ClaimTypes.Surname }) { DataProtector = dataProtector, AllowHttp = allowHttp, RedirectToLogin = redirect, ApplicationName = _configuration[ConfigurationValues.ServerName] ?? "SimpleAuth", Users = sp => new MartenResourceOwnerStore(salt, sp.GetRequiredService <IDocumentSession>), Clients = sp => new MartenClientStore(sp.GetRequiredService <IDocumentSession>), Scopes = sp => new MartenScopeRepository(sp.GetRequiredService <IDocumentSession>), AccountFilters = sp => new MartenFilterStore(sp.GetRequiredService <IDocumentSession>), AuthorizationCodes = sp => new MartenAuthorizationCodeStore(sp.GetRequiredService <IDocumentSession>), ConfirmationCodes = sp => new MartenConfirmationCodeStore(sp.GetRequiredService <IDocumentSession>), DeviceAuthorizations = sp => new MartenDeviceAuthorizationStore(sp.GetRequiredService <IDocumentSession>), Consents = sp => new MartenConsentRepository(sp.GetRequiredService <IDocumentSession>), JsonWebKeys = sp => new MartenJwksRepository(sp.GetRequiredService <IDocumentSession>), Tickets = sp => new MartenTicketStore(sp.GetRequiredService <IDocumentSession>), Tokens = sp => new MartenTokenStore(sp.GetRequiredService <IDocumentSession>), ResourceSets = sp => new MartenResourceSetRepository(sp.GetRequiredService <IDocumentSession>), EventPublisher = sp => new LogEventPublisher(sp.GetRequiredService <ILogger <LogEventPublisher> >()) }; }