5. Algoritma Enkripsi dan Dekripsi Base64 Menggunakan JavaScript di Delphi
Mencari Google: Enkripsi JavaScript Base64 akan menemukan banyak kode enkripsi Base64.
var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; var base64decodechars = array baru ( -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, -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, -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); function base64encode (str) { var out, i, len; var C1, C2, C3; len = str.length; i = 0; out = ""; while (i <len) { c1 = str.charcodeat (i ++) & 0xff; if (i == len) { out += base64encodechars.charat (C1 >> 2); out += base64encodechars.charat ((C1 & 0x3) << 4); out += "=="; merusak; } c2 = str.charcodeat (i ++); if (i == len) { out += base64encodechars.charat (C1 >> 2); out += base64encodechars.charat (((c1 & 0x3) << 4) | ((C2 & 0xf0) >> 4)); out += base64encodechars.charat ((c2 & 0xf) << 2); out += "="; merusak; } C3 = str.charcodeat (i ++); out += base64encodechars.charat (C1 >> 2); out += base64encodechars.charat (((c1 & 0x3) << 4) | ((C2 & 0xf0) >> 4)); out += base64encodechars.charat (((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6)); out += base64encodechars.charat (C3 & 0x } kembali; } function base64decode (str) { var C1, C2, C3, C4; var I, len, out; len = str.length; i = 0; out = ""; while (i <len) { / * c1 */ Mengerjakan { c1 = base64decodechars [str.charcodeat (i ++) & 0xff]; } while (i <len && c1 == -1); if (c1 == -1) merusak; / * c2 */ Mengerjakan { c2 = base64decodechars [str.charcodeat (i ++) & 0xff]; } while (i <len && c2 == -1); if (c2 == -1) merusak; out += string.fromCharCode ((c1 << 2) | ((c2 & 0x30) >> 4)); / * c3 */ Mengerjakan { c3 = str.charcodeat (i ++) & 0xff; if (c3 == 61) kembali; C3 = base64decodechars [C3]; } while (i <len && c3 == -1); if (c3 == -1) merusak; Out += String.FromCharCode (((C2 & 0XF) << 4) | ((C3 & 0x / * c4 */ Mengerjakan { C4 = str.charcodeat (i ++) & 0xff; if (c4 == 61) kembali; C4 = base64decodechars [C4]; } while (i <len && c4 == -1); if (c4 == -1) merusak; out += string.FromCharCode (((C3 & 0x03) << 6) | C4); } kembali; } fungsi utf16to8 (str) { var out, i, len, c; out = ""; len = str.length; untuk (i = 0; i <len; i ++) { c = str.charcodeat (i); if ((c> = 0x0001) && (c <= 0x out += str.charat (i); } lain jika (c> 0x07ff) { out += string.fromCharCode (0xe0 | ((c >> 12) & 0x out += string.fromCharCode (0x80 | ((c >> 6) & 0x out += string.fromCharCode (0x80 | ((c >> 0) & 0x } kalau tidak { out += string.fromCharCode (0xc0 | ((c >> 6) & 0x out += string.fromCharCode (0x80 | ((c >> 0) & 0x } } kembali; } fungsi utf8to16 (str) { var out, i, len, c; var char2, char3; out = ""; len = str.length; i = 0; while (i <len) { c = str.charcodeat (i ++); Switch (c >> 4) { Kasus 0: Kasus 1: Kasus 2: Kasus 3: Kasus 4: Kasus 5: Kasus 6: Kasus 7: // 0xxxxxxxx out += str.charat (i-1); merusak; Kasus 12: Kasus 13: // 110x xxxx 10xx xxxx char2 = str.charcodeat (i ++); out += string.fromCharCode (((C & 0x merusak; Kasus 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i ++); char3 = str.charcodeat (i ++); out += string.fromCharCode (((C & 0x ((Char2 & 0x ((Char3 & 0x merusak; } } kembali; } |
Bagaimana kita bisa menambahkan sepotong kode yang panjang dalam metode addCode ()?
1) Metode pertama: Tentukan kode panjang seperti itu ke string. Beban kerja ini jelas sangat besar dan rawan kesalahan.
2) Metode kedua: mendefinisikannya dalam notepad dan membacanya saat program berjalan. Keamanan sangat rendah dan mudah dimodifikasi dan menyebabkan kesalahan.
3) Metode ketiga: Simpan di DLL sebagai file sumber daya. Metode ini lebih cocok. Kami akan menggunakan metode ini untuk menghadapinya di bawah ini.
Langkah -langkah untuk menghasilkan DLL:
1) Buat notepad baru, tempel kode di atas, dan akhirnya simpan sebagai base64.txt.
2) Buat notepad baru, tulis kode berikut, dan akhirnya simpan sebagai base64.rc.
Base64file exefile "base64.txt" |
3) Buat file batch baru di direktori yang sama dan simpan sebagai base64.bat.
BRCC32.EXE BASE64.RC |
4) Kompilasi ke dalam Resource File Res: Klik dua kali untuk mengeksekusi base64.bat, dan setelah itu, file base64.Res akan dihasilkan, yang merupakan file sumber daya dari skrip JavaScript.
5) Buat DLL baru, simpan sebagai base64.dpr, tambahkan kode berikut, dan kemudian kompilasinya menjadi base64.dll.
basis perpustakaan64; {$ R base64.res} Mulai akhir. |
Dengan cara ini, file sumber daya kami dikemas ke dalam DLL dan selesai!
Selanjutnya, tugas kami adalah membaca kode JavaScript melalui DLL.
{Baca file sumber daya ke variabel string} Fungsi readResource (): string; Var Hinst: Thandle; Stream: Tresourcestream; Codestring: tstrings; Mulai Hasil: = ''; // Muat dll Hinst: = loadLibrary ('base64.dll'); Jika hinst = 0 maka keluar; Mencoba // Baca file sumber daya Stream: = tresourcestream.create (hinst, 'base64file', 'exefile'); Codestring: = tStringList.Create (); Mencoba // Simpan file sumber daya dalam daftar Codestring.loadFromStream (stream); // mengembalikan string Hasil: = codestring.text; Akhirnya Codestring.free; Stream.free; akhir; Akhirnya Freelibrary (hinst); akhir; akhir; |
Pekerjaan berikut adalah untuk menambahkan fungsi JavaScript khusus melalui addCode () dari kontrol scriptcontrol.
Inisialisasi Komponen Scriptcontrol, silakan merujuk ke aplikasi lanjutan dari ScriptControl di Delphi (i)
http://blog.csdn.net/juwuyi/archive/
Sc.addcode (readResource ()); // enkripsi Kata -kata: = 'Ini adalah bagian dari teks yang dienkripsi dengan base64'; //Hasil: Hasil: = sc.eval ('base64Encode (utf16to8 (' '+Words+' '')) '); // dekripsi Hasil: = sc.eval ('utf8to16 (base64decode (' '+Words+' '')) '); |
<Span style = "font-family: song font; m