Problem in Public key cryptography - cryptography

I have a problem in PKC
Consider the following scenario
when a sender needs to send a message to the receiver :
sender generate the cipher text Using the receiver's Public Key
receiver Use receivers private key to decrypt the cipher text.
So my problem is sender is not generating any keys ? and how the Public key of receiver send to sender ?

Correct, the sender does not need to have any encryption keys of its own - it only needs the public key of the receiver to encrypt a message that only the receiver can decrypt.
The receiver's public key can be distributed openly and publically. There is no need to protect it, since the only thing it can be used for is to encrypt data that only the receiver can decrypt. In S/MIME email encryption, for example, it's common to attach your public key to every cleartext email you send so that anyone you correspond with can encrypt email they send to you using your public key.
Note that when the receiver decrypts a message encrypted with their public key, they have no idea who created the data found in the message. A digital certificate of the sender cryptographically signed by a third party trusted by both receiver and sender could be added to the encrypted message to provide proof of origin to the receiver.

Related

What's the security problem is a private key is leaked for a shared secret key

in the following case, if privateKeyOfA is leaked, what's the security problem? Can someone decrypt the message without privateKeyOfB??
Aes.encrypt(privateKeyOfA, publicKeyOfB, message)
Aes.decrypt(publicKeyOfA, privateKeyOfB)
If not, I guess why we need privateKeyOfA here is for A's signature?
The signature is needed since the receiver must know that the message is coming from someone that he can identify. If he cannot verify the signature, this means that he doesn't know the person.
If the private key of A is compromised by a hacker, he can send messages to everybody with signature impersonating the A.
A key exchange (e.g. using DH or ECDH) would be used by A to convert privateKeyOfA + publicKeyOfB into an AES key. This same AES key can also be generated identically by B using privateKeyOfB + publicKeyOfA. All traffic between A and B would be encrypted using the same AES key.
Assuming that all public keys are known (they are public after all), then anyone who has access to privateKeyOfA can regenerate all AES keys that were generated by A to communicate with anyone. This means all traffic involving this key (messages sent or received by A, with B or anyone else) would be compromised.
But if an ephemeral version was used (like in some modes of TLS), then a new key is generated for each session, so that if 1 key is ever compromised, only this session is compromised. You can read more about forward secrecy.
If the keys are used in the way you describe, then they are not used for signature.

RSA two-way decryption?

I've been experimenting with RSA encryption in python (cryptography.hazmat.primitives.asymmetric). I have the following setup: On one end is the client with the public key sending encrypted data back to the server, which holds the private key. Right now I've got one-directional encryption working, but I'm wondering how you would (or if you should) securely decrypt a message client-side. I thought about just encrypting the private key and storing it, but then the password would appear in the code and expose the key to compromise. Is there a way to securely implement this with a key exchange? Or--the most likely alternative--is this a misuse of the protocol?
EDIT: Wanted to clarify that the possible concerns here would be that using RSA in this way might expose the private key on the file system or between the server and the client.
The normal way is for the server to encrypt the reply with the client's public key and client decrypt with its private key. This requires TWO RSA keypairs -- one for the client and one for the server, and requires each end to know the other's public key.
This need (along with high cost of PKE compared to symmetric encryption) is why PKE is normally only used for authentication and/or key exchange, and a symmetric cipher is used to actually encrypt traffic.

In PKI if everyone knows the public key does it mean anyone can decrypt ....?

In PKI if everyone knows the public key of the server does it mean anyone can decrypt the server response to the client which is encrypted by the private key which in turn can be decrypted by the servers public key as long as someone can sniff somebody else traffic by being in the middle ?
In it's simplest form, public key crypto is used for one-way communication.
If Alice has a public/private key pair, and she gives the public key out to everybody, then anyone can encrypt a message using Alice's public key that only Alice can decrypt using her private key. Alice does not use her private key to encrypt messages for others to read.
For two way secure communication, public key crypto is typically used to transmit a session key that two parties can use to communicate securely.
Let's say Bob wants to talk to Alice. With Alice's public key in hand, Bob uses it to encrypt a secret key and send it to Alice. Alice then uses her private key to decrypt the secret key. Now the secret key is known only to Alice and Bob. So they can both use the secret key with a symmetric encryption algorithm such as AES to send messages back and forth between the two of them.
One thing Alice can do with her private key is create a digital signature. Alice can compose a message, hash the message, then encode the hash with her private key. Then Alice sends the message along with the encrypted hash out publicly. Anyone who has Alice's public key can decode the hash and ensure that the hash of the message matches the encoded hash. Because only Alice could have created the encoded hash, the reader knows that Alice did in fact send that message.
No, only the private key can decrypt the data.
In the client server communication, the data is usually encrypted with a random symmetric key initiated during the start of the communication (handshake process). And all further communication is done with it. The role of the PKI is the client generates the random symmetric key, encrypts with the server public key and the server decrypts it in order to continue the session.

SSL/TLS: Why will the server be the only one to be able to decrypt the encrypted number if it's a public key?

Wouldn't anyone else be able to decrypt it too using the public key? Or is it saying that it will be decrypted with a private key. If that's the case how could something be encrypted with one key and decrypted with another?
This is in reference to this wikipedia article.
Specifically - "In order to generate the session keys used for the secure connection, the client encrypts a random number with the server's public key and sends the result to the server. Only the server should be able to decrypt it, with its private key."
Sorry if this was a silly question.
Because it's public-private key encryption, not symmetric encryption. The plaintext is encrypted to cipher text with the public key and decrypted back to the plaintext with the private key. Trying to decrypt that ciphertext with the public key doesn't work.

Identifying your own message RSA

I have a Software that Encrypts message using AES , the random generated AES key is Encrypted by the receiver's public RSA key. now when I send the message to multiple users...
Sender Side :
Message is Encrypted by Random hashed (sha256) AES KEY
The AES key is then Encrypted many time and appended to the encrypted message using each receiver's public key.
the message has [ number for receivers, [list of encrypted keys], Encrypted message]
Receiver Side:
get the number of receivers
loop thru the appended encrypted keys and decrypt using your Private RSA. until you find the one intended for you. such that when he/she decrypt the key they get the AES Key.
3.decrypt the message using AES key.
Knowing that the key is of base 64 string which means it ends with '=', and of the length 256 because of the sha
the Question IS :
How Do i know (if I'm the receiver) that the Decrypted key using my Private RSA is correct Automatically ?
thank you in advance.
Two questions: Is the protocol you describe fixed, or might it be modified in any way? If it is fixed, which padding scheme do you use for RSA? PKCS#1 v1.5, OAEP or none at all?
If the protocol might be modified, you could use a cipher mode with authentication, such as EAX, CCM or GCM. If RSA key transport decryption fails silently, so will the authenticated AES decryption.
Use a variation of RSA-OAEP for the key transport that provides "plain text awareness" as described here: http://www.rsa.com/rsalabs/node.asp?id=2346.
There is no way to find this encrypted message belongs to which receiver.
But you can do is try to decrypt the message if the decrypt is successful then that is the Receiver