Advertisement
MaxSMoke

Visioner example

Jul 24th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.14 KB | Source Code | 0 0
  1. class Encrypt {
  2.   alphabet: string;
  3.   links: {};
  4.   table: string[][];
  5.   constructor(_alpthabet: string) {
  6.     this.alphabet = _alpthabet,
  7.       this.links = this.generateLinks(),
  8.       this.table = this.generateTable();
  9.   }
  10.  
  11.   generateLinks(): Object {
  12.     let tempLinks: Object = {};
  13.     for (let i = 0; i < this.alphabet.length; i++) {
  14.       tempLinks[this.alphabet[i]] = i;
  15.     }
  16.     return tempLinks;
  17.   }
  18.  
  19.   generateTable(): string[][] {
  20.     let tempTable: string[][] = [];
  21.     for (let i: number = 0; i < this.alphabet.length; i++) {
  22.       tempTable[i] = [];
  23.       for (let j: number = 0; j < this.alphabet.length; j++) {
  24.         let shiftedPos: number = (j + i) % this.alphabet.length;
  25.         tempTable[i][j] = this.alphabet.charAt(shiftedPos);
  26.       }
  27.     }
  28.     return tempTable;
  29.   }
  30.  
  31.   encrypt(_string: string, _key: string): string {
  32.     if (_string.length != _key.length) return "Lenth not a same";
  33.     let encryptedString: string = "";
  34.     for (let i: number = 0; i < _string.length; i++) {
  35.       let stringIndex: string = this.links[_string[i]];
  36.       let keyIndex: string = this.links[_key[i]];
  37.       encryptedString += this.table[keyIndex][stringIndex];
  38.     }
  39.     return encryptedString;
  40.   }
  41.  
  42.   decrypt(_string: string, _key: string): string {
  43.     let decryptedString: string = "";
  44.     if (_string.length != _key.length) return "Lenth not a same";
  45.     for (let i: number = 0; i < _string.length; i++) {
  46.       let keyIndex: number = this.links[_key[i]];
  47.       let secondIndex: number = this.table[keyIndex].findIndex(key => _string[i] === key); //indexOf(_string[i]);
  48.       decryptedString += Object.keys(this.links).find(key => this.links[key] === secondIndex);
  49.     }
  50.     return decryptedString;
  51.   }
  52. }
  53. const enAlphabet: string = "BCDEFGHIJKLMNOPQRSTUVWXYZA";
  54. console.log(enAlphabet.length);
  55. const en = new Encrypt(enAlphabet);
  56. const text: string = "HELLOWORLD";
  57. console.log(text.length);
  58. const key: string = "MAXSMOKEMA";
  59. console.log(key.length);
  60. const encString: string = en.encrypt(text, key);
  61. console.log(encString);
  62. const decryptedString: string = en.decrypt(encString, text);
  63. console.log(decryptedString);
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement