-
Notifications
You must be signed in to change notification settings - Fork 0
/
onetimepad.ts
33 lines (27 loc) · 1.14 KB
/
onetimepad.ts
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
//First version of Onetimepad
module OneTimePad {
function getNumberRepresentation(character:string){
return character.charCodeAt(0) - 65;
}
function getCharacterRepresentation(number:number){
return String.fromCharCode(number + 65);
}
export function cypher (plainText: string, padKey: string){
let cipherText : string = '',
plainTextArray:string[] = plainText.toLocaleUpperCase().split(''),
padKeyArray:string[] = padKey.toLocaleUpperCase().split('');
for(var i=0;i<plainTextArray.length;i++){
cipherText += getCharacterRepresentation((getNumberRepresentation(plainTextArray[i]) + getNumberRepresentation(padKeyArray[i])) % 26)
}
return cipherText;
}
export function decryp(encriptedText: string, padKey: string){
var decryptedText : string = '',
encriptedTextArray:string[] = encriptedText.toLocaleUpperCase().split(''),
padKeyArray:string[] = padKey.toLocaleUpperCase().split('');
for(var i=0;i<encriptedTextArray.length;i++){
decryptedText += getCharacterRepresentation((getNumberRepresentation(encriptedTextArray[i]) - getNumberRepresentation(padKeyArray[i])) % 26)
}
return decryptedText;
}
}