Advanced Applications in ScriptControl in Delphi (2)
5, Delphi uses JavaScript's Base64's addendomite algorithm
Search Google: JavaScript base64 encryption will find a lot of Base64 encrypted code, the following is the code you searched (the following code is copyright):
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /"; var base64DecodeChars = new Array (-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1 , -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, 1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1); Function Base64encode (STR) {var Out, I, Len; Var C1 , C2, C3; Len = Str.Length; I = 0; OUT = ""; While (I
Base64encodechars.charat (((C2 & 0xF) << 2) | ((C3 & 0xC0) >> 6)); out = base64encodechars.charat (c3 & 0x3f);} return out;} function base64decode (str) { VAR C1, C2, C3, C4; VAR I, LEN, OUT; LEN = Str.Length; I = 0; OUT = ""; While (I 3c) >> 2)); / * C4 * / do {C4 = Str.Charcodeat (i ) & 0xff; if (c4 == 61) Return Out; C4 = Base64DecodeChars [C4];} while (i 007f) {OUT = Str.Charat (i);} else if (c> 0x07ff) {OUT = String.Fromcharcode (0xE0 | ((C >> 12) & 0x0f); out = string.fromcharcode (0x80 | ((C >> 6) & 0x 3F)); OUT = String.Fromcharcode (0x80 | ((c >> 0) & 0x 3f);} else {OUT = String.Fromcharcode (0xc0 | ((c >> 6) & 0x 1f)); OUT = String.Fromcharcode (0x80 | ((c >> 0) & 0x 3F));}} Return Out;} Function UTF8TO16 (STR) {VAR OUT, I, LEN, C; VAR CHAR2, CHAR3; OUT = "" "" "" "; len = str.length; i = 0; While (I 1F) << 6) | (Char2 & 0x 3F); Break; Case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i ); char3 = str.charcodeat (i ); out = string.fromcharcode (((C & 0x) 0F) << 12) | ((Char2 & 0x 3F) << 6) | ((Char3 & 0x 3F) << 0)); Break;}} Return Out;} How can we join in such a piece of code? How can we join in the addCode () method? 1) First method: define such a long code to a string. This workload is definitely very large, and it is easy to make mistakes. 2) Second method: Define it into a notepad, read when the program is running. The security is very low, it is easy to be modified, resulting in an error. 3) The third method: store it in a DLL in the form of a resource file. This method is more suitable. Let's use this method to handle it. The step of generating a DLL: 1) Newly built a notepad, paste the code above, and finally save it to base64.txt. 2) Create a new notebook, write the following code, and finally saved as Base64.rc. Base64File Exefile "Base64.txt" 3) Creating a batch file with a directory, saved as base64.bat. BRCC32.EXE BASE64.RC 4) Compile into resource files: Double-click to execute Base64.bat, will generate a base64.res file, this is the resource file of the JavaScript script. 5) Creating a new DLL, saved as Base64.dpr, plus the following code, then compile it into base64.dll. Library base64; {$ r base64.res} begin end. In this way, our resource file is packaged into a DLL! Below, our job is to read the JavaScript code through the DLL. {Read resource file to a string variable} Function Readresourc (): string; var hinst: thandle; stream: tresovern:; codestring: tstrings; begin result: = '; // Load DLL Hinst: = loadingLibrary (' base64.dll '); If hinst = 0 THEN EXIT; TRY // Read Resource Files Stream: = TRESOURCESTREAM.CREATE (Hinst,' Base64File ',' Exefile '); Code'); CodeString: = TstringList.create (); TRY // Put the resource file Store codestring.loadfromstream (stream) in the list; // Returns a string result: = CodeString.Text; Finally CodeString.Free; Stream.Free; End; Finally Freelibrary (HinSt); end; The following works is to add a custom JavaScript function by addControl controls (). Initialize the ScriptControl component, please see the advanced apps of ScriptControl in Delphi (1) http://blog.9cbs.net/juwuyi/archive/ 2005/03/19 / 324041.ASPX Sc.addcode (ReadResourc ()); // Encrypted Words: = 'This is a text with BASE64 to encrypt'; // Result: 6L Z5PIV5LIA5Q6155SOQKFTRTY 06L b6kgm5yqg 5A G55QE5PAH 5A2XRESULT: = SC.EVAL ('base64encode (' '' words '')) '); // Decryption Result: = sc.eval (' UTF8TO16 (Base64Decode ('' Words '')) '); The picture effect is as follows: This is finished! ----------------------- Joecom (juwuyi) 2005-03-21