# How exactly does plaintext become ciphertext?

I'm having a hard time finding out how PlainText becomes Ciphertext. I understand that I do the following: PlainText -> RSA/AES/Whatever -> Ciphertext

Can someone please tell me how this happens, I'm not sure how data (more specifically text) becomes this when using RSA, which uses primes to generate the following output from Test:

-----BEGIN PGP MESSAGE-----
Version: GnuPG v2

hIwDHM1PjJSQd3UBA/91V6HzAJ8E5a3nWttSS1/XDTtE12h9T+4u0kyBHEIunqTr
eoA7aHJla8BxDrxXeJ0z8qR3oA2SIVIWw8XDoBxoNqt34wNO2bATcOXNGmIha06n
vhtFvkdHQANMt1n2wjr88y5mHMpiBYTKOSUdAIgItBDLYA5EIe7tW1o97kHrPtLA
LAGxS4ZwSw2+sHIi8p4CVmyJO/2oIVgYqmp+hDGqs8yNikf0fPpdqLYq5cxOV51s
wzzjZABB4vbrcGsxSt7m/dEyzCLG8wGtzGfYqnNvJxq1rPb6q0zPvum4G5CcSUvo
Dd9vVjNO6+BqGD7ZawGOqa1SfSubIoMJefPZCF1lgLz3qm/vqv1UYnwvZkVFbcAE
WbpqVA1G/Ol6ym2/m9OsqTjLMHViZ7lQfDMOI2x9d5NhBrgCkzCu6wLStO82G9ov
9rQFKzIyyOYu30lU3cO/BQ2Blzqwm+vp0XqlLmJi7lr61DfFaKh8+iWp0J74
=IaiN
-----END PGP MESSAGE-----


And how does Test get signed like the signed message below? I know it uses SHA256, but does it then sign the hash with your private key?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Test
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iJwEAQEIAAYFAlfHNb4ACgkQnCfhDHW4n/i7JgP+LB4DDKDcAbPOLHd0LCnVNy5S
08snokVv3+3o35KHJApBU2YzX8ZcRxT8AjpUTVfVW6dpOdiTQtTA47ZdJ5mLUvsQ
ftFsz+UrclJ+xqSeNbKln9MebHsfQetrZ7VvG6fD3vAvO+6IwYw+4K5I1XUUNZzb
VY0D+Cs5T820ddStLTM=
=UE3G
-----END PGP SIGNATURE-----

• Roughly speaking, the message is encoded to a number (which is the plaintext), then it is encrypted, generating another number (a very big one), and finally it is encoded in a text, which is what PGP shows to you. Aug 31 '16 at 20:30

You are asking a very generic question: how does plaintext become ciphertext. Well, plaintext is used as input for an encryption function, usually defined as $$C = E(K, M)$$ or $$C = E_K(M)$$ where $$E$$ is the encryption function, $$K$$ is the key, $$M$$ is the message or plaintext and $$C$$ of course is the ciphertext. So the ciphertext is anything that is output by the encryption function (unless the output is $$\perp$$, i.e. the input does not conform to the requirements).

You've defined a string "Test" as input, but modern ciphers operate on bytes. This means that "Test" first has to be encoded into a byte array. This is performed using a character encoding such as UTF-8.

PGP however defines an entire protocol and container format. Instead of just outputting the ciphertext itself it defines a packet based container format which specifies the key and algorithm used, etc. This protocol and container format are specified in RFC 4880: OpenPGP Message Format. That also includes the container of the signature and how it can be calculated.

OpenPGP can use different ciphers. In general though it uses a hybrid cryptosystem where a public key of an asymmetric cipher - such as RSA - encrypts a secret key of a symmetric cipher. That key is then in turn used to encrypt the actual data.

Note that a signature is not called ciphertext, that term is specific to encryption/decryption.

Pulling straight from the wikipedia article on RSA:

## Encryption

Suppose that Bob would like to send message M to Alice.

He first turns M into an integer m, such that 0 ≤ m < n and gcd(m, n) = 1 (that is, m and n are coprime integers) by using an agreed-upon reversible protocol known as a padding scheme. He then computes the ciphertext c, using Alice's public key e, corresponding to

${\displaystyle c\equiv m^{e}{\pmod {n}}}$

This can be done efficiently, even for 500-bit numbers, using modular exponentiation. Bob then transmits c to Alice.

## Decryption

Alice can recover m from c by using her private key exponent d by computing

${\displaystyle c^{d}\equiv (m^{e})^{d}\equiv m{\pmod {n}}} {\displaystyle c^{d}\equiv > (m^{e})^{d}\equiv m{\pmod {n}}}$

Given m, she can recover the original message M by reversing the padding scheme.

Put very simply:

• encode the message as a number
• raise the number to a power
• reduce the larger number obtained this way by dividing it with the RSA modulus and taking the remainder

That's it, as far as the actual act of encryption goes.

The original number you raised to the power and reduced can be recovered by exploiting some mathematical relationship, based on the structure of the modulus. The person who formed the modulus generally does not share the structure with anyone else, and so will be the only person that can invert the ciphertext back to your original message. Also, this means that you cannot simply do this trick with an arbitrary number for the modulus, it must fulfill certain criteria.

Note that this is only how RSA encryption works, and is totally, completely unrelated to how symmetric algorithms like AES function, and that should be asked in a separate question. Your question about RSA signatures appears to already have an answer here