Replace non-characters with '-' instead of '' to mark punctuation

This commit is contained in:
Robbie Antenesse 2018-02-20 23:05:02 -07:00
parent e38a705cf6
commit 988525177c
1 changed files with 4 additions and 3 deletions

View File

@ -1,7 +1,8 @@
const CHARS = [ const CHARS = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '$' 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'&', '$',
]; ];
const generatePad = (string) => { const generatePad = (string) => {
@ -14,7 +15,7 @@ const generatePad = (string) => {
} }
export const encrypt = (string) => { export const encrypt = (string) => {
const strippedString = string.replace(/[\s]+/g, '$').replace(/[^a-zA-Z0-9\$]/g, ''); const strippedString = string.replace(/[\s]+/g, '&').replace(/[^a-zA-Z0-9\&]/g, '$');
const pad = generatePad(strippedString); const pad = generatePad(strippedString);
return { return {
oneTimePad: pad, oneTimePad: pad,
@ -33,7 +34,7 @@ export const decrypt = (string, pad) => {
let charIndex = (letterValue - padValue); let charIndex = (letterValue - padValue);
while (charIndex < 0) {charIndex += CHARS.length} while (charIndex < 0) {charIndex += CHARS.length}
return CHARS[charIndex % CHARS.length]; return CHARS[charIndex % CHARS.length];
}).join('').replace(/\$/g, ' '); }).join('').replace(/\&/g, ' ').replace(/\$/g, '-');
} }
document.getElementById('encryptInput').onclick = () => { document.getElementById('encryptInput').onclick = () => {