简体   繁体   English

公钥加密

[英]Public Key Encryption

I was thinking about communication between two computers using public-key encryption. 我当时正在考虑使用公钥加密在两台计算机之间进行通信。

Let us assume that on one computer side I generate a private key and a public key. 让我们假设我在一台计算机上生成一个私钥和一个公钥。 If I send the public key to the other computer, can it still be captured by a packet sniffer and use it to decrypt my message? 如果我将公共密钥发送到另一台计算机,它仍然可以被数据包嗅探器捕获并用它来解密我的消息吗? If yes, what can be done to prevent this and what is the use of public key encryption then? 如果是,可以采取什么措施防止这种情况的发生,那么公钥加密的用途是什么?

Update 更新

Is SSL equivalent to public-key encryption? SSL是否等效于公钥加密? Should I use it instead? 我应该改用它吗?

No. The public key is not used to decrypt, but to encrypt. 不可以。公共密钥不是用来解密,而是用来加密。 You decrypt with your private key. 您使用私钥解密。 That way, only you can decrypt the message. 这样,只有才能解密邮件。

Usually though, private-public key encryption is too expensive to encrypt data with. 通常,私钥-公钥加密过于昂贵,无法用于加密数据。 You'd use a private/public encryption scheme to exchange a shared key (large random number) to encrypt your data with. 您将使用私有/公共加密方案来交换共享密钥(较大的随机数)以用来加密数据。

Example: Alice creates a private/public keypair, sends the public key to Bob. 示例:Alice创建一个专用/公用密钥对,并将公用密钥发送给Bob。 Bob creates a large random number and encrypts it with Alice's public key. 鲍勃创建一个较大的随机数,并使用爱丽丝的公钥对其进行加密。 Alice can decrypt that and find the large random number. 爱丽丝可以解密并找到较大的随机数。 Alice and Bob then use the random number to encrypt their data stream. 然后,爱丽丝和鲍勃使用随机数加密其数据流。

As additional security, you could change keys periodically. 为了提高安全性,您可以定期更改密钥。

To update on your SSL question: it works exactly as I describe above. 要更新您的SSL问题:它的工作原理与我上面所述的完全一样。 See also http://en.wikipedia.org/wiki/Transport_Layer_Security 另请参见http://en.wikipedia.org/wiki/Transport_Layer_Security

The answer is yes, it can be captured by sniffer. 答案是肯定的,它可以被嗅探器捕获。

Why RSA alone does not fit with Matt's situation? 为什么仅RSA不符合Matt的情况?

In Matt's situation, he want both of 2 computers to be ensured legal. 在Matt的情况下,他希望确保两台计算机都合法。 My point is RSA can only ensure 1 of them legal, instead of both of them. 我的观点是, RSA只能确保其中之一合法,而不能同时确保两者合法。

Another important thing is cracker CAN also use private key to encrypt and public key to decrypt. 另一个重要的事情是黑客也可以用私钥加密和公钥来解密。 The key can make client know it was connecting to a "legal" server, but can't help server to ensure it's a "legal" client since key stored at client side or send from server can be leaked. 密钥可以使客户端知道它正在连接到“合法”服务器,但是由于存储在客户端或从服务器发送的密钥可能无法帮助服务器确保它是“合法”客户端。 A better solution is implement your own hash algorithm to prevent being hacked. 更好的解决方案是实现您自己的哈希算法以防止被黑客入侵。

Here is an article about RSA private key encryption: 这是有关RSA私钥加密的文章:

http://www.codeproject.com/Articles/38739/RSA-Private-Key-Encryption http://www.codeproject.com/Articles/38739/RSA-Private-Key-Encryption

Let's think about bank website, the client should know he is on legal website, but bank server doesn't need to ensure the client is legal or not since mobile phone authentication and other methods can do the trick. 让我们考虑一下银行网站,客户应该知道他在合法网站上,但是银行服务器不需要确保客户合法或合法,因为手机身份验证和其他方法可以解决这个问题。 The mobile phone authentication is just something like "hash method", so, hash method is a must . 手机身份验证就像“哈希方法”一样,因此, 哈希方法是必须的 RSA alone can not ensure safety. 单靠RSA无法确保安全性。

Without hash method, if a cracker gets key stored, or transmitted by Internet, he can easily make a fake client without any difficulties. 如果没有哈希方法,那么如果破解者将密钥存储或通过Internet进行传输,他就可以轻松地伪造客户端,而不会遇到任何困难。

Well, then what's your solution? 好吧,那您的解决方案是什么?

Since I'm implementing a Client-Server based software, I can share you my solution: 由于我正在实施基于客户端服务器的软件,因此可以与我分享我的解决方案:

  1. I saved private key into Client's source code. 我将私钥保存到客户端的源代码中。

  2. When server send some response or client send some request, use private/public key to encrypt and decrypt by the other side. 当服务器发送一些响应或客户端发送一些请求时,请使用私钥/公钥对对方进行加密和解密。

  3. You need some protection, like code obfuscation to protect the key stored in client. 您需要一些保护,例如代码混淆来保护存储在客户端中的密钥。

  4. You need to design an hash algorithm to ensure data sent by client is legal. 您需要设计一种哈希算法,以确保客户端发送的数据合法。 The hacker might get your key finally, but hard to know what your algorithm is so it's still safe enough. 黑客可能最终会获得您的密钥,但是很难知道您的算法是什么,因此仍然足够安全。

  5. Hash algorithm means an algorithm combined with add some salt/SHA-1/UUID/timestamp...etc. 哈希算法是指结合添加一些salt / SHA-1 / UUID / timestamp ...等的算法。 I don't mean you should invent a new encrypt algorithm. 我并不是说您应该发明一种新的加密算法。

For example, if the plain text is I'm so awesome 例如,如果纯文本是I'm so awesome

The algorithm can be: 该算法可以是:

Result = SHA1(salt + plain text + timestamp + anything you like) 结果= SHA1(盐+纯文本+时间戳+您喜欢的任何东西)

If find your algorithm leaked, just change some key values. 如果发现算法泄漏,只需更改一些键值即可。

What if your hash algorithm leaked? 如果您的哈希算法泄漏了怎么办?

Remember there's no algorithm can not be hacked. 请记住,没有算法不能被黑客入侵。 We don't need to build a castle can not be destroyed, we just need to make out enemy pay hard. 我们不需要建造一座不能被摧毁的城堡,我们只需要让敌人付出艰辛的代价。

Still, you also need a quick "big red button" if anything goes wrong. 尽管如此,如果出现任何问题,您还需要快速的“红色大按钮”。 Hash algorithm can play this role quite easy, a small modification can make crackers pay lots of time to hack it out. 哈希算法可以很容易地扮演这个角色,只需进行少量修改就可以使破解者花费大量时间来破解它。 That's already good enough . 那已经足够好了

UPDATE: 更新:

Is SSL equivalent to public-key encryption? SSL是否等效于公钥加密? Should I use it instead? 我应该改用它吗?

Yes, but you still have to store key somewhere better than send it through network. 是的,但是您仍然需要将密钥存储在比通过网络发送密钥更好的地方。 HTTPS/TLS is another decent choice. HTTPS/TLS是另一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM