Advertisement
EzFlow997

Javascript Encrypt and Decrypt Text

Jun 6th, 2024 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class Encrpter {
  2.     constructor(){
  3.         this.data = new EncryptionData().encyption;
  4.         this.encrypted_text = "";
  5.         this.decrypted_text = "";
  6.         this.encrypting = false;
  7.         this.decrypting = false;
  8.         this.illegal_characters = [34, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 127];
  9.     }
  10.     encrypt_text(in_text){
  11.         this.encrypting = true;
  12.         //console.log(this.data);
  13.         //console.log(in_text);
  14.         //in_text = "Velkoz#1";
  15.         this.encrypted_text = "";
  16.  
  17.         for(let i = 0; i < in_text.length; i++){
  18.             let c = in_text[i].charCodeAt(0);
  19.             //console.log(c);
  20.             let random_pick = (1 + (Math.random()*4)).toFixed(0);
  21.             //console.log(random_pick);
  22.             this.encrypted_text = this.encrypted_text + this.data[c][random_pick];
  23.         }
  24.         //console.log(this.encrypted_text);
  25.         this.encrypting = false;
  26.     }
  27.     decrypt_text(in_text){
  28.         this.decrypting = true;
  29.         //in_text = "y0fLNKCz4vKi8HUqskakf$WN&0wMvcn?R&HFX#nbN3xZagomis85njeZMjHS8ONJdm6iueWA6Xk";
  30.         this.decrypted_text = "";
  31.  
  32.         let decrypters_chunk = [];
  33.         let decrypters_character = [];
  34.  
  35.         //console.log(this.data);
  36.         for(let i = 32; i < 134; i++){
  37.             for(let j = 1; j < 6; j++){
  38.                 if(this.data[i] != undefined){
  39.                     if(this.data[i][j] != undefined){
  40.                         let encrypt = this.data[i][j];
  41.                         let temp = in_text;
  42.                         //console.log(i, encypt);
  43.                         while(temp.indexOf(encrypt) > -1){
  44.                             decrypters_chunk.push(encrypt);
  45.                             decrypters_character.push(String.fromCharCode(i));
  46.                             temp = temp.replace(encrypt,"");
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.         //console.log(decrypters_chunk);
  53.         //console.log(decrypters_character);
  54.        
  55.         while(decrypters_chunk.length > 0){
  56.             for(let i = 0; i < decrypters_chunk.length; i++){
  57.                 let encrypt = decrypters_chunk[i];
  58.                 if(in_text.indexOf(encrypt) == 0){
  59.                     this.decrypted_text = '' + this.decrypted_text + '' + decrypters_character[i];
  60.                     in_text = '' + in_text.slice(encrypt.length, in_text.length + 1);
  61.                     decrypters_chunk.splice(i,1);
  62.                     decrypters_character.splice(i,1);
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.         //console.log(this.decrypted_text);
  68.         this.decrypting = false;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement