//[Obsolete] //public static string EncryptToBase64String(SecureString passwordToProtect, string keyId, RSACryptoServiceProvider publicKey) //{ // return EncryptToBase64String(passwordToProtect, keyId, publicKey, null, null); //} ///// <summary> ///// This may be used to secure a password for symmetric encryption. ///// </summary> ///// <param name="passwordToProtect"></param> ///// <param name="publicKey"></param> ///// <returns></returns> //[Obsolete("Use the IPublicKey infrastructure")] //public static string EncryptToBase64String(SecureString passwordToProtect, string key1Id, RSACryptoServiceProvider publicKey1, string key2Id, RSACryptoServiceProvider publicKey2) //{ // // Use a 4-byte array to fill it with random bytes and convert it then // // to an integer value. // byte[] plainBytes; // //byte[] encryptedBytes = null; // plainBytes = passwordToProtect.ToByteArray(); // var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000); // var asymObj = asymEnc.EncryptObject(plainBytes, key1Id, publicKey1, key2Id, publicKey2); // var json = Serializer.SerializeToJson(asymObj); // var bytes = Encoding.UTF8.GetBytes(json); // return Convert.ToBase64String(bytes); //} /// <summary> /// This may be used to secure a password for symmetric encryption. /// </summary> /// <param name="passwordToProtect"></param> /// <param name="publicKey"></param> /// <returns></returns> public static async Task <string> EncryptToBase64StringAsync(SecureString passwordToProtect, IPublicKey publicKey1, IPublicKey publicKey2) { // Use a 4-byte array to fill it with random bytes and convert it then // to an integer value. byte[] plainBytes; //byte[] encryptedBytes = null; plainBytes = passwordToProtect.ToByteArray(); var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000); var asymObj = await asymEnc.EncryptObject_PrivateAsync(plainBytes, publicKey1, publicKey2); var json = Serializer.SerializeToJson(asymObj); var bytes = Encoding.UTF8.GetBytes(json); return(Convert.ToBase64String(bytes)); }
/// <summary> /// This may be used to decrypte a password used for symmetric encryption. /// </summary> /// <param name="encryptedValueAsBase64String"></param> /// <param name="privateKey"></param> /// <returns></returns> public static async Task <SecureString> DecryptFromBase64StringAsync(string encryptedValueAsBase64String, IPrivateKey privateKey1, IPrivateKey privateKey2) { if (string.IsNullOrEmpty(encryptedValueAsBase64String)) { throw new ArgumentException("encryptedValueAsBase64String"); } byte[] plainBytes = null; // Read encrypted data var bytes = Convert.FromBase64String(encryptedValueAsBase64String); var json = Encoding.UTF8.GetString(bytes); var asymEncObj = Serializer.DeserializeFromJson <AsymmetricallyEncryptedObject>(json); // deserialize the object var asymEnc = new AsymmetricEncryptor(); plainBytes = (byte[])await asymEnc.DecryptObjectAsync(asymEncObj, privateKey1, privateKey2); var secureString = new SecureString(); var chars = System.Text.Encoding.UTF8.GetChars(plainBytes); foreach (var c in chars) { secureString.AppendChar(c); } for (int i = 0; i < chars.Length; i++) { // clear chars array chars[i] = 'X'; } chars = null; return(secureString); }
//[Obsolete] //public static string EncryptToBase64String(SecureString passwordToProtect, string keyId, RSACryptoServiceProvider publicKey) //{ // return EncryptToBase64String(passwordToProtect, keyId, publicKey, null, null); //} ///// <summary> ///// This may be used to secure a password for symmetric encryption. ///// </summary> ///// <param name="passwordToProtect"></param> ///// <param name="publicKey"></param> ///// <returns></returns> //[Obsolete("Use the IPublicKey infrastructure")] //public static string EncryptToBase64String(SecureString passwordToProtect, string key1Id, RSACryptoServiceProvider publicKey1, string key2Id, RSACryptoServiceProvider publicKey2) //{ // // Use a 4-byte array to fill it with random bytes and convert it then // // to an integer value. // byte[] plainBytes; // //byte[] encryptedBytes = null; // plainBytes = passwordToProtect.ToByteArray(); // var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000); // var asymObj = asymEnc.EncryptObject(plainBytes, key1Id, publicKey1, key2Id, publicKey2); // var json = Serializer.SerializeToJson(asymObj); // var bytes = Encoding.UTF8.GetBytes(json); // return Convert.ToBase64String(bytes); //} /// <summary> /// This may be used to secure a password for symmetric encryption. /// </summary> /// <param name="passwordToProtect"></param> /// <param name="publicKey"></param> /// <returns></returns> public static async Task<string> EncryptToBase64StringAsync(SecureString passwordToProtect, IPublicKey publicKey1, IPublicKey publicKey2) { // Use a 4-byte array to fill it with random bytes and convert it then // to an integer value. byte[] plainBytes; //byte[] encryptedBytes = null; plainBytes = passwordToProtect.ToByteArray(); var asymEnc = new AsymmetricEncryptor(AsymmetricStrategyOption.Aes256_1000); var asymObj = await asymEnc.EncryptObject_PrivateAsync(plainBytes, publicKey1, publicKey2); var json = Serializer.SerializeToJson(asymObj); var bytes = Encoding.UTF8.GetBytes(json); return Convert.ToBase64String(bytes); }
/// <summary> /// This may be used to decrypte a password used for symmetric encryption. /// </summary> /// <param name="encryptedValueAsBase64String"></param> /// <param name="privateKey"></param> /// <returns></returns> public static async Task<SecureString> DecryptFromBase64StringAsync(string encryptedValueAsBase64String, IPrivateKey privateKey1, IPrivateKey privateKey2) { if (string.IsNullOrEmpty(encryptedValueAsBase64String)) { throw new ArgumentException("encryptedValueAsBase64String"); } byte[] plainBytes = null; // Read encrypted data var bytes = Convert.FromBase64String(encryptedValueAsBase64String); var json = Encoding.UTF8.GetString(bytes); var asymEncObj = Serializer.DeserializeFromJson<AsymmetricallyEncryptedObject>(json); // deserialize the object var asymEnc = new AsymmetricEncryptor(); plainBytes = (byte[])await asymEnc.DecryptObjectAsync(asymEncObj, privateKey1, privateKey2); var secureString = new SecureString(); var chars = System.Text.Encoding.UTF8.GetChars(plainBytes); foreach (var c in chars) { secureString.AppendChar(c); } for (int i = 0; i < chars.Length; i++) { // clear chars array chars[i] = 'X'; } chars = null; return secureString; }