Skip to content

Instantly share code, notes, and snippets.

@agung037
Created January 3, 2022 05:25
Show Gist options
  • Save agung037/e6bc542fc9ee90efb73570156fcb4e68 to your computer and use it in GitHub Desktop.
Save agung037/e6bc542fc9ee90efb73570156fcb4e68 to your computer and use it in GitHub Desktop.
Caesar Cipher
print('''
█▀▀ ▄▀█ █▀▀ █▀ ▄▀█ █▀█   █▀▀ █ █▀█ █ █ █▀▀ █▀█
█▄▄ █▀█ ██▄ ▄█ █▀█ █▀▄   █▄▄ █ █▀▀ █▀█ ██▄ █▀▄
by Agung''')
def menu_pilihan():
print("1. Sembunyikan pesan")
print("2. Baca pesan")
user_input = input("Masukan pilihan anda : ")
while user_input not in ["1","2"] :
print("❌ hanya menerima input 1 atau 2 \n")
user_input = input("Masukan pilihan anda : ")
return int(user_input)
def user_shift():
user_input = input("Masukan kunci 🔑 : ")
# error validation
while user_input.isnumeric() == False:
print("❌ harus angka bulat positif \n")
user_input = input("Masukan kunci : ")
# ambil modulus
return int(user_input) % 26
def user_text():
text = input("Masukan teks anda : ")
return text
def proses(pilihan, user_text, user_shift):
# mendefinisikan alphabet
alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
# mengubah alphabet ke dalam array / list
alphabet_list = []
for huruf in alphabet:
alphabet_list.append(huruf)
hasil = ""
# jika user memilih untuk encypt (menyembunyikan pesan)
if pilihan == 1:
for huruf in user_text:
if huruf not in alphabet_list:
hasil += huruf
continue
urutan_awal = alphabet.index(huruf)
hasil_encrypt = urutan_awal + user_shift
huruf_encrypt = alphabet[hasil_encrypt]
hasil += huruf_encrypt
print(f"\ntulisan anda telah disembunyikan menjadi :\n{hasil}")
print("\n-----------------memulai program baru---------------------")
# jika user memilih untuk decrypt (membaca pesan)
if pilihan == 2:
for huruf in user_text:
if huruf not in alphabet_list:
hasil += huruf
continue
urutan_awal = alphabet.index(huruf)
hasil_decrypt = (26 + urutan_awal) - user_shift
huruf_decrypt = alphabet[hasil_decrypt]
hasil += huruf_decrypt
print(f"\ntulisan anda telah dibaca menjadi :\n{hasil}")
print("\n-----------------memulai program baru---------------------")
# loop agar aplikasi selalu menyala
while True:
proses(menu_pilihan(), user_text(), user_shift())
@agung037
Copy link
Author

agung037 commented Jan 3, 2022

caesar ss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment