I just started teaching myself cryptography, I am reading this http://www.garykessler.net/library/crypto.html to understand the basics.
I am confused by Sections 3.4 and 5.3. Section 3.4 details how to combine hashing, secret key, and public key crypto to get a secure connection over an unsecured network.
To make the digital signature for the message, Alice encrypts the hash value of her message with her private key. Why did Alice do this? I thought private keys were exclusively for decrypting and public keys for encrypting? This means anyone with Alice's public key can decrypt her digital signature and get the hash of her message. How safe is it to use private keys for encryption? Is this the only case where it is acceptable (to send a hash of a message)?
I also have some questions about the digital envelope. A random session key is generated for each session. It is used to encrypt Alice's message, and then it is also encrypted using Bob's public key and both the encrypted message and encrypted session key are sent to Bob. Is this session key the private key in symmetric encryption (i.e. this is how they securely transfer their shared private key to begin using AES for example)? Should Bob respond by only using the session key Alice sent to encrypt his message (no more RSA and hashing). Is it safe to assume Alice is who she really says, and Bob is who he really says at this point? Or does Bob need to first respond and somehow confirm he is in fact Bob? And should the session key change after each message (does this make their correspondence safer)?
Ok, finally one last question! Sorry! In section 5.3 we are given the public key (11,15) and the private key (3,15) as our pair of keys for RSA. The problem is, I can encrypt a string of data using any one of the pairs and decrypt it using any one of the pairs! This means I can use the public key to encrypt something... And then use the very same public key to decrypt that something! Only the private key should be able to do this? My assumption is that this is only the case because the number pair for each key is ridiculously small (3, 11, and 15). If I were using 100 digit prime numbers would this work differently? I think that messages encrypted by the private key can only be decrypted by the public key (the private key can't decrypt its own messages) and messages encrypted by the public key can only be decrypted by the private key (the public key can't decrypt its own messages). Is this correct, or can the private key decrypt its own messages? I'm not at a stage where I can actually use RSA yet..
That's it! For anyone who actually read through this, thanks so much. If you can point me to resources that better explain this topic, that works as well.
I have come to the following conclusions, some not so correct (corrections are addressed in the accepted answer for this question)
Ok I did some more research and I'll answer some of my own questions.
Can you use the private key to encrypt information using RSA?
Yes, actually the keys are interchangeable (the public key can be the private key, and the private key can become the public key). What makes one private and one public is the fact that you keep one private and decide to hand the other one out.
The problem with encrypting using the private key is that ANYONE can decrypt this information if they have the public key! This begs the question of "why even bother encrypting the hash, if anyone can read it? Why not send it as a plaintext hash?"
Turns out this step is important to verify that Alice is who she says. Only the real Alice has the private key, and therefore, only the real Alice can use the private key to encrypt messages that can only be decrypted with the public key. If Bob decrypts the message and the hash of the message and finds that the hash he computes on his own end is different than the one the "alleged Alice" sent, then he can conclude that either the hash or message (or both!) have been tampered with by a 3rd party and that he is not having a secure correspondence.
What is a session key?
If I were to use AES for symmetric encryption, the "session key" would be a 128-bit, 192-bit, or 256-bit secure key I generated (depending on the variant of AES I want to use). This is also known as the private key in symmetric encryption. Since AES is a block cipher, I don't need to worry about changing the key every message (block ciphers use the same key to encrypt different blocks of data). I should change the key every time a new correspondence is established between Alice and Bob, however, for fear that Bob (in my case, Alice is a central server, and "Bob" would be multiple clients) has been compromised and our previously generated secret key has been revealed to a malicious 3rd party.
How should Bob respond now that Alice has initiated correspondence?
Both Alice and Bob have their own pair of public and private keys, and they share an additional private key, the 'session key' used for symmetric encryption. Bob must verify the integrity of Alice's message to confirm Alice's identity and decide his response.
First, Bob needs to decrypt the hash Alice sent (a hash of her message) that was encrypted using Alice's private key. Bob can decrypt it using Alice's public key.
Then, Bob needs to decrypt the session key Alice generated (the private key for their symmetric encryption) that was encrypted using Bob's public key. Only Bob can decrypt this (only he holds Bob's private key!)
Now Bob has a decrypted session key, and a hash of a message Alice wants to send (presumably containing authentication data). The message Alice sent was encrypted using the session key. Bob initializes his symmetric encryption using the session key and then decrypts the encrypted message Alice sent.
To finally verify the integrity of this correspondence, Bob must take a hash of this newly decrypted message. If it is the same as the decrypted hash he got from Alice, a secure connection is established! Alice is really Alice, and Bob is really Bob! Finally. This is the point at which you can assume Alice is who she really says, and Bob is who he really says.
Now that Alice and Bob have established a secure connection, do they only need to use symmetric encryption for communication?
They don't need to use RSA anymore (it's slow), but they still need to send hashes of messages! How can you verify that a malicious 3rd party is not intercepting messages between Alice and Bob, and changing them to gibberish (after all, this 3rd party doesn't know what they're actually saying unless private keys have been compromised)?
The answer is to generate a hash for each message, and then encrypt both the hash and the message and send them.
Since the hash is encrypted, a malicious 3rd party can't just substitute it with their own hash (it will get garbled when Alice or Bob receive the message and attempt to decrypt it). When Alice or Bob receive a message, they can decrypt it to obtain the plaintext hash and plaintext message. They can then compute the hash for the message and compare it with the hash they received. If the hashes aren't the same, it can be concluded a malicious 3rd party tampered with the data. Throw it out!
In RSA can public keys be used to encrypt/decrypt the same info? And private keys?
No, RSA is a one way street (so to speak...) Information encrypted with the public key can only be decrypted with the private key. Information encrypted with the private key can only be decrypted with the public key.
The public key cannot decrypt information encrypted with the public key.
The private key cannot decrypt information encrypted with the private key.
The example keys that webpage gave me to use were too simplistic. Instead, I tried using the pair (3,667) for the public key and (5955,667) for the private key. These worked better.
If I encrypt the number 100 using the public key:
100^3 mod 667 = 167 // if I attempt to decrypt this using the public key again, this happens: 167^3 mod 667 = 469 // << WRONG!! the decrypted value should be 100 // if I attempt to decrypt this using the private key, this happens: 167^5955 mod 667 = 100 // Hoorah! I got the right data!
The same outcome occurs when I switch the role of the private and public keys. It is important to choose strong keys for RSA!
Alright I think that's it. If anyone would care to correct any mistakes I made, that would be appreciated.