5。DelphiでJavaScriptを使用したBase64暗号化と復号化アルゴリズム
Googleの検索:JavaScript Base64暗号化は、多くのBase64暗号化コードを検索します。
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、-1、-1、-1、-1、-1、62、-1、-1、63、 52、53、54、55、56、57、58、59、60、61、-1、-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); 関数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 += "=="; 壊す; } 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 += "="; 壊す; } 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 } 戻ります。 } 関数base64decode(str){ VAR C1、C2、C3、C4; var i、len、out; len = str.length; i = 0; out = ""; while(i <len){ / * c1 */ する { C1 = base64DecodeChars [str.CharCodeat(i ++)&0xff]; } while(i <len && c1 == -1); if(c1 == -1) 壊す; / * c2 */ する { C2 = base64DecodeChars [str.CharCodeat(i ++)&0xff]; } while(i <len && c2 == -1); if(c2 == -1) 壊す; out += string.fromCharcode((c1 << 2)|((c2&0x30)>> 4)); / * c3 */ する { c3 = str.charcodeat(i ++)&0xff; if(c3 == 61) 戻ります。 C3 = base64DecodeChars [C3]; } while(i <len && c3 == -1); if(c3 == -1) 壊す; out += string.fromCharcode(((c2&0xf)<< 4)|((c3&0x) / * c4 */ する { c4 = str.charcodeat(i ++)&0xff; if(c4 == 61) 戻ります。 C4 = base64DecodeChars [C4]; } while(i <len && c4 == -1); if(c4 == -1) 壊す; out += string.fromCharcode(((c3&0x03)<< 6)| c4); } 戻ります。 } 関数utf16to8(str){ var out、i、len、c; out = ""; len = str.length; for(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 } それ以外 { out += string.fromCharcode(0xc0 |((c >> 6)&0x out += string.fromCharcode(0x80 |((c >> 0)&0x } } 戻ります。 } 関数utf8to16(str){ var out、i、len、c; var char2、char3; out = ""; len = str.length; i = 0; while(i <len){ c = str.charcodeat(i ++); スイッチ(C >> 4) { ケース0:ケース1:ケース2:ケース3:ケース4:ケース5:ケース6:ケース7: // 0xxxxxxxx out += str.charat(i-1); 壊す; ケース12:ケース13: // 110x xxxx 10xx xxxx char2 = str.charcodeat(i ++); out += string.fromCharcode(((c&0x。) 壊す; ケース14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat(i ++); char3 = str.charcodeat(i ++); out += string.fromCharcode(((c&0x。) ((char2&0x ((char3&0x 壊す; } } 戻ります。 } |
AddCode()の方法にこのような長いコードを追加するにはどうすればよいですか?
1)最初の方法:このような長いコードを文字列に定義します。このワークロードは間違いなく非常に大きく、エラーが発生しやすいです。
2)2番目の方法:メモ帳で定義し、プログラムが実行されているときに読み取ります。セキュリティは非常に低く、変更が簡単でエラーにつながるのは簡単です。
3)3番目の方法:リソースファイルとしてDLLに保存します。この方法はより適しています。この方法を使用して、以下で処理します。
DLLを生成する手順:
1)新しいメモ帳を作成し、上記のコードを貼り付け、最後にbase64.txtとして保存します。
2)新しいメモ帳を作成し、次のコードを書き、最後にbase64.rcとして保存します。
base64file exefile "base64.txt" |
3)同じディレクトリに新しいバッチファイルを作成し、base64.batとして保存します。
brcc32.exe base64.rc |
4)リソースファイルにコンパイルするRES:ダブルクリックしてbase64.batを実行し、その後、base64.resファイルが生成されます。これはJavaScriptスクリプトのリソースファイルです。
5)新しいDLLを作成し、base64.dprとして保存し、次のコードを追加してから、base64.dllにコンパイルします。
ライブラリBase64; {$ r base64.res} 始める 終わり。 |
このようにして、リソースファイルはDLLにパッケージ化されており、完成しています!
次に、私たちの仕事は、DLLを介してJavaScriptコードを読むことです。
{文字列変数にリソースファイルを読み取る} function readresource():string; var Hinst:Thandle; ストリーム:TresourCestream; Codestring:tstrings; 始める 結果:= ''; // DLLをロードします hinst:= loadLibrary( 'base64.dll'); Hinst = 0の場合、終了します。 試す //リソースファイルを読み取ります ストリーム:= tresourcestream.create(hinst、 'base64file'、 'efefile'); codestring:= tstringlist.create(); 試す //リスト内のリソースファイルを保存します codestring.loadfromstream(stream); //文字列を返します 結果:= codestring.text; ついに codestring.free; stream.free; 終わり; ついに Freelibrary(Hinst); 終わり; 終わり; |
以下の作業は、ScriptControlコントロールのAddCode()を介してカスタムJavaScript関数を追加することです。
ScriptControlコンポーネントの初期化、Delphi(i)のScriptControlの高度なアプリケーションを参照してください
http://blog.csdn.net/juwuyi/archive/
sc.addcode(readresource()); //暗号化 単語:= 'これは、base64'で暗号化されたテキストです。 //結果: 結果:= sc.eval( 'base64encode(utf16to8(' '+words+' '' '' '')) '); //復号化 結果:= sc.eval( 'utf8to16(base64decode(' '+words+' '' '') '); |
<span style = "font-family:song font; m