-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_caesar_cypher.py
More file actions
56 lines (50 loc) · 1.64 KB
/
python_caesar_cypher.py
File metadata and controls
56 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
hurufs = 'abcdefghijklmnopqrstuvwxyz'
jumlah_huruf = len(hurufs)
def enkripsi(plaintext, kunci):
ciphertext = ''
for huruf in plaintext:
huruf = huruf.lower()
if not huruf == ' ':
indeks = hurufs.find(huruf)
if indeks == -1:
ciphertext += huruf
else:
indeks_baru = indeks + kunci
if indeks_baru >= jumlah_huruf:
indeks_baru -= jumlah_huruf
ciphertext += hurufs[indeks_baru]
return ciphertext
def dekripsi(ciphertext, kunci):
plaintext = ''
for huruf in ciphertext:
huruf = huruf.lower()
if not huruf == ' ':
indeks = hurufs.find(huruf)
if indeks == -1:
plaintext += huruf
else:
indeks_baru = indeks - kunci
if indeks_baru < 0:
indeks_baru += jumlah_huruf
plaintext += hurufs[indeks_baru]
return plaintext
print( )
print('*** CAESAR CIPHER PROGRAM ***')
print( )
print('Apakah kamu mau enkripsi atau dekripsi?')
user_input = input('e/d: ').lower()
print( )
if user_input == 'e':
print('ENKRIPSI MODE TERPILIH')
print( )
kunci = int(input('Masukan kunci (1 sampai 26): '))
text = input('Masukan Text yang akan di enkripsi: ')
ciphertext = enkripsi(text, kunci)
print(f'CIPHERTEXT: {ciphertext}')
elif user_input == 'd':
print('DEKRIPSI MODE TERPILIH')
print( )
kunci = int(input('Masukan kunci (1 sampai 26): '))
text = input('Masukan Text yang akan di dekripsi: ')
plaintext = dekripsi(text, kunci)
print(f'PLAINTEXT: {plaintext}')