Created
December 16, 2015 23:47
-
-
Save ukazap/fe4e5895bd7d0c26165b to your computer and use it in GitHub Desktop.
Contoh Mainan Algoritma Elgamal dalam Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prime' | |
####### PEMBANGKITAN KUNCI OLEH ALICE | |
# Pilih sembarang bilangan prima p (p dapat di-share di antara anggota kelompok) | |
p = Prime.take(1000).last #bilangan prima ke-1000 | |
# Pilih dua buah bilangan acak, g dan x, dengan syarat g < p dan 1 <= x <= p – 2 | |
g, x = nil | |
until g != x | |
g = rand 1..(p-1) | |
x = rand 1..(p-2) | |
end | |
# Hitung y = g^x mod p | |
y = (g**x) % p | |
puts "Kunci publik: (#{y}, #{g}, #{p})" | |
puts "Kunci privat: #{x}\n\n" | |
####### ENKRIPSI OLEH BOB | |
pesan = 666 | |
# Pilih bilangan acak k, yang dalam hal ini 1 <= k <= p – 2 | |
k = rand 1..(p-1) | |
# Hitung a dan b, kemudian kirim ke Alice. | |
a = (g**k) % p | |
b = (y**k) * pesan % p | |
puts "Pesan: #{pesan}" | |
puts "K: #{k}" | |
puts "Terenkripsi: (#{a}, #{b})\n\n" | |
####### DEKRIPSI OLEH ALICE | |
# Alice menerima ciphertext (a, b) kemudian mendekripsi menggunakan kunci privat x | |
satu_per_a_pangkat_x = a**(p-1-x) % p | |
dekripsi = (b * satu_per_a_pangkat_x) % p | |
puts "Terdekripsi: #{dekripsi}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment