internal static IEnumerable <byte[]> Encrypt(this IEnumerable source, EncryptionSettings settings) { DataProtector encryptionAlgorithm = AeadAes256CbcHmac256EncryptionAlgorithm.GetOrCreate(settings.DataEncryptionKey, settings.EncryptionType); ISerializer serializer = settings.GetSerializer(); foreach (var item in source) { byte[] serializedData = serializer.Serialize(item); yield return(encryptionAlgorithm.Encrypt(serializedData)); } }
string EncryptDPAPI(string s) { DataProtector dp = new DataProtector( DataProtector.Store.USE_MACHINE_STORE); byte[] dataToEncrypt = Encoding.UTF8.GetBytes(s); // Not passing optional entropy in this example // Could pass random value (stored by the application) for added // security when using DPAPI with the machine store. return(Convert.ToBase64String(dp.Encrypt(dataToEncrypt, null))); }
/// <summary> /// Send a clear text and encrypt and save in database /// </summary> /// <param name="cleartext">The data to protect</param> /// <returns>The ID of the projected blob</returns> public virtual async Task <EncryptedOutput> EncryptDataAsync(string cleartext) { var encryptionKey = config.GetCurrentKey(); if (string.IsNullOrWhiteSpace(encryptionKey)) { throw new MissingEncryptionKeyException("Could not retrieve Current Encryption Key."); } var data = new SensitiveDataEntity { EncryptionKeyName = config.CurrentKeyName, Data = dataProtector.Encrypt(config.GetCurrentKey(), cleartext) }; var savedId = sensitiveRepository.Add(data); return(new EncryptedOutput() { Id = savedId.ToString() }); }
public static string EncryptString(string encryptedString) { // Create an instance of the encryption API // We assume the key has been encrypted on this machine and not by a user DataProtector dp = new DataProtector(Store.Machine); // Use the API to encrypt the connection string // API works with bytes so we need to convert to and from byte arrays byte[] dataBytes = Encoding.ASCII.GetBytes(encryptedString); byte[] encryptedBytes = dp.Encrypt(dataBytes, null); // Return the encyrpted data to the string return Convert.ToBase64String(encryptedBytes); }
public static string EncryptString(string encryptedString) { // Create an instance of the encryption API // We assume the key has been encrypted on this machine and not by a user DataProtector dp = new DataProtector(Store.Machine); // Use the API to encrypt the connection string // API works with bytes so we need to convert to and from byte arrays byte[] dataBytes = Encoding.ASCII.GetBytes(encryptedString); byte[] encryptedBytes = dp.Encrypt(dataBytes, null); // Return the encyrpted data to the string return(Convert.ToBase64String(encryptedBytes)); }
/// <summary> /// Encrypts the string. /// </summary> /// <param name="inString">The in string.</param> /// <returns>System.String.</returns> public static string EncryptString(string inString) { string results = ""; DataProtector dp = new DataProtector(Store.MachineStore); byte[] dataToEncrypt = Encoding.Unicode.GetBytes(inString); try { results = Convert.ToBase64String(dp.Encrypt(dataToEncrypt)); } catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("DrSched.asmx", "encryptString Exception: " + ex.Message); } return(results); }