分享
 
 
 

IDEA數據加密算法實現

王朝other·作者佚名  2007-09-15
窄屏简体版  字體: |||超大  

IDEA數據加密算法介紹之續篇,代碼實現:

http://blog.csdn.net/CXXSoft/archive/2006/08/23/1109279.aspx

5、 源碼:

//*******************************************************************************

* IDEA數據加密算法1.0版本

* 開發作者: 成曉旭

* 項目簡述: IDEA數據加密算法1.0版本

* 啟動時間: 2000年05月08日

* 完成時間: 2000年01月14日 20:09:00 <1個晚上>

*

* 開發環境: Windows98

* 開發工具: Borland Delphi5

*

* 文件名稱: IDEA.pas

* 簡 介: IDEA數據加密算法1.0版本

*

* 備註: 任何人使用此文件時,請保留此段自述文件,謝謝!

*

******************************************************************************/

unit IDEA;

interface

uses

Windows, Messages, SysUtils, Classes,Dialogs;

function BinToHex(strBin:string):string;

function HexToTen(strHex:string):int64;

function LeftMove(S:string;P:integer):String;

function ModAdd(intAdd1,intAdd2:int64):int64;

function ModMul(intMul1,intMul2:int64;intFlag:integer):int64;

function ModXor(intXor1,intXor2:int64):int64;

procedure CreatePassword(strPassword:string;VAR arrPassword:array of int64;VAR arrOpenPassword:array of int64); //產生加密子密鑰函數

procedure GetOpenPassword(intSource:int64;VAR intResult:int64;Flag:integer); //求取解密子密鑰函數

procedure CreateKeyTable(); //產生加、解密子密鑰之間的對應關系表

procedure Riddle(arrPassword:array of int64;arrRiddleKey:array of int64;VAR arrOpenPassword:array of int64);StdCall;export; //IDEA數據解密函數

procedure CreateOpenPassword(VAR arrOpenPassKey:array of int64); //產生解密子密鑰函數

procedure CreateKey(VAR arrKey:array of int64;VAR arrOpenKey:array of int64;VAR strOutKey:string);StdCall;export; //產生加密密鑰函數

implementation

Const strNum:array[1..16] of string[4]=('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1010','1011','1100','1101','1110','1111');

Const chrHex:array[1..16] of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

var

g_arr_Ten_Password:array[1..52] of int64;

g_arr_Flag:array[1..52] of integer;

g_arr_Table:array[1..52] of integer;

...{--------------------------------------------------------------------}

procedure CreateKey(VAR arrKey:array of int64;VAR arrOpenKey:array of int64;VAR strOutKey:string);StdCall;export;

var

intKey,intLoop1,intLoop2,intLoop3:integer;

hexKey,ch,strTemp:string;

strKey:string[128];

begin

strKey:='';

Randomize;

for intLoop1:=1 to 8 do

begin

intKey:=Random(65536);

hexKey:=IntToHex(intKey,4);

strTemp:='';

for intLoop2:=1 to 4 do

begin

ch:=Copy(hexKey,intLoop2,1);

for intLoop3:=1 to 16 do

begin

if ch = chrHex[intLoop3] then

begin

ch:= strNum[intLoop3];

break;

end;

end;

strTemp:=strTemp+ch;

end;

strKey:=strKey+strTemp;

end;

strOutKey:=strKey;

CreatePassword(strKey,arrKey,arrOpenKey); //調用產生加密子密鑰過程

end;

...{--------------------------------------------------------------------}

function BinToHex(strBin:string):string;

var

intLoop1,intLoop2:integer;

strTemp,strResult:string;

begin

intLoop1:=1;

strResult:='';

if Length(strBin) <> 16 then ShowMessage('二進制數據長度有錯!');

while (intLoop1<=16) do

begin

strTemp:=Copy(strBin,intLoop1,4);

for intLoop2:=1 to 16 do

if strTemp=strNum[intLoop2] then

begin

strTemp:=chrHex[intLoop2];

break;

end;

strResult:=strResult+strTemp;

intLoop1:=intLoop1 + 4;

end;

BinToHex:=strResult;

end;

...{--------------------------------------------------------------------}

function HexToTen(strHex:string):int64; //十六進制轉十進制

var

intLoop1,intLoop2,intTemp:integer;

intResult:int64;

strTemp:string;

begin

intResult:=0;

intTemp:=0;

if Length(strHex) <> 4 then ShowMessage('十六進制數據長度有錯!');

for intLoop1:=1 to 4 do

begin

Case intLoop1 of

1:intTemp:=4096;

2:intTemp:=256;

3:intTemp:=16;

4:intTemp:=1

end;

strTemp:=Copy(strHex,intLoop1,1);

for intLoop2:=1 to 16 do

if UpperCase(strTemp) = chrHex[intLoop2] then

begin

intResult:=intResult+(Int64(intLoop2) - 1)*Int64(intTemp);

break;

end;

end;

HexToTen:=intResult;

end;

...{--------------------------------------------------------------------}

function LeftMove(S:string;P:integer):String;

var

int_Len,i:integer;

str_Result,str_Num:string;

begin

int_Len:=length(s);

str_Num:=S;

str_Result:='';

if int_Len <= P then

str_Result :=S

else

begin

for i:=P+1 to int_Len do

str_Result:=str_Result+copy(str_Num,i,1);

str_Result:=str_Result+copy(str_Num,1,p);

end;

LeftMove:=Trim(str_Result);

end;

...{--------------------------------------------------------------------}

function ModAdd(intAdd1,intAdd2:int64):int64;

begin

ModAdd:=(intAdd1 + intAdd2) mod 65536; //模65536求和

end;

function ModMul(intMul1,intMul2:int64;intFlag:integer):int64;

var intTemp:int64;

begin

intTemp:=(intMul1 * intMul2) mod 65537;

if intFlag = 0 then

begin

if intMul1 = 0 then intTemp:=65537 - intMul2;

if intMul2 = 0 then intTemp:=65537 - intMul1;

if intTemp = 65536 then intTemp:=0;

if intTemp = 65537 then intTemp:=1;

end

else

begin

if intMul1 = 0 then intTemp:=65537 - intMul2;

if intMul2 = 0 then intTemp:=65537 - intMul1;

if intTemp = 0 then intTemp:=65536;

if intTemp = 65537 then intTemp:=1;

if intTemp = 65536 then intTemp:=0;

end;

ModMul:=intTemp;

end;

function ModXor(intXor1,intXor2:int64):int64;

begin

ModXor:= intXor1 xor intXor2;

end;

...{--------------------------------------------------------------------}

procedure CreatePassword(strPassword:string;VAR arrPassword:array of int64;VAR arrOpenPassword:array of int64);

var

strKey:string;

strTemp:array[1..52] of string[4];

intStart,intCount:integer;

begin

strKey:=strPassword; //

intCount:=1;

intStart:=1;

...{--------------------產生52個16bit的加密子密鑰部分-------------------}

while (intCount<=52) do

begin

strTemp[intCount]:=BinToHex(Copy(strKey,intStart,16));

intStart:=intStart+16;

intCount:=intCount+1;

if ((intCount mod 8) = 1) then

begin

strKey:=LeftMove(strKey,25);

intStart:=1;

end;

end;

intCount:=1;

while (intCount<=52) do

begin

arrPassword[intCount-1]:=HexToTen(strTemp[intCount]);

g_arr_Ten_Password[intCount]:=arrPassword[intCount-1];

intCount:=intCount+1;

end;

CreateOpenPassword(arrOpenPassword);

...{--------------------產生52個16bit的加密子密鑰部分-------------------}

end;

...{--------------------------------------------------------------------}

procedure GetOpenPassword(intSource:int64;VAR intResult:int64;Flag:integer);

var

int_Source,int_Result,int_Mod_Value:int64;

int_Loop:integer;

begin

int_Source:=intSource;

...{--------------------求取每個加密子密鑰相應的解密子密鑰部分-------------------}

Case Flag of

0: intResult:=int_Source;

1: intResult:=65536-int_Source;

2:

begin

if int_Source=0 then

intResult:=int_Source

else

for int_Loop:=1 to 65536 do

begin

int_Result:=Int64(int_Loop)*65537+1;

int_Mod_Value:=int_Result mod int_Source;

if int_Mod_Value = 0 then

begin

int_Result:=int_Result div int_Source;

intResult:=int_Result;

break;

end;

end;

end;

end;

...{--------------------求取每個加密子密鑰相應的解密子密鑰部分-------------------}

end;

...{--------------------------------------------------------------------}

procedure CreateKeyTable();

var

int_Count_CreatePassword,int_Count_OpenPassword,int_ExChange:integer;

begin

...{----------------求取在產生解密子密鑰時所用的參數標誌對應表部分-------------}

for int_Count_CreatePassword:=52 downto 1 do

Case int_Count_CreatePassword mod 6 of

0,5:begin g_arr_Flag[int_Count_CreatePassword]:=0; end;

2,3:begin g_arr_Flag[int_Count_CreatePassword]:=1; end;

1,4:begin g_arr_Flag[int_Count_CreatePassword]:=2; end;

end;

...{----------------求取在產生解密子密鑰時所用的參數標誌對應表部分-------------}

...{----------------求取每個加密子密鑰與相應解密子密鑰間的對應關系映射表部分-------------}

int_Count_CreatePassword:=52;

int_Count_OpenPassword:=1;

while (int_Count_CreatePassword>=1) do

begin

Case int_Count_CreatePassword mod 6 of

1,2,3,4:

begin

g_arr_Table[int_Count_OpenPassword]:=int_Count_CreatePassword-3;

g_arr_Table[int_Count_OpenPassword+1]:=int_Count_CreatePassword-1;

g_arr_Table[int_Count_OpenPassword+2]:=int_Count_CreatePassword-2;

g_arr_Table[int_Count_OpenPassword+3]:=int_Count_CreatePassword;

if (int_Count_CreatePassword >=48) or (int_Count_CreatePassword <=4 ) then //首輪和末輪處理不需交換,故要再換回來

begin

int_ExChange:=g_arr_Table[int_Count_OpenPassword+1];

g_arr_Table[int_Count_OpenPassword+1]:=g_arr_Table[int_Count_OpenPassword+2];

g_arr_Table[int_Count_OpenPassword+2]:=int_ExChange;

end;

int_Count_CreatePassword:=int_Count_CreatePassword-4;

int_Count_OpenPassword:=int_Count_OpenPassword+4;

end;

0,5:

begin

g_arr_Table[int_Count_OpenPassword]:=int_Count_CreatePassword-1;

g_arr_Table[int_Count_OpenPassword+1]:=int_Count_CreatePassword;

int_Count_CreatePassword:=int_Count_CreatePassword-2;

int_Count_OpenPassword:=int_Count_OpenPassword+2;

end;

end;

end;

...{----------------求取每個加密子密鑰與相應解密子密鑰間的對應關系映射表部分-------------}

end;

...{--------------------------------------------------------------------}

procedure CreateOpenPassword(VAR arrOpenPassKey:array of int64);

var

int_Loop:integer;

arrTemp:array[1..52] of int64;

begin

...{-------------通過調用產生加、解密子密鑰對應關系函數產生對應關系表部分------------------}

CreateKeyTable;

...{-------------通過調用求解解密子密鑰函數產生解密子密鑰部分------------------}

for int_Loop:=1 to 52 do

begin

GetOpenPassword(g_arr_Ten_Password[int_Loop],arrTemp[g_arr_Table[int_Loop]],g_arr_Flag[int_Loop]);

end;

for int_Loop:=1 to 52 do

arrOpenPassKey[int_Loop-1]:=arrTemp[int_Loop];

...{-------------通過調用求解解密子密鑰函數產生解密子密鑰部分------------------}

end;

...{--------------------------------------------------------------------}

procedure Riddle(arrPassword:array of int64;arrRiddleKey:array of int64;VAR arrOpenPassword:array of int64);

var

Count,i:integer;

V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13,V14,C1,C2,C3,C4,ExChange:int64;

P:array[1..4] of int64;

begin

...{------------------------數據 "解密" 算法實現部分------------------}

for i:=Low(arrPassword) to High(arrPassword) do

P[i+1]:=arrPassword[i];

Count:=0;

while (Count<48) do

begin

