Last active
August 29, 2020 08:26
-
-
Save defrindr/f5afd29800a5d0cdf6762a48824e703e to your computer and use it in GitHub Desktop.
Hp jadul cipher
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
class HPJadul { | |
dictionary = [ | |
[" ", "0", "\n"], // 0 | |
[".", ",", "'", "?", "!", "\"", "1", "-", "(", ")", "@", "/", ":", "_"], // 1 | |
["a", "b", "c", "2"], // 2 | |
["d", "e", "f", "3"], // 3 | |
["g", "h", "i", "4"], // 4 | |
["j", "k", "l", "5"], // 5 | |
["m", "n", "o", "6"], // 6 | |
["p", "q", "r", "s", "7"], // 7 | |
["t", "u", "v", "8"], // 8 | |
["w", "x", "y", "z", "9"], // 9 | |
]; | |
decode(source) { | |
let numeric = ''; | |
let array = []; | |
let count = 0; | |
let result = ""; | |
source = source+"#"; // add padding | |
source = source.split(""); | |
source.forEach( (char) => { | |
// console.log(char); | |
if(char === numeric){ | |
count += 1; | |
}else if(char === "\\"){ | |
array.push([numeric, count]); | |
count = 0; | |
}else{ | |
array.push([numeric, count]); | |
numeric = char; | |
count = 1; | |
} | |
}); | |
array.shift(); // remove padding | |
array.forEach( (char) => { | |
let index_number = char[0]; | |
let index_char = char[1]-1; | |
result += this.dictionary[index_number][index_char]; | |
}); | |
return result; | |
} | |
encode(source){ | |
let result = ""; | |
let temp = 0; | |
source = source.toLowerCase().split(""); | |
source.forEach( (char) => { | |
let dictionary_length = this.dictionary.length; // dictionary length | |
for(let row_index = 0; row_index < dictionary_length; row_index++){ | |
let row_length = this.dictionary[row_index].length; // nested dictionary length | |
for (let column_index = 0; column_index < row_length; column_index++) { | |
if(this.dictionary[row_index][column_index] === char){ | |
if(temp === row_index){ | |
result += "\\"; | |
} | |
result += `${row_index}`.repeat(column_index+1); | |
temp = row_index; | |
} | |
} | |
} | |
}); | |
return result; | |
} | |
} | |
const jadul = new HPJadul(); | |
plaintext = "aku cinta kamu, tapi kamunya itu lho siapa ? :v suedih jadinya"; | |
chiper = jadul.encode(plaintext) | |
result = jadul.decode(chiper); | |
console.log("String : " + plaintext) | |
console.log("Encoding : "+chiper) | |
console.log("Decoding : "+result) | |
/** | |
* | |
Aturan : | |
Jika huruf dalam 1 list, maka gunakan "\" sebagai pemisah | |
contoh : | |
defri | |
3\ 33\ 333777444 | |
Output : | |
String: aku cinta kamu, tapi kamunya itu lho siapa ? : v suedih jadinya | |
Encoding: 25588022244466820552688110827444055268866999204448\ 88055544666077774442720111101111111111111888077778833\ 3444\ 440523444669992 | |
Decoding: aku cinta kamu, tapi kamunya itu lho siapa ? : v suedih jadinya | |
* | |
*/ |
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
class HPJadul : | |
dictionary = [ | |
[" ","0","\n"], # 0 | |
[".", ",", "'", "?", "!", "\"", "1", "-", "(", ")", "@", "/", ":", "_"], # 1 | |
["a", "b", "c", "2"], # 2 | |
["d", "e", "f", "3"], # 3 | |
["g", "h", "i", "4"], # 4 | |
["j", "k", "l", "5"], # 5 | |
["m", "n", "o", "6"], # 6 | |
["p", "q", "r", "s", "7"], # 7 | |
["t", "u", "v", "8"], # 8 | |
["w", "x", "y", "z", "9"], # 9 | |
] | |
def decode(self, string): | |
numeric = '' | |
array = [] | |
count = 0 | |
result = "" | |
string = list(string+"~") # add padding & convert ke list | |
for i in string: | |
if i == numeric: | |
count += 1 | |
elif i == "\\": | |
array.append([numeric, count]) | |
count = 0 | |
else: | |
array.append([numeric, count]) | |
numeric = i | |
count = 1 | |
array.pop(0) # remove padding | |
for _ in array: | |
result += self.dictionary[int(_[0])][_[1]-1] | |
return result | |
def encode(self, string): | |
result = "" | |
temp = 0 | |
string = string.lower() # to lower case | |
for _char in string: | |
for i in range(len(self.dictionary)): | |
for a in range(len(self.dictionary[i])): | |
if self.dictionary[i][a] == _char: | |
if temp == i: | |
result += "\\" | |
result += str(i) * int(a+1) | |
temp = i | |
return result | |
hpjadul = HPJadul() | |
toEncode = "aku cinta kamu, tapi kamunya itu lho siapa ? :v suedih jadinya" | |
toDecode = hpjadul.encode(toEncode) | |
result = hpjadul.decode(toDecode) | |
print("String : "+str(toEncode)) | |
print("Encoding : "+str(toDecode)) | |
print("Decoding : "+str(result)) | |
###### | |
# Aturan : | |
# Jika huruf dalam 1 list , maka gunakan "\" sebagai pemisah | |
# contoh : | |
# defri | |
# 3\33\333777444 | |
# Output | |
# String : aku cinta kamu, tapi kamunya itu lho siapa ? :v suedih jadinya | |
# Encoding : 25588022244466820552688110827444055268866999204448\88055544666077774442720111101111111111111888077778833\3444\440523444669992 | |
# Decoding : aku cinta kamu, tapi kamunya itu lho siapa ? :v suedih jadinya | |
###### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment