全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2008
推到 Plurk!
推到 Facebook!

AES 的功能,是否可在 XE2 中順利運作

答題得分者是:tick228
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#1 引用回覆 回覆 發表時間:2011-12-23 14:13:30 IP:115.30.xxx.xxx 未訂閱
 
範例我有修改一些,但對 UniCODE 的字,解密後還是 ?? (問號),沒有修改成功

我是想做一支函數,對字串加密 , 不知有人用過此 AES 功能嗎 ?

您是如何在 XE2 可以讓 AES 順利運作, 煩請指點一下

還是有其它的套件可以使用 ?

謝謝您



------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
sryang
尊榮會員


發表:39
回覆:762
積分:920
註冊:2002-06-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2011-12-30 13:33:33 IP:118.171.xxx.xxx 訂閱
Unicode 部份的兩個函數必須修改

Elah_wide 改一行:
SS := TStringStream.Create(Value, TEncoding.Unicode);

搞定
<embed width="0" hidden="true" height="0" type="application/lingoes-npruntime-capture-word-plugin" id="lingoes_plugin_object"></embed>
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#3 引用回覆 回覆 發表時間:2011-12-30 19:42:21 IP:210.66.xxx.xxx 未訂閱

試過了,還是不行... , 繼續努力中....

===================引 用 sryang 文 章===================
Unicode 部份的兩個函數必須修改

Elah_wide 改一行:
SS := TStringStream.Create(Value, TEncoding.Unicode);

搞定
<embed width="0" hidden="true" height="0" type="application/lingoes-npruntime-capture-word-plugin" id="lingoes_plugin_object"></embed>
------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
tick228
高階會員


發表:1
回覆:47
積分:104
註冊:2003-11-03

發送簡訊給我
#4 引用回覆 回覆 發表時間:2012-05-25 15:08:27 IP:101.15.xxx.xxx 未訂閱
 您好, 不知這問題是否已解決?

由於此 Encrypt/Decrypt 功能都是用 Byte 來處理, 所以不適合用 Unicode 來使用 (要改很多地方)

若改用 UTF8String 即可快速解決(只需小改), 但是 Key 還是只能用 AnsiString (因有限制 Key 字元數)

底下程式是改過並測試沒問題

///////////////////////////////////////////////////////

{ -- wide 字串加密函數 -- }

// Key 要用 AnisString
function TForm1.Elah_wide(Value: String; Key: AnsiString; LKeyBit: TComboBox): String;
var
KeyBit: TKeyBit;
SS, DS: TStringStream;
Ms: TMemoryStream;
AESKey128: TAESKey128;
AESKey192: TAESKey192;
AESKey256: TAESKey256;
i: Integer;
begin
Result := '';
Value := Trim(Value); //去掉空格
// 將 Value 轉成 UTF8String
SS := TStringStream.Create(UTF8Encode(Value));
DS := TStringStream.Create('');
Ms := TMemoryStream.Create;
KeyBit := kb128;
i := StrToInt(ComboBox1.Text);
case i of
128: KeyBit := kb128;
192: KeyBit := kb192;
256: KeyBit := kb256;
end;
try
{ -- 128 位密匙最大長度為 16 個字元 -- }
if KeyBit = kb128 then
begin
FillChar(AESKey128, SizeOf(AESKey128), 0);
Move(PAnsiChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));
EncryptAESStreamECB_wide(SS, 0, AESKey128, MS);
end;
{ -- 192 位密匙最大長度為 24 個字元 -- }
if KeyBit = kb192 then
begin
FillChar(AESKey192, SizeOf(AESKey192), 0);
Move(PAnsiChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));
EncryptAESStreamECB(SS, 0, AESKey192, MS);
end;
{ -- 256 位密匙最大長度為 32 個字元 -- }
if KeyBit = kb256 then
begin
FillChar(AESKey256, SizeOf(AESKey256), 0);
Move(PAnsiChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));
EncryptAESStreamECB(SS, 0, AESKey256, MS);
end;
Ms.Position := 0;
EncodeStream(MS, DS);
Result := StrToHex_wide(DS.DataString);
finally
Ms.Free;
SS.Free;
DS.Free;
end;
end;
{ -- wide 字串解密函數 -- }
// Key 要用 AnisString
function TForm1.Dlah_wide(Value: String; Key: AnsiString; LKeyBit: TComboBox): String;
var
KeyBit: TKeyBit;
SS, DS: TStringStream;
Ms: TMemoryStream;
AESKey128: TAESKey128;
AESKey192: TAESKey192;
AESKey256: TAESKey256;
i: Integer;
begin
Result := '';
SS := TStringStream.Create(HexToStr_wide(Value));
// 接收要設成 UTF8
DS := TStringStream.Create('', TEncoding.UTF8);
Ms := TMemoryStream.Create;
KeyBit := kb128;
i := StrToInt(ComboBox1.Text);
case i of
128: KeyBit := kb128;
192: KeyBit := kb192;
256: KeyBit := kb256;
end;
try
DecodeStream(SS, Ms);
{ -- 128 位密匙最大長度為 16 個字元 -- }
if KeyBit = kb128 then
begin
FillChar(AESKey128, SizeOf(AESKey128), 0);
Move(PAnsiChar(Key)^, AESKey128, Min(SizeOf(AESKey128), Length(Key)));
DecryptAESStreamECB(MS, 0, AESKey128, DS);
end;
{ -- 192 位密匙最大長度為 24 個字元 -- }
if KeyBit = kb192 then
begin
FillChar(AESKey192, SizeOf(AESKey192), 0);
Move(PAnsiChar(Key)^, AESKey192, Min(SizeOf(AESKey192), Length(Key)));
DecryptAESStreamECB(MS, 0, AESKey192, DS);
end;
{ -- 256 位密匙最大長度為 32 個字元 -- }
if KeyBit = kb256 then
begin
FillChar(AESKey256, SizeOf(AESKey256), 0);
Move(PAnsiChar(Key)^, AESKey256, Min(SizeOf(AESKey256), Length(Key)));
DecryptAESStreamECB(MS, 0, AESKey256, DS);
end;
Result := Trim(DS.DataString);
finally
Ms.Free;
SS.Free;
DS.Free;
end;
end;

////////////////////////////////////////////////////////////////

請參考, 謝謝!!


編輯記錄
tick228 重新編輯於 2012-05-25 01:10:07, 註解 無‧
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#5 引用回覆 回覆 發表時間:2012-05-28 12:31:43 IP:210.66.xxx.xxx 未訂閱
 Tick228 您好,感謝您的分享

我之前試的方法,都沒有成功過....感覺沒那麼單純 !?

您的範例我會找個時間測試,

謝謝您
------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
系統時間:2024-04-19 11:06:16
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!