{"id":494,"date":"2026-11-13T13:39:36","date_gmt":"2026-11-13T05:39:36","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-elgamal-encrypts-a-message-as-a-pair-of-powers\/"},"modified":"2026-11-13T21:58:58","modified_gmt":"2026-11-13T13:58:58","slug":"how-elgamal-encrypts-a-message-as-a-pair-of-powers","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-elgamal-encrypts-a-message-as-a-pair-of-powers\/","title":{"rendered":"How ElGamal encrypts a message as a pair of powers"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>ElGamal<\/strong> encrypts an integer as a pair. The public key is <code>g^x mod p<\/code>. A ciphertext is <code>(g^k mod p, m * (g^x)^k mod p)<\/code>. Decryption divides the second element by the first element raised to <code>x<\/code>, which is multiplication by the modular inverse. The fragment uses <code>p = 23<\/code>, <code>g = 5<\/code>, <code>x = 6<\/code>, <code>m = 7<\/code>, and <code>k = 3<\/code>, and the pair is <code>10 19<\/code>. These are the equations on a group small enough to read. A deployment uses a group whose discrete logarithm is hard.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The sender has to hide <code>m<\/code> using a published group element, without learning <code>x<\/code>. The ephemeral exponent <code>k<\/code> makes two encryptions of the same <code>m<\/code> differ. The fragment fixes <code>k<\/code> so the printed pair stays put. The group modulo 23 is a demonstration, in the same sense as a textbook RSA modulus built from two small primes. The ciphertext is two residues, not a padded byte string.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">With these parameters, raising the first residue to <code>x<\/code> and inverting it recovers 7 from the second residue. A different <code>k<\/code> produces a different pair for the same message. The map is not authenticated. A changed pair decrypts to some residue, or fails the range check, but the equations alone do not provide a tag. The discrete logarithm in a 23-element group is trivial. The equations are what the fragment checks.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Publish <code>p<\/code>, <code>g<\/code>, and <code>y = g^x mod p<\/code>. Keep <code>x<\/code>.<\/li>\n<li>Draw <code>k<\/code> and send <code>c1 = g^k mod p<\/code> together with <code>c2 = m * y^k mod p<\/code>.<\/li>\n<li>Decrypt by computing <code>c2 * c1^(p - 1 - x) mod p<\/code>, which multiplies by the inverse of <code>c1^x<\/code>.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails in a group this small, or without a fresh exponent<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Use modulo 23 as a deployed group<\/td>\n<td>The discrete logarithm is a short search. A deployment uses a large group.<\/td>\n<\/tr>\n<tr>\n<td>Reuse one <code>k<\/code> for every message<\/td>\n<td>The ephemeral exponent is what makes equal messages encrypt differently. A fixed <code>k<\/code> is a fixed multiplier.<\/td>\n<\/tr>\n<tr>\n<td>Send <code>m<\/code> as a raw residue when <code>m<\/code> is a long message<\/td>\n<td>One residue holds one group element. A long message uses a symmetric key, and that key is what the residue hides.<\/td>\n<\/tr>\n<tr>\n<td>Skip the inverse and subtract <code>x<\/code><\/td>\n<td>The second element is a product. Decryption multiplies by the modular inverse of <code>c1^x<\/code>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">RSA hides an integer with a public exponent. ElGamal hides it with a published group element and an ephemeral exponent.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment encrypts the integer 7 in the multiplicative group modulo 23. It prints <code>10 19<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def elgamal_demo() -&gt; str:\n    p, g = 23, 5\n    private = 6\n    public = pow(g, private, p)\n    message, k = 7, 3\n    c1 = pow(g, k, p)\n    c2 = (message * pow(public, k, p)) % p\n    recovered = (c2 * pow(c1, p - 1 - private, p)) % p\n    assert recovered == message\n    return f&quot;{c1} {c2}&quot;\n\nprint(elgamal_demo())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>OpenPGP<\/strong> used ElGamal to encrypt a session key, which is the historical GnuPG encryption algorithm. Some encrypted-voting designs add ElGamal ciphertexts because the product of ciphertexts encrypts the sum of the messages. A deployed mailbox today is more often an elliptic-curve encryption.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the 23-element group for any secret. Keep the pair of powers where the group is large, each encryption draws a fresh <code>k<\/code>, and a long message is carried by a symmetric key rather than by a single residue.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for ElGamal returns the ciphertext pair and the decryption by the inverse power. <strong>ElGamal sends <code>g^k<\/code> and <code>m<\/code> times <code>y^k<\/code>, and decryption multiplies by the inverse of <code>g^(kx)<\/code>.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: ElGamal encrypts an integer as a pair. The public key is g^x mod p. A ciphertext is (g^k mod p, m * (g^x)^k mod p). Decryption divides the second element by the first element raised to x, which is multiplication by the modular inverse. The fragment uses&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-494","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=494"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":706,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/494\/revisions\/706"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}