Nick Desaulniers: Hidden in Plain Sight - Public Key Crypto |
How is it possible for us to communicate securely when there’s the possibility of a third party eavesdropping on us? How can we communicate private secrets through public channels? How do such techniques enable us to bank online and carry out other sensitive transactions on the Internet while trusting numerous relays? In this post, I hope to explain public key cryptography, with actual code examples, so that the concepts are a little more concrete.
First, please check out this excellent video on public key crypto:
Hopefully that explains the gist of the technique, but what might it actually look like in code? Let’s take a look at example code in JavaScript using the Node.js crypto module. We’ll later compare the upcoming WebCrypto API and look at a TLS handshake.
Meet Alice. Meet Bob. Meet Eve. Alice would like to send Bob a secret message. Alice would not like Eve to view the message. Assume Eve can intercept, but not tamper with, everything Alice and Bob try to share with each other.
Alice chooses a modular exponential key group, such as modp4, then creates a public and private key.
1 2 3 | |
A modular exponential key group is simply a “sufficiently large” prime number, paired with a generator (specific number), such as those defined in RFC2412 and RFC3526.
The public key is meant to be shared; it is ok for Eve to know the public key. The private key must not ever be shared, even with the person communicating to.
Alice then shares her public key and group with Bob.
1 2 3 4 | |
Bob now creates a public and private key pair with the same group as Alice.
1 2 | |
Bob shares his public key with Alice.
1 2 | |
Alice and Bob now compute a shared secret.
1 2 | |
Alice and Bob have now derived a shared secret from each others’ public keys.
1
| |
Meanwhile, Eve has intercepted Alice and Bob’s public keys and group. Eve tries to compute the same secret.
1 2 3 4 5 | |
This is because Alice’s secret is derived from Alice and Bob’s private keys, which Eve does not have. Eve may not realize her secret is not the same as Alice and Bob’s until later.
That was asymmetric encryption; using different keys. The shared secret may now be used in symmetric encryption; using the same keys.
Alice creates a symmetric block cypher using her favorite algorithm, a hash of their secret as a key, and random bytes as an initialization vector.
1 2 3 4 5 | |
Alice then uses her cypher to encrypt her message to Bob.
1
| |
Alice then sends the cypher text, cypher, and hash to Bob.
1 2 3 4 5 6 | |
Bob now constructs a symmetric block cypher using the algorithm from Alice, and a hash of their shared secret.
1 2 | |
Bob now decyphers the encrypted message (cypher text) from Alice.
1 2 | |
Eve has intercepted the cypher text, cypher, hash, and tries to decrypt it.
1 2 3 4 5 | |
http://nickdesaulniers.github.io/blog/2015/02/22/public-key-crypto-code-example/
| Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |