问题描述
我正在为公司中的应用程序编程,并且有一个文件监视程序:
fsw.NotifyFilter = (NotifyFilters.Security Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite)
AddHandler fsw.Changed,New FileSystemEventHandler(AddressOf OnChanged)
AddHandler fsw.Created,AddressOf OnChanged
AddHandler fsw.Renamed,AddressOf OnRenamed
AddHandler fsw.Deleted,AddressOf OnChanged
但是我想通过在Skype中发送文件,将文件上传或上传到任何云来保护某些文件免受用户侵害。
例如我有一个可以使用dogland.exe打开的dgg文件,我想将此.dgg
扩展名仅与此应用一起使用,并对其进行加密和保护,以防其他程序读取此文件。
保护此文件的最佳方法是什么? 我正在使用vb.net 4.6.1
解决方法
如果您愿意的话,您将无法阻止在其计算机上具有管理员权限的用户发送文件,所以您的答案是使它在其他地方毫无用处。
在回答您的问题之前,我想解决两个关键点:
- 安全性分为不同级别。在信息安全方面,您所获得的保护级别与您投入的工作量有关;
- 您不能阻止熟练的,坚定的攻击者,因为某处总是存在弱点。您所能做的就是使他们的工作更加艰辛。您必须确定您愿意承担的风险状况以及需要做出的努力。
也就是说,最简单的绝对方法是对称加密,其中程序使用相同的密钥进行加密和解密。有人检查您的代码并获取密钥很容易受到攻击,但是它将阻止大多数偶然的尝试。
要尝试,请以主要形式输入:
Private Function Encrypt3DES(plainText As String,pw As String) As String
Dim wrapper As New Simple3Des(pw)
Dim cipherText As String = wrapper.EncryptData(plainText)
Return cipherText
End Function
Private Function Decrypt3DES(cipherText As String,pw As String,ByRef plainText As String) As Boolean
Dim wrapper As New Simple3Des(pw)
' DecryptData throws if the wrong password is used.
Try
plainText = wrapper.DecryptData(cipherText)
Return True
Catch ex As System.Security.Cryptography.CryptographicException
Return False
End Try
End Function
这进入了一个帮助器模块:
Imports System.Security.Cryptography
Public NotInheritable Class Simple3Des
Private TripleDes As New TripleDESCryptoServiceProvider
Sub New(ByVal key As String)
' Initialize the crypto provider.
TripleDes.Key = TruncateHash(key,TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash("",TripleDes.BlockSize \ 8)
End Sub
Private Function TruncateHash(
ByVal key As String,ByVal length As Integer) As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Public Function EncryptData(
ByVal plaintext As String) As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte =
System.Text.Encoding.Unicode.GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,TripleDes.CreateEncryptor(),System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes,plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
Public Function DecryptData(
ByVal encryptedtext As String) As String
Try
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms,TripleDes.CreateDecryptor(),System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes,encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
Catch ex As Exception
Return ""
End Try
End Function
End Class
要实现上述代码,只需在对数据进行调用之前将其加密方法写入文件:
Dim encdata = Encrypt3DES(PlainTextStringToEncrypt,password)
' Write encdata to file...
从文件加载数据后,您调用Decrypt3DES
解密数据:
Dim DecodedData as string = ""
If Decrypt3DES(EncryptedData,Password,DecodedData) = True Then
'Do something with DecodedData
End If
这可能是最简单的解决方案,但是“最佳”一词在信息安全中是相对的。您需要根据要保护的信息的价值和暴露信息的风险状况来调整工作水平。您可以做太多或做得太少。
通过不将加密密钥存储在本地,而是在运行时从服务器检索加密密钥,并在使用后立即从内存中清除它,可以使此方法更强大。或者,您可以对加密密码进行加密,因此实际的密码本身在反汇编程序中不可见,因此需要进行额外的工作。您可以做一千件事-希望这可以为您提供一个起点。
如果您想获得更高级的知识,可以研究用户无权访问的基于证书的签名和/或基于服务器的控件。所有这些都是值得保护的信息的问题。
免责声明:我不保证此设置适用于任何特定目的。这可能适合或不适合您的需求。做您自己的研究,并确保所有安全机制都适合您的目的。