我在寫一個存儲電子郵件帳戶信息的類。某些屬性(如電子郵件地址,帳戶密碼和服務器名稱)以加密形式存儲在私有變量中。序列化加密的類屬性
屬性setter對數據進行加密和解密消氣吧。
是的,我知道,存儲在軟件中的加密密鑰本質上是不安全的,但該代碼將在稍後的時候我可以找出一個替代改變。可能使用用戶輸入的密鑰或將用於生成正確密鑰的密碼。目前,該程序僅供個人使用。
當我試圖序列的加密信息,因爲信息是使用屬性getter其解密過程的數據訪問的信息被髮送到XML文件中明文。
我特別不希望有暴露一個額外的加密性能,並標記一個未加密通過串行忽視 - 它似乎不雅,可能不太安全。
我當然可以在主程序中傳遞給屬性setter之前進行de/encryption,但是我想知道如果我想重用'EmailAccount'類。
此外,我寧願暫時至少有明文XML文件,這樣我可以保證的是,EN /解密工作。我意識到我可以加密整個文件,但這是爲了以後。
下面是「EmailAccount」級的縮小版..
Public Class EmailAccount
Public Enum UseSSL
Yes
No
End Enum
Dim _imapPwd As String
Dim _smtpPwd As String
Dim _user As String
Dim _imapServer As String
Dim _smtpServer As String
Dim _usessl As UseSSL
Dim _imapPort As Integer
Dim _smtpPort As Integer
Public Property User As String
Set(value As String)
_user = Encrypt(value)
End Set
Get
Return Decrypt(_user)
End Get
End Property
Public Property ImapPassword As String
Set(value As String)
_imapPwd = Encrypt(value)
End Set
Get
Return Decrypt(_imapPwd)
End Get
End Property
Public Property ImapServer As String
Set(value As String)
_imapServer = Encrypt(value)
End Set
Get
Return Decrypt(_imapServer)
End Get
End Property
Public Sub New()
User = "[email protected]"
ImapPassword = "nopasswordfddfsg"
ImapServer = "no.imap.server"
SmtpPassword = "nopasswordsdfgdf"
SmtpServer = "no.smtp.server"
ImapPort = -1
SmtpPort = -1
SslState = UseSSL.Yes
End Sub
Private Function Encrypt(clearText As String) As String
Dim EncryptionKey As String = "crypto key goes here"
Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D,
&H65, &H64, &H76, &H65, &H64, &H65,
&H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using ms As New MemoryStream()
Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(clearBytes, 0, clearBytes.Length)
cs.Close()
End Using
clearText = Convert.ToBase64String(ms.ToArray())
End Using
End Using
Return clearText
End Function
Private Function Decrypt(cipherText As String) As String
Dim EncryptionKey As String = "crypto key goes here"
Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D,
&H65, &H64, &H76, &H65, &H64, &H65,
&H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using ms As New MemoryStream()
Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(cipherBytes, 0, cipherBytes.Length)
cs.Close()
End Using
cipherText = Encoding.Unicode.GetString(ms.ToArray())
End Using
End Using
Return cipherText
End Function
End Class
在我的主要形式,我有以下程序序列化數據。
''' <summary>
''' Convert a class state into XML
''' </summary>
''' <typeparam name="T">The type of object</typeparam>
''' <param name="obj">The object to serilize</param>
''' <param name="FilePath">The path to the XML</param>
Public Shared Sub Serialize(Of T)(ByVal obj As T, Filepath As String)
Dim XmlBuddy As New System.Xml.Serialization.XmlSerializer(GetType(T))
Dim MySettings As New System.Xml.XmlWriterSettings()
MySettings.Indent = True
MySettings.CloseOutput = True
Dim MyWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(Filepath, MySettings)
XmlBuddy.Serialize(MyWriter, obj)
MyWriter.Flush()
MyWriter.Close()
End Sub
不知道!。加密子的輸出是一個字符串,所以我會假設沒有。 –