2015-02-08 67 views
0

我目前正在使用备份管理器在处理完所有任务后发送电子邮件给我。这封电子邮件应包含已完成的日志。安全电子邮件,不提示输入密码

问题是,我的电子邮件SMTP服务器(gmail)只允许使用SSL进行加密连接。我知道如何建立这样的连接,但由于程序运行时间从上午2点到上午8点,或者在相似的时间,我不想每次都输入密码。但是,我也不想将密码保存为硬盘上的纯文本。所以我正在寻找一种方法来保存加密的密码,并稍后解密它,而不用提示或类似的东西。

感谢您的帮助,

Turakar

+0

我想如果你不想保存它,你可以在你的服务器上为那个存储passsword和make session的服务打包。但它看起来像工程 – Suhan 2015-02-08 12:17:10

+1

您可以使用此代码: http://stackoverflow.com/questions/1678555/password-encryption-decryption-code-in-net – 2015-02-08 12:39:35

回答

0

我用用ProtectedData由托默克莱恩suggsted答案。只需使用ProtectedData.Protect(data, salt, scope)来保护您的密码以字节为单位,ProtectedData.Unprotect(data, salt, scope)即可解除保护。一旦完成,请记得从内存中删除您的密码,否则攻击者可以从那里取回密码。

0

private string Encrypt(string clearText) 
 
{ 
 
    string EncryptionKey = "MAKV2SPBNI99212"; 
 
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 
 
    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(); 
 
      } 
 
      clearText = Convert.ToBase64String(ms.ToArray()); 
 
     } 
 
    } 
 
    return clearText; 
 
} 
 
    
 
private string Decrypt(string cipherText) 
 
{ 
 
    string EncryptionKey = "MAKV2SPBNI99212"; 
 
    byte[] cipherBytes = Convert.FromBase64String(cipherText); 
 
    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(); 
 
      } 
 
      cipherText = Encoding.Unicode.GetString(ms.ToArray()); 
 
     } 
 
    } 
 
    return cipherText; 
 
}

+0

但在这种情况下,关键是静态的,不是它? – Turakar 2015-02-08 12:43:29

+0

哪个关键? 加密是静态的,但您可以将加密的密码保存到您的app.config ... – 2015-02-08 12:45:28

+0

我在我的一些Web/Win应用程序中使用它,它的工作非常好 – 2015-02-08 12:46:34