Menu

[r11]: / trunk / EncDecUtils / src / EncDecUtils.mxml  Maximize  Restore  History

Download this file

328 lines (287 with data), 10.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="539" height="530">
<fx:Script>
<![CDATA[
/****************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2012 Adobe Systems Incorporated and it’s licensors
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
* ****************************************************************************/
import alchemy.samples.as3_crypto_wrapper;
import mx.controls.Alert;
import mx.managers.FocusManager;
import mx.utils.Base64Decoder;
import mx.utils.Base64Encoder;
private var aes128key:ByteArray = null;
private var aes128iv:ByteArray = null;
private var fm:FocusManager;
//I could possibly replace this with toString(16)
private function toBase16(input:ByteArray):String
{
var output:String = new String;
const hex:String = "0123456789abcdef";
const length:int = input.length;
for(var i:int = 0; i < length; i++) {
output += hex.charAt((input[i] & 0xF0) >> 4);
output += hex.charAt(input[i] & 0x0F);
}
return output;
}
//I should replace this with: int("0x" + ...)
private function fromBase16(input:String):ByteArray
{
var output:ByteArray = new ByteArray;
const hex:Object = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10,'b':11,'c':12,'d':13,'e':14,'f':15,'A':10,'B':11,'C':12,'D':13,'E':14,'F':15};
const length:int = input.length;
var i:int = 0;
while(i < length) {
var byte:uint = (hex[input.charAt(i)] << 4);
i++;
if(i < length) {
byte += hex[input.charAt(i)];
i++;
}
output.writeByte(byte);
}
return output;
}
private function bArrayToBase64(input:ByteArray):String {
var base64Enc:Base64Encoder = new Base64Encoder;
base64Enc.encodeBytes(input);
return (base64Enc.toString());
}
private function stringToBase64(input:String):String {
try {
var base64Enc:Base64Encoder = new Base64Encoder;
base64Enc.encode(input);
return (base64Enc.toString());
} catch (e:Error) {
//Bug. This alert box never actually shows.
Alert.show("Error: " + e.message, "ERROR",4,this);
}
return ("ERROR: Could not encode value.");
}
private function fromBase64String(input:String):ByteArray {
var base64Dec:Base64Decoder = new Base64Decoder;
try {
base64Dec.decode(input);
return (base64Dec.toByteArray());
} catch (e:Error) {
//Although it isn't documented, the above functions can throw errors.
//Bug. The Alert box never actually shows.
Alert.show("ERROR: " + e.message,"Error",4,this);
}
return(null);
}
/**
private function initKeyAndIV():void
{
as3_crypto_wrapper.prng_seed(null);
if(null == aes128key)
aes128key = as3_crypto_wrapper.prng_random(16);
if(null == aes128iv)
aes128iv = as3_crypto_wrapper.prng_random(16);
}
*/
private function onSHA1():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.sha1_begin();
as3_crypto_wrapper.sha1_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.sha1_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
private function onSHA256():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.sha256_begin();
as3_crypto_wrapper.sha256_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.sha256_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
private function onMD5():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.md5_begin();
as3_crypto_wrapper.md5_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.md5_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
/**
private function onEncryptAES():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
initKeyAndIV();
var hdl:uint = as3_crypto_wrapper.aes128_cbc_encrypt_begin(aes128key, aes128iv);
var encrypted:ByteArray = as3_crypto_wrapper.symmetric_encrypt_update(hdl, buffer);
encrypted.writeBytes(as3_crypto_wrapper.symmetric_encrypt_finish(hdl));
//Not sure why they write out the buffer? Make sure it is clear?
this.stringOut.text = toBase16(buffer);
encrypted.position = 0;
this.hexOut.text = toBase16(encrypted);
this.base64out.text = bArrayToBase64(encrypted);
}
private function onDecryptAES():void
{
var buffer:ByteArray = fromBase16(this.hexOut.text);
var hdl:uint = as3_crypto_wrapper.aes128_cbc_decrypt_begin(aes128key, aes128iv);
var decrypted:ByteArray = as3_crypto_wrapper.symmetric_decrypt_update(hdl, buffer);
decrypted.writeBytes(as3_crypto_wrapper.symmetric_decrypt_finish(hdl));
this.hexOut.text = toBase16(decrypted);
decrypted.position = 0;
this.stringOut.text = decrypted.readUTF();
}
private function onGetRandomData():void
{
as3_crypto_wrapper.prng_seed(null);
var buffer:ByteArray = as3_crypto_wrapper.prng_random(20);
dataText.text = toBase16(buffer);
}
*/
private function onBase64Decode():void {
var bArray:ByteArray = fromBase64String(this.dataText.text);
if (bArray == null) {
this.stringOut.text = "ERROR! Could not decode Base64 data.";
this.hexOut.text = "";
this.base64out.text = "";
return;
}
this.stringOut.text = bArray.toString();
this.hexOut.text = this.toBase16(bArray);
this.base64out.text = "";
}
private function onBase64Encode():void {
var output:String = stringToBase64(this.dataText.text);
this.stringOut.text = "";
this.hexOut.text = "";
this.base64out.text = output;
}
private function onHexEncode():void {
var bArray:ByteArray = new ByteArray;
bArray.writeUTFBytes(this.dataText.text);
this.hexOut.text = toBase16(bArray);
this.stringOut.text = "";
this.base64out.text = "";
}
private function onHexDecode():void {
var bArray:ByteArray;
bArray = fromBase16(this.dataText.text);
var tmpString:String = "";
for (var i:int = 0; i < bArray.length; i++) {
if (bArray[i] > 31 && bArray[i] < 128) {
tmpString += String.fromCharCode(bArray[i].toString());
} else {
tmpString += "*";
}
}
this.stringOut.text = tmpString;
this.hexOut.text = "";
this.base64out.text = "";
}
private function onUrlEnc():void {
var regEx:RegExp = new RegExp("[a-zA-Z0-9\,\-\_\.\!\~\*\'\(\)]");
var pos:int = 0;
var orig:String = this.dataText.text;
var encStr:String = "";
while (pos < orig.length) {
if (!(regEx.test(orig.charAt(pos)))) {
var bArray:ByteArray = new ByteArray();
bArray[0] = int(orig.charCodeAt(pos));
encStr += "%" + toBase16(bArray).toUpperCase();
} else {
encStr += orig.charAt(pos);
}
pos++;
}
this.stringOut.text = encStr;
this.base64out.text = "";
this.hexOut.text = ""
}
private function onUrlDec():void {
var regEx:RegExp = new RegExp("\%[a-fA-F0-9]{2}");
var pos:int = 0;
var orig:String = this.dataText.text;
var decStr:String = "";
while (pos < orig.length) {
if (orig.charAt(pos) == "%" && regEx.test(orig.substr(pos,3))) {
var bArray:ByteArray = fromBase16(orig.charAt(pos+1) + orig.charAt(pos+2));
if (bArray[0] > 31 && bArray[0] < 128) {
decStr += String.fromCharCode(bArray[0].toString());
} else {
decStr += "*";
}
pos += 3;
} else {
decStr += orig.charAt(pos);
pos++;
}
}
this.stringOut.text = decStr;
this.base64out.text = "";
this.hexOut.text = ""
}
private function callEscape():void {
var decStr:String = escape(dataText.text);
this.stringOut.text = decStr;
//this.base64out.text = stringToBase64(decStr);
//var ba:ByteArray = new ByteArray();
//ba.writeUTFBytes(decStr);
this.base64out.text = "";
this.hexOut.text = "";
}
private function callUnescape():void {
var decStr:String = unescape(dataText.text);
this.stringOut.text = decStr;
this.base64out.text = stringToBase64(decStr);
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(decStr);
this.hexOut.text = toBase16(ba);
}
private function setFocusManager():void {
if (this.focusManager == null) {
this.fm = new FocusManager(this);
this.dataText.focusManager = this.fm;
this.stringOut.focusManager = this.fm;
this.base64out.focusManager = this.fm;
this.hexOut.focusManager = this.fm;
}
}
]]>
</fx:Script>
<s:Group id="decCanvas" width="529" height="467" initialize="setFocusManager()">
<s:Label x="10" y="10" text="Input"/>
<s:TextArea id="dataText" right="10" left="10" top="26" height="51"/>
<s:Button x="10" y="85" label="Digest SHA1" id="sha1" click="onSHA1()"/>
<s:Button x="10" y="115" label="Digest SHA256" id="sha256" click="onSHA256()"/>
<s:Button x="10" y="145" label="Digest MD5" id="md5" click="onMD5()"/>
<s:Button x="126" y="85" label="Base64 Encode" click="onBase64Encode()"/>
<s:Button x="126" y="115" label="Base64 Decode" click="onBase64Decode()"/>
<s:Button x="247" y="85" label="Hex Encode" click="onHexEncode()"/>
<s:Button x="246" y="115" label="Hex Decode" click="onHexDecode()" width="94"/>
<s:Button x="347" y="85" label="encodeURI" click="onUrlEnc()"/>
<s:Button x="348" y="115" label="decodeURI" click="onUrlDec()"/>
<s:Button x="435" y="85" label="escape" click="callEscape()"/>
<s:Button x="436" y="114" label="unescape" click="callUnescape()"/>
<s:Label x="10" y="186" text="String"/>
<s:TextArea id="stringOut" x="10" y="202" width="509" height="63"/>
<s:Label x="10" y="285" text="Hex16" width="43"/>
<s:TextArea id="hexOut" x="10" y="299" width="509" height="62"/>
<s:Label x="10" y="381" text="Base64"/>
<s:TextArea id="base64out" x="10" y="395" width="509" height="63"/>
</s:Group>
</s:Application>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.