在客户端加密的最佳实践

时间:2012-04-28 07:42:05

标签: c# .net security encryption

我目前正在使用允许“加密”选项的网络API。

我可以设置我的帐户以获得“共享密钥”,并且使用此密钥我应该在提交到服务器之前加密客户端上的所有数据。

他们网站的详细信息:

  

加密算法:DES

     

阻止模式:ECB

     

填充:PKCS7或PKCS5   (它们是可以互换的)

“共享密钥”在这个意义上我认为是一种对称算法 - 用于解密/加密的相同密钥,虽然我可能在这个上错了。

我想知道在客户端处理这种情况的最佳做法是什么?

如果我的应用程序的逻辑应该使用此密钥来加密数据,那么黑客如何安全?

请注意,我的应用程序是用C#编写的,这意味着它可以实际免费反编译。

3 个答案:

答案 0 :(得分:1)

除非您的密钥被泄露,否则您的数据传输是安全的 - 任何窃听客户端 - 服务器连接的人都无法解密您的数据,除非他们有您的密钥。

您面临的主要挑战在于在客户端和服务器上本地安全存储密钥。为此,我建议查看通过.NET中的ProtectedData类公开的Windows数据保护API(DPAPI)。

答案 1 :(得分:0)

如果shared key表示public key,那么您最常见的是使用一种称为asymmetric encryption的算法。这样您就可以安全地使用黑客,因为公钥不能用于解密数据。

如果它是对称的,则所有都取决于密钥的安全性。您可以将其与程序分开存储(以便用户可以将其安全地存储在闪存驱动器上)。因此,每个用户必须拥有自己的密钥,不可能为所有用户使用一个对称密钥。

答案 2 :(得分:0)

通过这种方式,客户端将使用不同的密钥加密数据,服务器将使用不同的密钥进行解密。这称为非对称加密/解密。

.NET Framework为非对称加密提供了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)