客户端加密的最佳实践
我目前正在使用允许“加密”选项的Web API。客户端加密的最佳实践
我可以设置我的帐户有一个“共享密钥”,并且使用此密钥我应该在提交给服务器之前加密客户端上的所有数据。从他们的网站
详情:
加密算法:DES
块模式:ECB
填充:PKCS7或PKCS5 (它们是可以互换)
“共享关键“在这个意思我认为是一个对称算法 - 相同的密钥用于解密/加密,虽然我可能是wr在这一个上。
我想知道在客户端处理这种情况的最佳做法是什么?
如果我的应用程序的逻辑应该使用此密钥来加密数据,那么它对黑客的安全性如何?
请注意,我的应用程序是用C#编写的,这意味着它可以实际上免费反编译。
如果shared key
表示public key
那么您很可能使用了一种算法asymmetric encryption。这样你就可以安全地使用黑客,因为公钥不能用来解密数据。
如果它是对称的,那么所有取决于安全密钥的安全性。您可以将其与程序分开存储(因此用户可以将其安全地存储在闪存驱动器中)。因此,每个用户必须拥有自己的密钥,所有人都无法使用一个对称密钥。
除非您的密钥被泄露,否则您的数据传输是安全的 - 任何人窃听您的客户端 - 服务器连接都将无法解密您的数据,除非他们有您的密钥。
您的主要挑战在于密钥在客户端和服务器上的本地安全存储。为此,我建议查看通过.NET中的ProtectedData类公开的Windows Data Protection API(DPAPI)。
以这种方式,客户端将用不同的密钥加密数据,服务器将用不同的密钥解密。这称为非对称加密/解密。
.NET框架为非对称加密提供RSACryptoServiceProvider和DSACryptoServiceProvider类。当您使用默认构造函数创建新实例时,这些类会创建公钥/私钥对。非对称密钥既可以存储在多个会话中使用,也可以仅为一个会话生成。尽管公钥一般都可以使用,但私钥应该受到严密保护。
For example [VB.NET]:
Dim cspParam as CspParameters = new CspParameters()
cspParam.Flags = CspProviderFlags.UseMachineKeyStore
Dim RSA As System.Security.Cryptography.RSACryptoServiceProvider
= New System.Security.Cryptography.RSACryptoServiceProvider(cspParam)
The key information from the cspParam object above can be saved via:
Dim publicKey as String = RSA.ToXmlString(False) ' gets the public key
Dim privateKey as String = RSA.ToXmlString(True) ' gets the private key
The above methods enable you to convert the public and/or private keys to Xml Strings.
And of course, as you would guess, there is a corresponding FromXmlString method to get them back.
So to encrypt some data with the Public key. The no-parameter constructor is used as we are loading our keys from XML and
do not need to create a new cspParams object:
Dim str as String = "HelloThere"
Dim RSA2 As RSACryptoServiceProvider = New RSACryptoServiceProvider()
' ---Load the private key---
RSA2.FromXmlString(privateKey)
Dim EncryptedStrAsByt() As Byte =RSA2.Encrypt(System.Text.Encoding.Unicode.GetBytes(str),False)
Dim EncryptedStrAsString = System.Text.Encoding.Unicode.GetString(EncryptedStrAsByt)
and as a "proof of concept", to DECRYPT the same data, but now using the Public key:
Dim RSA3 As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParam)
'---Load the Public key---
RSA3.FromXmlString(publicKey)
Dim DecryptedStrAsByt() As Byte =RSA3.Decrypt(System.Text.Encoding.Unicode.GetBytes(EncryptedStrAsString), False)
Dim DecryptedStrAsString = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt)
这对于给定的问题没有帮助,因为API不使用不对称加密。 – 2012-04-28 11:26:10
对于您的情况,请从服务器数据库读取共享密钥,然后使用它。以这种方式,客户端将不会有硬编码密钥,所以如果有人反编译该DLL,则不会找到任何信息。 – 2012-04-28 11:31:10
授权后,您可以将共享密钥存储在会话中。 – 2012-04-29 09:05:24
更新了我的问题。看来该算法是对称(DES)。 – 2012-04-28 08:49:45
@liortal我已更新了答案。 – 2012-04-28 09:25:58