Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. function encrypt(text, key) {
  2. //console.log('input:', text, key);
  3. const ranges = [ // define ranges
  4. 'qwertyuiop', // lower 1
  5. 'QWERTYUIOP', // upper 1
  6. 'asdfghjkl', // lower 2
  7. 'ASDFGHJKL', // upper 2
  8. 'zxcvbnm,.', // lower 3
  9. 'ZXCVBNM<>' // upper 3
  10. ];
  11. let keys = (key < 10) ? '00' + key : (key < 100) ? '0' + key : key.toString(); // convert key to string
  12. //console.log('keys:', key);
  13. let output = '';
  14. for (let i = 0, max = text.length; i < max; i++) {
  15. // check ranges
  16. let rangeIndex;
  17. findRangeIndexLoop:
  18. for (let r = 0, rMax = ranges.length; r < rMax; r++) {
  19. if (ranges[r].indexOf(text[i]) !== -1) {
  20. rangeIndex = r;
  21. break findRangeIndexLoop;
  22. }
  23. }
  24. //console.log('rangeIndex:', rangeIndex);
  25. // encrypt
  26. if (rangeIndex || rangeIndex === 0) {
  27. const currentPosition = ranges[rangeIndex].indexOf(text[i]);
  28. //console.log('currentPosition:', currentPosition);
  29. const keyIndex = (rangeIndex >= 0 && rangeIndex <= 1) ? 0 : (rangeIndex >= 2 && rangeIndex <= 3) ? 1 : 2;
  30. //console.log('keyIndex:', keyIndex, ' | keys[keyIndex]:', keys[keyIndex]);
  31. let newPosition = currentPosition + parseInt(keys[keyIndex]);
  32. //console.log('newPosition:', newPosition);
  33. while (newPosition > ranges[rangeIndex].length - 1) {
  34. newPosition -= ranges[rangeIndex].length;
  35. //console.log('newPosition recalculated:', newPosition);
  36. }
  37. //console.log('new letter:', ranges[rangeIndex][newPosition]);
  38. output += ranges[rangeIndex][newPosition];
  39. } else {
  40. // if out or range use char as is
  41. //console.log('new letter:', text[i]);
  42. output += text[i];
  43. }
  44. //console.log('===');
  45. }
  46. return output;
  47. }
  48.  
  49. function decrypt(text, key) {
  50. //console.log('input:', text, key);
  51. const ranges = [ // define ranges
  52. 'qwertyuiop', // lower 1
  53. 'QWERTYUIOP', // upper 1
  54. 'asdfghjkl', // lower 2
  55. 'ASDFGHJKL', // upper 2
  56. 'zxcvbnm,.', // lower 3
  57. 'ZXCVBNM<>' // upper 3
  58. ];
  59. let keys = (key < 10) ? '00' + key : (key < 100) ? '0' + key : key.toString(); // convert key to string
  60. //console.log('keys:', key);
  61. let output = '';
  62.  
  63. for (let i = 0, max = text.length; i < max; i++) {
  64. // check ranges
  65. let rangeIndex;
  66. findRangeIndexLoop:
  67. for (let r = 0, rMax = ranges.length; r < rMax; r++) {
  68. if (ranges[r].indexOf(text[i]) !== -1) {
  69. rangeIndex = r;
  70. break findRangeIndexLoop;
  71. }
  72. }
  73. //console.log('rangeIndex:', rangeIndex);
  74. // decrypt
  75. if (rangeIndex || rangeIndex === 0) {
  76. const currentPosition = ranges[rangeIndex].indexOf(text[i]);
  77. //console.log('currentPosition:', currentPosition);
  78. const keyIndex = (rangeIndex >= 0 && rangeIndex <= 1) ? 0 : (rangeIndex >= 2 && rangeIndex <= 3) ? 1 : 2;
  79. //console.log('keyIndex:', keyIndex, '| keys[keyIndex]:', keys[keyIndex]);
  80. let newPosition = currentPosition - parseInt(keys[keyIndex]);
  81. //console.log('newPosition:', newPosition);
  82. while (newPosition > ranges[rangeIndex].length - 1) {
  83. newPosition -= ranges[rangeIndex].length;
  84. //console.log('newPosition recalculated:', newPosition);
  85. }
  86. while (newPosition < 0) {
  87. newPosition += ranges[rangeIndex].length;
  88. //console.log('newPosition recalculated:', newPosition);
  89. }
  90. //console.log('new letter:', ranges[rangeIndex][newPosition]);
  91. output += ranges[rangeIndex][newPosition];
  92. } else {
  93. // if out or range use char as is
  94. //console.log('new letter:', text[i]);
  95. output += text[i];
  96. }
  97. //console.log('===');
  98. }
  99. return output;
  100. }
  101.  
  102. encrypt("Ball", 134); // ">fdd"
  103. encrypt("Ball", 444); // ">gff"
  104.  
  105. decrypt(">fdd", 134); // "Ball"
  106. decrypt(">gff", 444); // "Ball"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement