Source: aes.js

  1. /* ricomoo aes wrapper
  2. * Design notes:
  3. * AES is just only one of crypto algorithms, so we need make this pluginized so
  4. * can be overriden by user.
  5. */
  6. import AESLib from './opensources/ricmoo-aes-2.js';
  7. /* The ricomoo AES Wrapper.
  8. * The dependee class is ported from github/ricomoo, the original soruce file doesn't have any license declarations.
  9. * @module anclient/js/aes */
  10. /**AES class
  11. * @class
  12. * @property {AESLib} aesLib the ricomoo/aes
  13. * @protpery {function} encrypt
  14. *
  15. */
  16. export default function AES () {
  17. var verbose = false;
  18. var aesLib = new AESLib(window);
  19. // aesLib(window);
  20. /**get byte[] of random 128bits iv
  21. * @return {byte[]} iv
  22. */
  23. this.getIv128 = function () {
  24. var iv = new Array(16);
  25. for (var i = 0; i < 16; i++) {
  26. iv[i] = Math.random() * 101;
  27. }
  28. iv = new Uint8Array(iv);
  29. return iv;
  30. }
  31. this.encrypt = function (txt, key, iv) {
  32. //txt = pad(txt, 16);
  33. txt = this.pad16m(txt);
  34. if (this.verbose) console.log("txt: " + txt);
  35. var textBytes = aesjs.utils.utf8.toBytes(txt);
  36. key = this.pad16m(key);
  37. if (this.verbose) console.log("key: " + key);
  38. var keyBytes = aesjs.utils.utf8.toBytes(key);
  39. var aesCbc = new aesjs.ModeOfOperation.cbc(keyBytes, iv);
  40. var encryptedBytes = aesCbc.encrypt(textBytes);
  41. // check https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string
  42. // var encryptedB64 = btoa(String.fromCharCode.apply(null, encryptedBytes));
  43. var encryptedB64 = this.bytesToB64(encryptedBytes);
  44. // var u8_2 = new Uint8Array(atob(encryptedB64).split("").map(function(c) { return c.charCodeAt(0); }));
  45. return encryptedB64;
  46. }
  47. this.bytesToB64 = function (byteArr) {
  48. return btoa(String.fromCharCode.apply(null, byteArr));
  49. }
  50. this.b64ToBytes = function (b64Str) {
  51. return new Uint8Array(atob(b64Str).split("").map(function(c) {
  52. return c.charCodeAt(0);
  53. }));
  54. }
  55. this.decrypt = function (cipherB64, key, iv) {
  56. // check https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string
  57. //var encryptedBytes = new Uint8Array(atob(cipherB64).split("").map(function(c) {
  58. // return c.charCodeAt(0); }));
  59. var encryptedBytes = this.b64ToBytes(cipherB64);
  60. // The cipher-block chaining mode of operation maintains internal
  61. // state, so to decrypt a new instance must be instantiated.
  62. var keyBytes = aesjs.utils.utf8.toBytes(key);
  63. var aesCbc = new aesjs.ModeOfOperation.cbc(keyBytes, iv);
  64. var decryptedBytes = aesCbc.decrypt(encryptedBytes);
  65. // Convert our bytes back into text
  66. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  67. return decryptedText.trim();
  68. }
  69. this.pad16m = function (str, paddings, dir) {
  70. var len = str.length;
  71. len = Math.ceil(len / 16);
  72. return this.pad(str, len * 16, paddings, dir);
  73. }
  74. var STR_PAD_LEFT = 1;
  75. var STR_PAD_RIGHT = 2;
  76. var STR_PAD_BOTH = 3;
  77. this.pad = function (str, len, paddings, dir) {
  78. if (typeof(len) == "undefined") { var len = 0; }
  79. if (typeof(paddings) == "undefined") { var paddings = '-'; }
  80. if (typeof(dir) == "undefined") { var dir = STR_PAD_LEFT; }
  81. if (len + 1 >= str.length) {
  82. switch (dir) {
  83. case STR_PAD_RIGHT:
  84. str = str + Array(len + 1 - str.length).join(paddings);
  85. break;
  86. case STR_PAD_BOTH:
  87. var right = Math.ceil((padlen = len - str.length) / 2);
  88. var left = padlen - right;
  89. str = Array(left + 1).join(paddings) + str + Array(right + 1).join(paddings);
  90. break;
  91. default:
  92. str = Array(len + 1 - str.length).join(paddings) + str;
  93. break;
  94. } // switch
  95. }
  96. return str;
  97. }
  98. }
  99. /**
  100. * @constructor
  101. * Test JS AES
  102. * @class*/
  103. export function testAES() {
  104. var aes = new AES();
  105. var iv = aes.getIv128();
  106. var k = aes.pad16m("my-password");
  107. var c = aes.encrypt("plain-text", k, iv);
  108. console.log(c);
  109. var p = aes.decrypt(c, k, iv);
  110. console.log(p);
  111. }