Advertisement
Guest User

Untitled

a guest
Sep 27th, 2022
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.73 KB | Software | 0 0
  1. function importSecretKey(rawKey) {
  2.   return crypto.subtle.importKey(
  3.     "raw",
  4.     rawKey,
  5.     {length: 128, name: "AES-GCM"},
  6.     true,
  7.     ["encrypt", "decrypt"]
  8.   );
  9. }
  10.  
  11. function StrToBuffer(AInput) {
  12.     return new Uint8Array(AInput.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) } ) )
  13. }
  14.  
  15. function decryptMessage(aKey, aCipherText, aIV, aAAD, aTagLength = 96) {
  16.  
  17.   return crypto.subtle.decrypt(
  18.     {
  19.         name: "AES-GCM",
  20.         iv: aIV,
  21.         additionalData: aAAD,
  22.         tagLength: aTagLength
  23.     },
  24.     aKey,
  25.     aCipherText,
  26.     )
  27. }
  28.  
  29. function encryptMessage(aKey, aClearText, aIV, aAAD, aTagLength = 96) {
  30.  
  31.   return crypto.subtle.encrypt({
  32.         name: 'AES-GCM',
  33.         iv: aIV,
  34.         additionalData: aAAD,
  35.         tagLength: aTagLength
  36.     }, aKey, aClearText);
  37.  
  38. }
  39.  
  40.  
  41. var key = await importSecretKey(StrToBuffer('11111111111111111111111111111111'))
  42. var iv = StrToBuffer('4D4554000000000100000000')
  43. var aad = StrToBuffer('3033333333333333333333333333333333')
  44.  
  45. var ciphertext = StrToBuffer('b662a493a5dfdbccc1dc832271bae416945f2e0474d102d2c7941fcd50c678534083e5d1520ae04c3038a281d176b6b2a1ce6e15fe861f4689b7fe7909f309908a40f843e9ceff7675f00b0e217b7620')
  46. var cleartext = StrToBuffer('0fc00000010c07e6091a0103272e5aff8880020209060001190900ff01010206090c07e6091a0103230000ff8880115a1749cb251d1749cb251d1749cb251d1749cb251d')
  47.  
  48. // encrypt to a 128 bit tag
  49.  
  50. var encMsg = await encryptMessage(key, cleartext, iv, aad, 128);
  51.  
  52. // decrypt and verify
  53. var decMsg = await decryptMessage(key, encMsg, iv, aad, 128);
  54.  
  55. // decrypt original and verify
  56. var decMsg = await decryptMessage(key, ciphertext, iv, aad, 96);
  57.  
  58. // decrypt truncated and verify
  59. var decMsg = await decryptMessage(key, encMsg.slice(0, -4), iv, aad, 96)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement