5. Base64 Algorithme de cryptage et de décryptage utilisant JavaScript à Delphi
La recherche de Google: JavaScript Base64 Encryption trouvera de nombreux codes de cryptage Base64.
var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 + /"; var base64decodechars = nouveau tableau ( -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 = ""; tandis que (i <len) { c1 = str.charcodeat (i ++) & 0xff; si (i == len) { OUT + = Base64Encodechars.Charat (C1 >> 2); out + = Base64Encodechars.Charat ((C1 & 0x3) << 4); out + = "=="; casser; } c2 = str.charcodeat (i ++); si (i == len) { OUT + = Base64Encodechars.Charat (C1 >> 2); out + = Base64Encodechars.Charat (((C1 & 0x3) << 4) | ((C2 & 0xf0) >> 4)); out + = Base64Encodechars.Charat ((C2 & 0xf) << 2); out + = "="; casser; } 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 } retourner; } function base64decode (str) { var C1, C2, C3, C4; var i, len, out; Len = str.length; i = 0; out = ""; tandis que (i <len) { / * c1 * / faire { c1 = base64DodeChars [str.charcodeat (i ++) & 0xff]; } while (i <len && c1 == -1); if (c1 == -1) casser; / * c2 * / faire { c2 = base64DodeChars [str.charcodeat (i ++) & 0xff]; } while (i <len && c2 == -1); if (c2 == -1) casser; out + = string.fromCharcode ((c1 << 2) | ((C2 & 0x30) >> 4)); / * c3 * / faire { c3 = str.charcodeat (i ++) & 0xff; if (c3 == 61) retourner; C3 = base64decodechars [C3]; } while (i <len && c3 == -1); if (c3 == -1) casser; out + = String.fromCharcode (((C2 & 0xf) << 4) | ((C3 & 0x / * c4 * / faire { c4 = str.charcodeat (i ++) & 0xff; if (c4 == 61) retourner; C4 = base64decodechars [C4]; } while (i <len && c4 == -1); if (c4 == -1) casser; out + = string.fromCharcode (((C3 & 0x03) << 6) | C4); } retourner; } fonction utf16to8 (str) { var out, i, len, c; out = ""; Len = str.length; pour (i = 0; i <len; i ++) { c = str.charcodeat (i); if ((c> = 0x0001) && (c <= 0x out + = str.charat (i); } else if (c> 0x07ff) { out + = string.fromCharcode (0xe0 | ((c >> 12) & 0x out + = string.fromCharcode (0x80 | ((c >> 6) & 0x out + = string.fromCharcode (0x80 | ((c >> 0) & 0x } autre { out + = string.fromCharcode (0xc0 | ((c >> 6) & 0x out + = string.fromCharcode (0x80 | ((c >> 0) & 0x } } retourner; } fonction utf8to16 (str) { var out, i, len, c; var char2, char3; out = ""; Len = str.length; i = 0; tandis que (i <len) { c = str.charcodeat (i ++); commutateur (c >> 4) { Cas 0: Cas 1: Cas 2: Cas 3: Cas 4: Cas 5: Cas 6: Cas 7: // 0xxxxxxxx out + = str.charat (i-1); casser; Cas 12: Cas 13: // 110x xxxx 10xx xxxx char2 = str.charcodeat (i ++); out + = string.fromcharcode ((c & 0x casser; Cas 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i ++); char3 = str.charcodeat (i ++); out + = string.fromcharcode ((c & 0x ((Char2 & 0x ((Char3 & 0x casser; } } retourner; } |
Comment pouvons-nous ajouter un morceau de code aussi long dans la méthode d'addcode ()?
1) La première méthode: définissez un code aussi long sur une chaîne. Cette charge de travail est définitivement très grande et sujet aux erreurs.
2) La deuxième méthode: le définissez-le dans un bloc-notes et lisez-le lorsque le programme est en cours d'exécution. La sécurité est très faible et il est facile d'être modifié et de conduire à des erreurs.
3) La troisième méthode: stockez-la dans une DLL en tant que fichier de ressources. Cette méthode est plus appropriée. Nous utiliserons cette méthode pour y faire face ci-dessous.
Étapes pour générer une DLL:
1) Créez un nouveau bloc-notes, collez le code ci-dessus et enfin enregistrez-le en base64.txt.
2) Créez un nouveau bloc-notes, écrivez le code suivant et enfin enregistrez-le en base64.rc.
Base64File EXEFILE "Base64.txt" |
3) Créez un nouveau fichier de lots dans le même répertoire et enregistrez-le en tant que base64.bat.
BRCC32.exe Base64.RC |
4) Compiler dans Resource File Res: Double-cliquez pour exécuter la base64.bat, et après cela, un fichier Base64.res sera généré, qui est le fichier de ressource du script JavaScript.
5) Créez une nouvelle DLL, enregistrez-la sous le nom de base64.dpr, ajoutez le code suivant, puis compilez-le dans la base64.dll.
bibliothèque Base64; {$ R base64.res} Commencer fin. |
De cette façon, notre fichier de ressources est emballé dans une DLL et il est terminé!
Ensuite, notre travail consiste à lire le code JavaScript via DLLS.
{Lire le fichier de ressources dans une variable de chaîne} Fonction readResource (): String; Var Hinst: Thandle; Stream: tresourcestream; CodeString: tStrings; Commencer Résultats: = ''; // Chargez la DLL Hinst: = loadLibrary ('base64.dll'); Si hinst = 0 alors sortez; Essayer // Lire le fichier de ressources Stream: = tresourcestream.create (hinst, «base64file», «exefile»); CodeString: = tStringList.create (); Essayer // Enregistrez le fichier de ressources dans la liste CodeString.LoadFromStream (Stream); // renvoie une chaîne Résultat: = CodeString.Text; Enfin CodeString.free; Stream.free; fin; Enfin Freelibrary (Hinst); fin; fin; |
Le travail suivant consiste à ajouter des fonctions JavaScript personnalisées via AddCode () du contrôle ScriptControl.
Initialiser le composant ScriptControl, veuillez vous référer à l'application avancée de ScriptControl dans Delphi (I)
http://blog.csdn.net/juwuyi/archive/
Sc.addDcode (readResource ()); // cryptage Mots: = 'Ceci est un morceau de texte crypté avec Base64'; //Résultat: Résultat: = SC.Eval ('Base64Encode (UTF16TO8 (' '+ Words +' '')) '); // décryptage Résultat: = sc.eval ('utf8to16 (base64decode (' '+ mots +' '')) '); |
<Span Style = "Font-Family: Song Font; M