code_junkie

How to encrypt in VBScript using AES?

Nov 12th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. <?xml version="1.0"?>
  2.  
  3. <!--
  4.  
  5. //
  6. // Ionic.COM.SlowAES.wsc
  7. //
  8. // This is a Windows Script Component that exposes the SlowAES
  9. // encryption engine via COM. This AES can be used from any
  10. // COM-capable environment, including Javascript or VBScript.
  11. //
  12. //
  13. // This code is licensed under the Microsoft Public License. See the
  14. // accompanying License.txt file for details.
  15. //
  16. // Copyright 2009 Dino Chiesa
  17. //
  18.  
  19. -->
  20.  
  21. <package>
  22.  
  23. <component id="Ionic.Com.SlowAES">
  24.  
  25. <comment>
  26. SlowAES is a Javascript implementation of AES.
  27. See http://code.google.com/p/slowaes.
  28. This is a COM package for SlowAES.
  29. </comment>
  30.  
  31. <?component error="true" debug="true"?>
  32.  
  33. <registration
  34. description="WSC Component for SlowAES"
  35. progid="Ionic.Com.SlowAES"
  36. version="1.00"
  37. classid="{ba78383f-1bcc-4df6-9fb9-61cd639ebc94}"
  38. remotable="False">
  39.  
  40. <!-- boilerplate registration/unregistration logic -->
  41. <script language="VBScript">
  42. <![CDATA[
  43.  
  44. strComponent = "Ionic SlowAES"
  45.  
  46. Function Register
  47. MsgBox strComponent & " - registered."
  48. End Function
  49.  
  50. Function Unregister
  51. MsgBox strComponent & " - unregistered."
  52. End Function
  53.  
  54. ]]>
  55. </script>
  56. </registration>
  57.  
  58. <public>
  59. <method name="EncryptString">
  60. <parameter name="plainText"/>
  61. </method>
  62.  
  63. <method name="DecryptBytes">
  64. <parameter name="cipherText"/>
  65. </method>
  66.  
  67. <method name="DecryptBytesToString">
  68. <parameter name="cipherText"/>
  69. </method>
  70.  
  71. <method name="DecryptHexString">
  72. <parameter name="hexStringCipherText"/>
  73. </method>
  74.  
  75. <method name="DecryptCommaDelimitedStringToString">
  76. <parameter name="cipherText"/>
  77. </method>
  78.  
  79. <property name="Key">
  80. <put/>
  81. </property>
  82.  
  83. <property name="Mode">
  84. <put/>
  85. <get/>
  86. </property>
  87.  
  88. <property name="IV">
  89. <put/>
  90. <get/>
  91. </property>
  92.  
  93. <property name="KeySize">
  94. <put/>
  95. <get/>
  96. </property>
  97. </public>
  98.  
  99. <script language="JavaScript">
  100. <![CDATA[
  101.  
  102. // ...insert slowAES code here... //
  103.  
  104. // defaults
  105. var _keysize = slowAES.aes.SIZE_128;
  106. var _mode = slowAES.modeOfOperation.CBC;
  107. var _iv = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  108. var _key;
  109.  
  110. /*
  111. * byteArrayToHexString
  112. * convert a byte array to hex string.
  113. */
  114. function byteArrayToHexString(a)
  115. {
  116. try { hexcase } catch(e) { hexcase=0; }
  117. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  118. var r= "";
  119. for (var i = 0; i < a.length; i++)
  120. {
  121. var b = hex_tab.charAt((a[i] >> 4) & 0x0F) +
  122. hex_tab.charAt(a[i] & 0x0F);
  123. r+= b;
  124. }
  125. return r;
  126. }
  127.  
  128. /*
  129. * hexStringToByteArray
  130. * convert a string of hex byts to a byte array
  131. */
  132. function hexStringToByteArray(s)
  133. {
  134. var r= Array(s.length/2);
  135. for (var i = 0; i < s.length; i+=2)
  136. {
  137. r[i/2] = parseInt(s.substr(i,2),16);
  138. }
  139. return r;
  140. }
  141.  
  142. function EncryptString(plainText)
  143. {
  144. var bytesToEncrypt = cryptoHelpers.convertStringToByteArray(plainText);
  145. var result = slowAES.encrypt(bytesToEncrypt,
  146. _mode,
  147. _key,
  148. _keysize,
  149. _iv);
  150. return result['cipher'];
  151. }
  152.  
  153. function DecryptBytesToString(cipherText)
  154. {
  155. var d = DecryptBytes(cipherText);
  156. var s = cryptoHelpers.convertByteArrayToString(d);
  157. s[cipherText.length]= 0;
  158. return s;
  159. }
  160.  
  161. function DecryptHexString(hexStringCipherText)
  162. {
  163. var cipherText = hexStringToByteArray(hexStringCipherText);
  164. return DecryptBytesToString(cipherText);
  165. }
  166.  
  167. function DecryptCommaDelimitedStringToString(cipherText)
  168. {
  169. var c = [];
  170. var atoms = cipherText.split(",");
  171. for (i=0; i < atoms.length; i++)
  172. {
  173. c.push(parseInt(atoms[i], 10));
  174. }
  175. var d = DecryptBytes(c);
  176. return cryptoHelpers.convertByteArrayToString(d);
  177. }
  178.  
  179. function DecryptBytes(cipherText)
  180. {
  181. if (cipherText == undefined) return null;
  182.  
  183. var originalSize = cipherText.length;
  184.  
  185. var result = slowAES.decrypt(cipherText,
  186. originalSize,
  187. _mode,
  188. _key,
  189. _keysize,
  190. _iv);
  191.  
  192. return result;
  193. }
  194.  
  195. function put_Key(keyString)
  196. {
  197. _key = hexStringToByteArray(keyString);
  198. }
  199.  
  200. function put_KeySize(size)
  201. {
  202. if (size == 128) _keysize = slowAES.aes.keySize.SIZE_128;
  203. else if (size == 192) _keysize = slowAES.aes.keySize.SIZE_192;
  204. else if (size == 256) _keysize = slowAES.aes.keySize.SIZE_256;
  205. else
  206. throw "Unsupported key size. Must be one of { 128, 192, 256 }.";
  207. }
  208.  
  209. function get_KeySize()
  210. {
  211. if (_keysize == slowAES.aes.keySize.SIZE_128) return 128;
  212. else if (_keysize == slowAES.aes.keySize.SIZE_192) return 192;
  213. else if (_keysize == slowAES.aes.keySize.SIZE_256) return 256;
  214. else return -1;
  215. }
  216.  
  217. function put_IV(ivString)
  218. {
  219. _iv = hexStringToByteArray(ivString);
  220. }
  221.  
  222. function get_IV()
  223. {
  224. return byteArrayToHexString(_iv);
  225. }
  226.  
  227. function put_Mode(mode)
  228. {
  229. if (mode == "CBC") _mode= slowAES.modeOfOperation.CBC;
  230. else if (mode == "OFB") _mode= slowAES.modeOfOperation.OFB;
  231. else if (mode == "CFB") _mode= slowAES.modeOfOperation.CFB;
  232. else throw "Unsupported mode. Must be one of {CBC, OFB, CFB}";
  233. }
  234.  
  235. function get_Mode()
  236. {
  237. if (_mode == slowAES.modeOfOperation.CBC) return "CBC";
  238. if (_mode == slowAES.modeOfOperation.OFB) return "OFB";
  239. if (_mode == slowAES.modeOfOperation.CFB) return "CFB";
  240. return "???";
  241. }
  242.  
  243. ]]>
  244.  
  245. </script>
  246.  
  247. </component>
  248.  
  249. </package>
  250.  
  251. ' '
  252. ' byteArrayToHexString'
  253. ' convert a byte array to hex string.'
  254. ' '
  255. Function byteArrayToHexString(a)
  256. Dim r,b,i
  257. r = ""
  258. For i = 0 To UBound(a)
  259. b = Hex( (a(i) And &HF0) / 16) & Hex(a(i) And &HF)
  260. r= r & b
  261. Next
  262. byteArrayToHexString= r
  263. End Function
  264.  
  265. ' '
  266. ' hexStringToByteArray'
  267. ' convert a string of hex byts to a byte array'
  268. ' '
  269. Function hexStringToByteArray(s)
  270. Dim r()
  271. ReDim r(Len(s)/2-1)
  272. Dim x
  273. For i = 0 To Len(s)-2 Step 2
  274. x= "&H" & Mid(s,i+1,2)
  275. r(i/2) = CInt(x)
  276. Next
  277. hexStringToByteArray= r
  278. End Function
  279.  
  280. Function DemoEncryption()
  281. WScript.echo "Testing Ionic.Com.SlowAES..."
  282.  
  283. WScript.echo "key: " & byteArrayToHexString(key)
  284. WScript.echo "iv: " & byteArrayToHexString(iv)
  285. WScript.echo "key length: " & keyLengthInBytes & " bytes"
  286. WScript.echo "key length: " & (keyLengthInBytes*8) & " bits"
  287. WScript.echo "plaintext: " & plaintext
  288. WScript.echo "plaintext.length: " & Len(plaintext)
  289.  
  290. WScript.echo "instantiate Ionic.Com.SlowAES"
  291. Dim aes
  292. set aes = CreateObject("Ionic.Com.SlowAES")
  293.  
  294. WScript.echo "keysize"
  295. aes.KeySize = keyLengthInBytes * 8
  296.  
  297. WScript.echo "key"
  298. aes.Key = byteArrayToHexString(key)
  299.  
  300. WScript.echo "iv "
  301. aes.IV= byteArrayToHexString(iv)
  302.  
  303. WScript.echo "mode "
  304. aes.Mode = "CBC"
  305.  
  306. WScript.echo "encrypting... "
  307. Dim result
  308. result= aes.EncryptString(plaintext)
  309.  
  310. ' result is a comma-separated string '
  311. ' if we Eval() on it we convert it to an array '
  312. Dim expr
  313. expr = "Array(" & result & ")"
  314.  
  315. result= Eval( expr )
  316.  
  317. WScript.echo "Cryptotext/Eval: " & byteArrayToHexString(result)
  318. WScript.echo "Cryptotext.length: " & UBound(result)+1
  319.  
  320. WScript.echo "decrypting... "
  321. Dim decrypted
  322. 'The javascript way to do this is to pass the byte array.'
  323. ' Like so:'
  324. ' var decrypted = aes.DecryptBytesToString(result);'
  325. ' '
  326. 'This does not work from VBScript. So, convert to a hexstring,'
  327. 'pass the hex string, and then convert back, in the COM component.'
  328. decrypted= aes.DecryptHexString(byteArrayToHexString(result))
  329.  
  330. WScript.echo "decrypted: " & decrypted
  331. End Function
  332.  
  333. dim plaintext, iv, key, keyLengthInBytes
  334.  
  335. plaintext= "Hello. This is a test. of the emergency broadcasting system."
  336. ' iv must be a hexstring representation of an array of bytes, length=16'
  337. iv = hexStringToByteArray("feedbeeffeedbeefbaadf00dbaadf00d")
  338. ' key must be a hexstring representation of an array of bytes, length=16 or 32'
  339. key = hexStringToByteArray("cafebabe0099887766554433221100AA")
  340. keyLengthInBytes= UBound(key)+1
  341.  
  342. If Err.Number <> 0 Then Err.Clear
  343.  
  344. Call DemoEncryption
  345.  
  346. If (Err.Number <> 0) Then WScript.echo("Error: " & Err.Description)
Add Comment
Please, Sign In to add comment