jsencryptionform.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var encoded = null;
  2. var elementId = null;
  3. function randomPassword(length) {
  4. var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  5. var pass = "";
  6. for(var i = 0; i < length; i++) {
  7. var index = Math.floor(Math.random() * 62);
  8. pass += chars.charAt(index);
  9. }
  10. return pass;
  11. };
  12. function encryptFormText() {
  13. if (document.key.text.value.length == 0) {
  14. alert("Please specify a key with which to encrypt the message.");
  15. return;
  16. }
  17. if(document.plain.text.value.length == 0) {
  18. alert("No plain text to encrypt! Please enter or paste plain text in the field above.");
  19. return;
  20. }
  21. encoded = GibberishAES.enc(document.plain.text.value, document.key.text.value);
  22. document.cipher.text.value = encoded;
  23. // generate a random ID
  24. elementId = randomPassword(8);
  25. encoded = encoded.replace(/\n/g, '');
  26. genSampleCode();
  27. };
  28. function genSampleCode() {
  29. document.encryptedCode.text.value = "";
  30. var element = document.getElementById("encryptedTest");
  31. element.innerHTML = "";
  32. if (encoded == "" || encoded == null || elementId == "" || elementId == null)
  33. return;
  34. // standard sample code
  35. if (document.encryptedCode.codeType[0].checked) {
  36. var code1 = "<div id=\"" + elementId + "\" title=\"" + encoded + "\">";
  37. var code2 = "<a href=\"javascript:decryptText('" + elementId + "')\">Show encrypted text</a>";
  38. var code3 = "</div>";
  39. document.encryptedCode.text.value = code1 + "\n\t" + code2 + "\n" + code3;
  40. element.innerHTML = code1 + code2 + code3;
  41. // inline
  42. } else if (document.encryptedCode.codeType[1].checked) {
  43. var code1 = "<a href=\"javascript:decryptText('" + elementId + "')\">Show encrypted text</a>";
  44. var code2 = "<br />\n<br />";
  45. var code3 = "There is <em><span id=\"" + elementId + "\" title=\"" + encoded + "\">hidden text</span></em> here";
  46. document.encryptedCode.text.value = code1 + "\n" + code2 + "\n" + code3;
  47. element.innerHTML = code1+code2+code3;
  48. // ***
  49. } else {
  50. var code1 = "This is encrypted: <span id=\"" + elementId + "\" title=\"" + encoded + "\"><a href=\"javascript:decryptText('" + elementId + "')\">***</a></span>";
  51. document.encryptedCode.text.value = code1;
  52. element.innerHTML = code1;
  53. }
  54. }
  55. function decryptFormText() {
  56. if (document.key.text.value.length == 0) {
  57. alert("Please specify a key with which to decrypt the message.");
  58. return;
  59. }
  60. if(document.cipher.text.value.length == 0) {
  61. alert("No cipher text to decrypt! Please enter or paste cipher text in the field above.");
  62. return;
  63. }
  64. try {
  65. var dec = GibberishAES.dec(document.cipher.text.value, document.key.text.value);
  66. document.plain.text.value = dec;
  67. } catch (err) {
  68. alert("Invalid key");
  69. }
  70. };
  71. function gup(name) {
  72. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  73. var regexS = "[\\?&]"+name+"=([^&#]*)";
  74. var regex = new RegExp( regexS );
  75. var results = regex.exec(window.location.href);
  76. if (results == null)
  77. return "";
  78. else
  79. return results[1];
  80. };
  81. function load() {
  82. document.key.text.value = "";
  83. document.plain.text.value = decodeURIComponent(gup("text"));
  84. document.cipher.text.value = decodeURIComponent(gup("cipher"));
  85. document.encryptedCode.text.value = "";
  86. document.key.text.focus();
  87. }