update,
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python
|
||||
import os,sys
|
||||
|
||||
ORD_a = ord('a') # 97
|
||||
ORD_A = ord('A') # 65
|
||||
|
||||
def shift_cipher_encrypt(plaintext, key):
|
||||
# apply encryption to text with given key
|
||||
encrypted_message = ''
|
||||
|
||||
for char in plaintext:
|
||||
if char.isalpha():
|
||||
char = char.upper()
|
||||
ascii_offset = ORD_A if char.isupper() else ORD_a # Determine ASCII offset based on uppercase or lowercase letter
|
||||
|
||||
# the comment shown below are the pseudo code, it demonstrate the ideas only
|
||||
# let say the input is 'the' // without quote
|
||||
# find distance of target character with reference to A or a
|
||||
# i.e. t - a = 19 , h - a = 7 , e - a = 4
|
||||
distance = ord(char) - ascii_offset
|
||||
|
||||
# [19,7,4] + [8,8,8] (key) = [27, 15, 12]
|
||||
# Shift the character by adding the key and taking modulo 26 to wrap around
|
||||
# [27,15,12] % [26,26,26] = [1,15,12] // get modules
|
||||
shifted_distance = (distance + key) % 26
|
||||
|
||||
# [1,15,12] + [97,97,97] = [98,112,109]
|
||||
# chr(98) , chr(112) , chr(109) = 'bpm'
|
||||
shifted_char = chr(shifted_distance + ascii_offset)
|
||||
|
||||
# so: the -> bpm
|
||||
encrypted_message += shifted_char
|
||||
|
||||
else:
|
||||
# consider integer case, retain
|
||||
encrypted_message += char
|
||||
|
||||
return encrypted_message
|
||||
|
||||
|
||||
def shift_cipher_decrypt(ciphertext, key):
|
||||
plaintext = ""
|
||||
|
||||
for char in ciphertext:
|
||||
if char.isalpha():
|
||||
ascii_offset = ORD_a if char.islower() else ORD_A # Determine ASCII offset based on lowercase or uppercase letter
|
||||
|
||||
# Calculate the distance of the target character from a or A
|
||||
distance = ord(char) - ascii_offset
|
||||
|
||||
# apply shift, get the remainder of 26
|
||||
shifted_distance = (distance - key) % 26
|
||||
|
||||
# Convert back to ASCII
|
||||
decrypted_char = chr(shifted_distance + ascii_offset)
|
||||
|
||||
plaintext += decrypted_char
|
||||
else:
|
||||
# If it is not an alphabetic character, retain as is.
|
||||
plaintext += char
|
||||
|
||||
return plaintext
|
||||
|
||||
|
||||
def count_letter_e(txt_in):
|
||||
# reserved function for demonstration purpose
|
||||
occurence = 0
|
||||
for char in txt_in:
|
||||
if char.isalpha():
|
||||
if char.lower() == 'e':
|
||||
occurence += 1
|
||||
return occurence
|
||||
|
||||
def count_most_occurrence_letter(txt_in):
|
||||
# letter e, as stated have the most occurrence in the message by statistics.
|
||||
# as 'Shift Cipher' is a encryption by letter shifting, the letters have good chance
|
||||
# to have the most occurrence too in the encrypted text.
|
||||
output = [0] * 26 # bucket for 26 letters
|
||||
|
||||
for char in txt_in:
|
||||
if char.isalpha():
|
||||
output[ord(char.lower()) - ORD_a] += 1
|
||||
|
||||
# output contains the statistics of paragraph letter by letter
|
||||
return output
|
||||
|
||||
def find_max_occurrence(char_occurrences):
|
||||
# get the letter of the most occurrences. i.e. m
|
||||
# by subtract between this letter to e, k can be guess
|
||||
|
||||
# find max occurrence and its index
|
||||
max_idx = char_occurrences.index(max(char_occurrences))
|
||||
|
||||
# subtract it with index of e -> 4
|
||||
return max_idx - 4
|
||||
|
||||
def encrypt_file(file_path, key=8):
|
||||
# open a file and apply encryption
|
||||
output_file = file_path.replace('.txt','_e.txt')
|
||||
|
||||
# convert it to integer
|
||||
key = int(key)
|
||||
|
||||
# open source file (plaintext)
|
||||
with open(file_path,'r',encoding="utf-8") as fi:
|
||||
temp = ''.join(fi.readlines())
|
||||
|
||||
# open target file (encrypted text)
|
||||
with open(output_file,'w+') as fo:
|
||||
fo.truncate(0)
|
||||
fo.writelines([shift_cipher_encrypt(temp, key)])
|
||||
|
||||
print(f'encryption done and file saved to {output_file}')
|
||||
return
|
||||
|
||||
def decrypt_file(file_path):
|
||||
# will open an encrypted file and decrypt it by a guessed key
|
||||
|
||||
with open(file_path,'r') as fi:
|
||||
# beginning of the process
|
||||
# read file and join the lines all
|
||||
lines = fi.readlines()
|
||||
e_temp = ''.join(lines)
|
||||
|
||||
characters_distribution = count_most_occurrence_letter(e_temp)
|
||||
|
||||
print('')
|
||||
print('distribution of letters in encrypted text (case insensitive, from a to z)')
|
||||
print(characters_distribution)
|
||||
|
||||
print('')
|
||||
guess_k = find_max_occurrence(characters_distribution)
|
||||
print(f'guessed k: {guess_k}')
|
||||
|
||||
print('')
|
||||
print('decrypted text:')
|
||||
decrypted_text = shift_cipher_decrypt(e_temp, guess_k)
|
||||
print(decrypted_text)
|
||||
|
||||
temp = encrypt_file('./wiki.txt', 8)
|
||||
# with open('./wiki_e.txt','w+') as fo:
|
||||
# fo.writelines([temp])
|
||||
|
||||
print("Exiting...")
|
5
banson_hker/phase1-fix/deliver/problem3/test.sh
Normal file
5
banson_hker/phase1-fix/deliver/problem3/test.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
python ./shift_cipher_encrypter.py
|
8
banson_hker/phase1-fix/deliver/problem3/wiki.txt
Normal file
8
banson_hker/phase1-fix/deliver/problem3/wiki.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
Ref: https://en.wikipedia.org/wiki/Hong_Kong
|
||||
|
||||
|
||||
Hong Kong was established as a colony of the British Empire after the Qing Empire ceded Hong Kong Island in 1841–1842. The colony expanded to the Kowloon Peninsula in 1860 and was further extended when the United Kingdom obtained a 99-year lease of the New Territories in 1898. Hong Kong was briefly occupied by Japan from 1941 to 1945 during World War II. The whole territory was transferred from the United Kingdom to China in 1997. Hong Kong maintains separate governing and economic systems from that of mainland China under the principle of "one country, two systems".[f]
|
||||
Originally a sparsely populated area of farming and fishing villages,[18][19] the territory is now one of the world's most significant financial centres and commercial ports. Hong Kong is the world's fourth-ranked global financial centre, ninth-largest exporter, and eighth-largest importer. Its currency, the Hong Kong dollar, is the eighth most traded currency in the world. Home to the second-highest number of billionaires of any city in the world, Hong Kong has the largest concentration of ultra high-net-worth individuals. Although the city has one of the highest per capita incomes in the world, severe income inequality exists among the population. Despite having the largest number of skyscrapers of any city in the world, housing in Hong Kong has been well-documented to experience a chronic persistent shortage.
|
||||
Hong Kong is a highly developed territory and has a Human Development Index (HDI) of 0.952, ranking fourth in the world. The city has the second highest life expectancy in the world, and a public transport rate exceeding 90%.
|
||||
|
||||
around 261 words
|
8
banson_hker/phase1-fix/deliver/problem3/wiki_e.txt
Normal file
8
banson_hker/phase1-fix/deliver/problem3/wiki_e.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
ZMN: PBBXA://MV.EQSQXMLQI.WZO/EQSQ/PWVO_SWVO
|
||||
|
||||
|
||||
PWVO SWVO EIA MABIJTQAPML IA I KWTWVG WN BPM JZQBQAP MUXQZM INBMZ BPM YQVO MUXQZM KMLML PWVO SWVO QATIVL QV 1841–1842. BPM KWTWVG MFXIVLML BW BPM SWETWWV XMVQVACTI QV 1860 IVL EIA NCZBPMZ MFBMVLML EPMV BPM CVQBML SQVOLWU WJBIQVML I 99-GMIZ TMIAM WN BPM VME BMZZQBWZQMA QV 1898. PWVO SWVO EIA JZQMNTG WKKCXQML JG RIXIV NZWU 1941 BW 1945 LCZQVO EWZTL EIZ QQ. BPM EPWTM BMZZQBWZG EIA BZIVANMZZML NZWU BPM CVQBML SQVOLWU BW KPQVI QV 1997. PWVO SWVO UIQVBIQVA AMXIZIBM OWDMZVQVO IVL MKWVWUQK AGABMUA NZWU BPIB WN UIQVTIVL KPQVI CVLMZ BPM XZQVKQXTM WN "WVM KWCVBZG, BEW AGABMUA".[N]
|
||||
WZQOQVITTG I AXIZAMTG XWXCTIBML IZMI WN NIZUQVO IVL NQAPQVO DQTTIOMA,[18][19] BPM BMZZQBWZG QA VWE WVM WN BPM EWZTL'A UWAB AQOVQNQKIVB NQVIVKQIT KMVBZMA IVL KWUUMZKQIT XWZBA. PWVO SWVO QA BPM EWZTL'A NWCZBP-ZIVSML OTWJIT NQVIVKQIT KMVBZM, VQVBP-TIZOMAB MFXWZBMZ, IVL MQOPBP-TIZOMAB QUXWZBMZ. QBA KCZZMVKG, BPM PWVO SWVO LWTTIZ, QA BPM MQOPBP UWAB BZILML KCZZMVKG QV BPM EWZTL. PWUM BW BPM AMKWVL-PQOPMAB VCUJMZ WN JQTTQWVIQZMA WN IVG KQBG QV BPM EWZTL, PWVO SWVO PIA BPM TIZOMAB KWVKMVBZIBQWV WN CTBZI PQOP-VMB-EWZBP QVLQDQLCITA. ITBPWCOP BPM KQBG PIA WVM WN BPM PQOPMAB XMZ KIXQBI QVKWUMA QV BPM EWZTL, AMDMZM QVKWUM QVMYCITQBG MFQABA IUWVO BPM XWXCTIBQWV. LMAXQBM PIDQVO BPM TIZOMAB VCUJMZ WN ASGAKZIXMZA WN IVG KQBG QV BPM EWZTL, PWCAQVO QV PWVO SWVO PIA JMMV EMTT-LWKCUMVBML BW MFXMZQMVKM I KPZWVQK XMZAQABMVB APWZBIOM.
|
||||
PWVO SWVO QA I PQOPTG LMDMTWXML BMZZQBWZG IVL PIA I PCUIV LMDMTWXUMVB QVLMF (PLQ) WN 0.952, ZIVSQVO NWCZBP QV BPM EWZTL. BPM KQBG PIA BPM AMKWVL PQOPMAB TQNM MFXMKBIVKG QV BPM EWZTL, IVL I XCJTQK BZIVAXWZB ZIBM MFKMMLQVO 90%.
|
||||
|
||||
IZWCVL 261 EWZLA
|
Reference in New Issue
Block a user