# Cryptography/ElGamal

ElGamal is one of the simplest cryptosystems based on the discrete logarithm problem. A quick reminder of the discrete logarithm problem - given $g, h \in G$, such that $h = g^{x}$, find $x$. This is a hard problem when dealing with finite sets. ElGamal has a set of public parameters which can be shared by a number of users of the system. These are referred to as the domain parameters. These parameters are:

- $p$ - a large prime of around 1024 bits (currently) such that $p - 1$ is divisible by another prime $q$ of around 160 bits.
- $g$ - an element of the finite field $F^{*}_{p}$ with order divisible by $q$, that is $g^{(p - 1)/q} (mod p) \ne 1$.


The domain parameters create a public group $G$ with a prime order $q$ and a generator $g$. In other words, we create a large but finite group, and a method of ordering that group (the generator), and we know that it will contain a prime number of elements. These are the properties we are after. These parameters are public, and the security of the system depends on the public and private key pair, which we shall generate next.

The private key $x$ is a randomly-chosen integer. The public key is $h = g^{x} (mod p)$.

In order to encrypt a message $m$ we generate a random ephemeral key $k$, and calculate: $c_{1} = g^{k} c_{2} = m * h^{k}$

This gives us the ciphertext $c = (c_{1}, c_{2})$.

In order to decrypt a ciphertext we compute: $\frac{c_{2}}{c_{1}^{x}} = \frac{m * h^{k}}{g^{x * k}} = \frac{m * g^{x * k}}{g^{x * k}} = m$

As you can see from the decryption method, the message 0 will encrypt to itself, so not a good choice of message. In addition to this, as a result of using the ephemeral key $k$, which will change every time you can see that the same message $m$ will encrypt to many different ciphertexts.

Since the ciphertext is expressed as 2 integers of the same length as the message, we end up with ciphertexts being twice as large as the message they're encrypting.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 22, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6234861016273499, "perplexity": 197.58152967841968}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-41/segments/1410657124607.22/warc/CC-MAIN-20140914011204-00097-ip-10-196-40-205.us-west-1.compute.internal.warc.gz"}