mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
npx standard --fix
This commit is contained in:
@ -11,28 +11,26 @@
|
||||
* @param {String} str - string to be decrypted
|
||||
* @return {String} decrypted string
|
||||
*/
|
||||
function rot13(str) {
|
||||
let response = [];
|
||||
let strLength = str.length;
|
||||
function rot13 (str) {
|
||||
const response = []
|
||||
const strLength = str.length
|
||||
|
||||
for (let i = 0; i < strLength; i++) {
|
||||
const char = str.charCodeAt(i);
|
||||
|
||||
if (char < 65 || (char > 90 && char < 97) || char > 122) {
|
||||
response.push(str.charAt(i));
|
||||
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
|
||||
response.push(String.fromCharCode(str.charCodeAt(i) - 13));
|
||||
} else {
|
||||
response.push(String.fromCharCode(str.charCodeAt(i) + 13));
|
||||
}
|
||||
for (let i = 0; i < strLength; i++) {
|
||||
const char = str.charCodeAt(i)
|
||||
|
||||
if (char < 65 || (char > 90 && char < 97) || char > 122) {
|
||||
response.push(str.charAt(i))
|
||||
} else if ((char > 77 && char <= 90) || (char > 109 && char <= 122)) {
|
||||
response.push(String.fromCharCode(str.charCodeAt(i) - 13))
|
||||
} else {
|
||||
response.push(String.fromCharCode(str.charCodeAt(i) + 13))
|
||||
}
|
||||
return response.join("");
|
||||
}
|
||||
return response.join('')
|
||||
}
|
||||
|
||||
|
||||
// Caesars Cipher Example
|
||||
const encryptedString = "Uryyb Jbeyq";
|
||||
const decryptedString = rot13(encryptedString);
|
||||
const encryptedString = 'Uryyb Jbeyq'
|
||||
const decryptedString = rot13(encryptedString)
|
||||
|
||||
console.log(decryptedString); // Hello World
|
||||
console.log(decryptedString) // Hello World
|
||||
|
@ -1,157 +1,122 @@
|
||||
/******************************************************
|
||||
Find and retrieve the encryption key automatically
|
||||
Find and retrieve the encryption key automatically
|
||||
Note: This is a draft version, please help to modify, Thanks!
|
||||
******************************************************/
|
||||
function keyFinder(str){ // str is used to get the input of encrypted string
|
||||
const wordbank =["I ","You ","We ","They ","He ","She ","It "," the ","The "," of "," is ","Is "," am ","Am "," are ","Are "," have ","Have "," has ","Has "," may ","May "," be ","Be "];
|
||||
//let wordbankelementCounter = 0;
|
||||
//let key = 0; // return zero means the key can not be found
|
||||
let inStr = str.toString(); //convert the input to String
|
||||
let outStr = ""; // store the output value
|
||||
let outStrElement = ""; // temporary store the word inside the outStr, it is used for comparison
|
||||
for (let k=0; k<26; k++){ //try the number of key shifted, the sum of character from a-z or A-Z is 26
|
||||
outStr = caesarCipherEncodeAndDecodeEngine(inStr,k); // use the encrytpion engine to decrypt the input string
|
||||
function keyFinder (str) { // str is used to get the input of encrypted string
|
||||
const wordbank = ['I ', 'You ', 'We ', 'They ', 'He ', 'She ', 'It ', ' the ', 'The ', ' of ', ' is ', 'Is ', ' am ', 'Am ', ' are ', 'Are ', ' have ', 'Have ', ' has ', 'Has ', ' may ', 'May ', ' be ', 'Be ']
|
||||
// let wordbankelementCounter = 0;
|
||||
// let key = 0; // return zero means the key can not be found
|
||||
const inStr = str.toString() // convert the input to String
|
||||
let outStr = '' // store the output value
|
||||
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
|
||||
for (let k = 0; k < 26; k++) { // try the number of key shifted, the sum of character from a-z or A-Z is 26
|
||||
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encrytpion engine to decrypt the input string
|
||||
|
||||
//loop through the whole input string
|
||||
for ( let s=0; s < outStr.length; s++){
|
||||
// loop through the whole input string
|
||||
for (let s = 0; s < outStr.length; s++) {
|
||||
for (let i = 0; i < wordbank.length; i++) {
|
||||
// initialize the outStrElement which is a temp output string for comparison,
|
||||
// use a loop to find the next digit of wordbank element and compare with outStr's digit
|
||||
for (let w = 0; w < wordbank[i].length; w++) {
|
||||
outStrElement += outStr[s + w]
|
||||
}
|
||||
|
||||
for ( let i=0; i < wordbank.length; i++){
|
||||
// console.log( k + outStrElement + wordbank[i] );//debug
|
||||
|
||||
// initialize the outStrElement which is a temp output string for comparison,
|
||||
// use a loop to find the next digit of wordbank element and compare with outStr's digit
|
||||
for ( let w=0; w < wordbank[i].length; w++){
|
||||
outStrElement += outStr[ s + w ];
|
||||
}
|
||||
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
|
||||
// linked list will be used in the next stage of development to calculate the number of occurace of the key
|
||||
if (wordbank[i] == outStrElement) {
|
||||
return k // return the key number if founded
|
||||
}
|
||||
|
||||
//console.log( k + outStrElement + wordbank[i] );//debug
|
||||
|
||||
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
|
||||
// linked list will be used in the next stage of development to calculate the number of occurace of the key
|
||||
if (wordbank[i] == outStrElement){
|
||||
return k; // return the key number if founded
|
||||
}
|
||||
|
||||
outStrElement = ""; //reset the temp word
|
||||
|
||||
} // end for ( let i=0; i < wordbank.length; i++)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return 0; // return 0 if found nothing
|
||||
outStrElement = '' // reset the temp word
|
||||
} // end for ( let i=0; i < wordbank.length; i++)
|
||||
}
|
||||
}
|
||||
return 0 // return 0 if found nothing
|
||||
}
|
||||
|
||||
/* this sub-function is used to assist the keyfinder to find the key */
|
||||
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted)
|
||||
{
|
||||
let shiftNum = numShifted;
|
||||
let charCode = 0;
|
||||
let outStr = "";
|
||||
let shftedcharCode = 0;
|
||||
let result = 0;
|
||||
function caesarCipherEncodeAndDecodeEngine (inStr, numShifted) {
|
||||
const shiftNum = numShifted
|
||||
let charCode = 0
|
||||
let outStr = ''
|
||||
let shftedcharCode = 0
|
||||
let result = 0
|
||||
|
||||
for (let i=0; i<inStr.length; i++){
|
||||
for (let i = 0; i < inStr.length; i++) {
|
||||
charCode = inStr[i].charCodeAt()
|
||||
shftedcharCode = charCode + shiftNum
|
||||
result = charCode
|
||||
|
||||
charCode = inStr[i].charCodeAt();
|
||||
shftedcharCode = charCode + shiftNum;
|
||||
result = charCode;
|
||||
if ((charCode >= 48 && charCode <= 57)) {
|
||||
if (shftedcharCode < 48) {
|
||||
let diff = Math.abs(48 - 1 - shftedcharCode) % 10
|
||||
|
||||
if ( (charCode>=48 && charCode<=57))
|
||||
{
|
||||
if ( shftedcharCode < 48 ){
|
||||
while (diff >= 10) {
|
||||
diff = diff % 10
|
||||
}
|
||||
document.getElementById('diffID').innerHTML = diff
|
||||
|
||||
let diff = Math.abs(48-1-shftedcharCode)%10;
|
||||
shftedcharCode = 57 - diff
|
||||
|
||||
while( diff >= 10){
|
||||
diff = diff%10;
|
||||
}
|
||||
document.getElementById("diffID").innerHTML = diff;
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 48 && shftedcharCode <= 57) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 57) {
|
||||
let diff = Math.abs(57 + 1 - shftedcharCode) % 10
|
||||
|
||||
shftedcharCode = 57-diff;
|
||||
|
||||
result = shftedcharCode;
|
||||
}
|
||||
while (diff >= 10) {
|
||||
diff = diff % 10
|
||||
}
|
||||
document.getElementById('diffID').innerHTML = diff
|
||||
|
||||
else if ( shftedcharCode>=48 && shftedcharCode<=57 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
shftedcharCode = 48 + diff
|
||||
|
||||
else if ( shftedcharCode > 57 ){
|
||||
result = shftedcharCode
|
||||
}
|
||||
} else if ((charCode >= 65 && charCode <= 90)) {
|
||||
if (shftedcharCode <= 64) {
|
||||
let diff = Math.abs(65 - 1 - shftedcharCode) % 26
|
||||
|
||||
let diff = Math.abs(57+1-shftedcharCode)%10;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 90 - diff
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 65 && shftedcharCode <= 90) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 90) {
|
||||
let diff = Math.abs(shftedcharCode - 1 - 90) % 26
|
||||
|
||||
while( diff >= 10){
|
||||
diff = diff%10;
|
||||
}
|
||||
document.getElementById("diffID").innerHTML = diff;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 65 + diff
|
||||
result = shftedcharCode
|
||||
}
|
||||
} else if ((charCode >= 97 && charCode <= 122)) {
|
||||
if (shftedcharCode <= 96) {
|
||||
let diff = Math.abs(97 - 1 - shftedcharCode) % 26
|
||||
|
||||
shftedcharCode = 48+diff;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 122 - diff
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 97 && shftedcharCode <= 122) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 122) {
|
||||
let diff = Math.abs(shftedcharCode - 1 - 122) % 26
|
||||
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
else if ( (charCode>=65 && charCode<=90) )
|
||||
{
|
||||
|
||||
if (shftedcharCode <=64 ){
|
||||
|
||||
let diff = Math.abs(65-1-shftedcharCode)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 90-diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if ( shftedcharCode>=65 && shftedcharCode<=90 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if (shftedcharCode>90 ){
|
||||
let diff = Math.abs(shftedcharCode-1-90)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 65+diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if ( (charCode>=97 && charCode<=122))
|
||||
{
|
||||
if ( shftedcharCode<=96 ){
|
||||
|
||||
let diff = Math.abs(97-1-shftedcharCode)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 122-diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if ( shftedcharCode>=97 && shftedcharCode<=122 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if (shftedcharCode>122 ){
|
||||
let diff = Math.abs(shftedcharCode-1-122)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 97+diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
}
|
||||
outStr = outStr + String.fromCharCode(parseInt(result));
|
||||
}
|
||||
return outStr;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 97 + diff
|
||||
result = shftedcharCode
|
||||
}
|
||||
}
|
||||
outStr = outStr + String.fromCharCode(parseInt(result))
|
||||
}
|
||||
return outStr
|
||||
}
|
||||
|
@ -3,8 +3,8 @@
|
||||
* @param {String} character - character to check
|
||||
* @return {object} An array with the character or null if isn't a letter
|
||||
*/
|
||||
function isLetter(str) {
|
||||
return str.length === 1 && str.match(/[a-zA-Z]/i);
|
||||
function isLetter (str) {
|
||||
return str.length === 1 && str.match(/[a-zA-Z]/i)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -12,12 +12,12 @@ function isLetter(str) {
|
||||
* @param {String} character - character to check
|
||||
* @return {Boolean} result of the checking
|
||||
*/
|
||||
function isUpperCase(character){
|
||||
function isUpperCase (character) {
|
||||
if (character == character.toUpperCase()) {
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
if (character == character.toLowerCase()){
|
||||
return false;
|
||||
if (character == character.toLowerCase()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,25 +27,23 @@ function isUpperCase(character){
|
||||
* @param {String} key - key for encrypt
|
||||
* @return {String} result - encrypted string
|
||||
*/
|
||||
function encrypt(message, key)
|
||||
{
|
||||
|
||||
let result = "";
|
||||
function encrypt (message, key) {
|
||||
let result = ''
|
||||
|
||||
for (let i = 0, j = 0; i < message.length; i++) {
|
||||
let c = message.charAt(i);
|
||||
if (isLetter(c)){
|
||||
if(isUpperCase(c)) {
|
||||
result += String.fromCharCode((c.charCodeAt(0) + key.toUpperCase().charCodeAt(j) - 2 * 65) % 26 + 65); // A: 65
|
||||
const c = message.charAt(i)
|
||||
if (isLetter(c)) {
|
||||
if (isUpperCase(c)) {
|
||||
result += String.fromCharCode((c.charCodeAt(0) + key.toUpperCase().charCodeAt(j) - 2 * 65) % 26 + 65) // A: 65
|
||||
} else {
|
||||
result += String.fromCharCode((c.charCodeAt(0) + key.toLowerCase().charCodeAt(j) - 2 * 97) % 26 + 97); // a: 97
|
||||
result += String.fromCharCode((c.charCodeAt(0) + key.toLowerCase().charCodeAt(j) - 2 * 97) % 26 + 97) // a: 97
|
||||
}
|
||||
} else {
|
||||
result+=c;
|
||||
result += c
|
||||
}
|
||||
j = ++j % key.length;
|
||||
j = ++j % key.length
|
||||
}
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,28 +52,27 @@ function encrypt(message, key)
|
||||
* @param {String} key - key for decrypt
|
||||
* @return {String} result - decrypted string
|
||||
*/
|
||||
function decrypt(message, key)
|
||||
{
|
||||
let result ="";
|
||||
function decrypt (message, key) {
|
||||
let result = ''
|
||||
|
||||
for(let i = 0, j = 0; i < message.length; i++){
|
||||
let c = message.charAt(i);
|
||||
if (isLetter(c)){
|
||||
if(isUpperCase(c)) {
|
||||
result += String.fromCharCode(90-(25-(c.charCodeAt(0)-key.toUpperCase().charCodeAt(j)))%26);
|
||||
for (let i = 0, j = 0; i < message.length; i++) {
|
||||
const c = message.charAt(i)
|
||||
if (isLetter(c)) {
|
||||
if (isUpperCase(c)) {
|
||||
result += String.fromCharCode(90 - (25 - (c.charCodeAt(0) - key.toUpperCase().charCodeAt(j))) % 26)
|
||||
} else {
|
||||
result += String.fromCharCode(122-(25-(c.charCodeAt(0)-key.toLowerCase().charCodeAt(j)))%26);
|
||||
result += String.fromCharCode(122 - (25 - (c.charCodeAt(0) - key.toLowerCase().charCodeAt(j))) % 26)
|
||||
}
|
||||
} else {
|
||||
result+=c;
|
||||
result += c
|
||||
}
|
||||
j = ++j % key.length;
|
||||
j = ++j % key.length
|
||||
}
|
||||
return result;
|
||||
return result
|
||||
}
|
||||
|
||||
let messageEncrypt = encrypt('Hello World!', 'code');
|
||||
console.log(messageEncrypt); // "Jhpnr Yrvng!"
|
||||
const messageEncrypt = encrypt('Hello World!', 'code')
|
||||
console.log(messageEncrypt) // "Jhpnr Yrvng!"
|
||||
|
||||
let messageDecrypt = decrypt('Jsopq Zstzg!', 'code');
|
||||
console.log(messageDecrypt); // "Hello World!"
|
||||
const messageDecrypt = decrypt('Jsopq Zstzg!', 'code')
|
||||
console.log(messageDecrypt) // "Hello World!"
|
||||
|
Reference in New Issue
Block a user