V1:= ModMul(P[1],arrRiddleKey[Count],1); //相乘 1

Inc(Count);

V2:= ModAdd(P[2],arrRiddleKey[Count]); //相加 2

Inc(Count);

V3:= ModAdd(P[3],arrRiddleKey[Count]); //相加 3

Inc(Count);

V4:= ModMul(P[4],arrRiddleKey[Count],1); //相乘 4

Inc(Count);

V5:= ModXor(V1,V3); //異或 5

V6:= ModXor(V2,V4); //異或 6

V7:= ModMul(V5,arrRiddleKey[Count],1); //相乘 7

Inc(Count);

V8:= ModAdd(V6,V7); //相加 8

V9:= ModMul(V8,arrRiddleKey[Count],1); //相乘 9

Inc(Count);

V10:=ModAdd(V7,V9); //相加 10

V11:=ModXor(V1,V9); //異或 11

V12:=ModXor(V2,V10); //異或 12

V13:=ModXor(V3,V9); //異或 13

V14:=ModXor(V4,V10); //異或 14

P[1]:=V11;

P[2]:=V13; //註意:交換中間兩個輸出結果

P[3]:=V12; //註意:交換中間兩個輸出結果

P[4]:=V14;

end; //結束8輪計算

ExChange:=P[2];

P[2]:=P[3];

P[3]:=ExChange; //因為最後一輪無需轉換,所以必須把P[2],P[3]再次轉換回來

C1:= ModMul(P[1],arrRiddleKey[48],1); //相乘 1

C2:= ModAdd(P[2],arrRiddleKey[49]); //相加 2

C3:= ModAdd(P[3],arrRiddleKey[50]); //相加 3

C4:= ModMul(P[4],arrRiddleKey[51],1); //相乘 4

...{------------------------數據 "解密" 算法實現部分------------------}

arrOpenPassword[Low(arrOpenPassword)]:=C1;

arrOpenPassword[Low(arrOpenPassword)+1]:=C2;

arrOpenPassword[Low(arrOpenPassword)+2]:=C3;

arrOpenPassword[Low(arrOpenPassword)+3]:=C4;

//數據解密成功!

end;

...{--------------------------------------------------------------------}

end.

 
 
 
免責聲明:本文為網絡用戶發布,其觀點僅代表作者個人觀點,與本站無關,本站僅提供信息存儲服務。文中陳述內容未經本站證實,其真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,並請自行核實相關內容。
2023年上半年GDP全球前十五強
 百态   2023-10-24
美眾議院議長啟動對拜登的彈劾調查
 百态   2023-09-13
上海、濟南、武漢等多地出現不明墜落物
 探索   2023-09-06
印度或要將國名改為「巴拉特」
 百态   2023-09-06
男子為女友送行,買票不登機被捕
 百态   2023-08-20
手機地震預警功能怎麽開?
 干货   2023-08-06
女子4年賣2套房花700多萬做美容:不但沒變美臉,面部還出現變形
 百态   2023-08-04
住戶一樓被水淹 還衝來8頭豬
 百态   2023-07-31
女子體內爬出大量瓜子狀活蟲
 百态   2023-07-25
地球連續35年收到神秘規律性信號,網友:不要回答!
 探索   2023-07-21
全球鎵價格本周大漲27%
 探索   2023-07-09
錢都流向了那些不缺錢的人,苦都留給了能吃苦的人
 探索   2023-07-02
倩女手遊刀客魅者強控制(強混亂強眩暈強睡眠)和對應控制抗性的關系
 百态   2020-08-20
美國5月9日最新疫情:美國確診人數突破131萬
 百态   2020-05-09
荷蘭政府宣布將集體辭職
 干货   2020-04-30
倩女幽魂手遊師徒任務情義春秋猜成語答案逍遙觀:鵬程萬裏
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案神機營:射石飲羽
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案昆侖山:拔刀相助
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案天工閣:鬼斧神工
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案絲路古道:單槍匹馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:與虎謀皮
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案鎮郊荒野:指鹿為馬
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:小鳥依人
 干货   2019-11-12
倩女幽魂手遊師徒任務情義春秋猜成語答案金陵:千金買鄰
 干货   2019-11-12
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有