delphi des算法源程序
{
调用时,只需调用以下两个函数。
函数EncryStr(Str,Key:String):String;
function DecryStr(Str,Key:String):String;
函数EncryStrHex(Str,Key:String):String;
函数DecryStrHex(StrHex,Key:String):String;
}
连接
使用
系统工具、变体、结构工具;
类型
TKeyByte = array[0..5]的字节;
TDesMode = (dmEncry,dm decry);
函数EncryStr(Str,Key:String):String;
function DecryStr(Str,Key:String):String;
函数EncryStrHex(Str,Key:String):String;
函数DecryStrHex(StrHex,Key:String):String;
常数
BitIP:数组[0..63]的Byte = //初始值设置为IP。
(57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7,
56, 48, 40, 32, 24, 16, 8, 0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6 );
BitCP:数组[0..63] of Byte = //逆初始IP-1。
( 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25,
32, 0, 40, 8, 48, 16, 56, 24 );
Bitexp:数组[0..47] of integer =//bit选择函数e。
( 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9,10,
11,12,11,12,13,14,15,16,15,16,17,18,19,20,19,20,
21,22,23,24,23,24,25,26,27,28,27,28,29,30,31,0 );
Bitpm:数组[0..byte =//置换函数p的31]
( 15, 6,19,20,28,11,27,16, 0,14,22,25, 4,17,30, 9,
1, 7,23,13,31,26, 2, 8,18,12,29, 5,21,10, 3,24 );
Sbox:数组[0..数组[0的7]..63] of byte =//s box
( ( 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ),
( 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ),
( 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ),
( 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ),
( 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ),
( 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ),
( 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 ),
( 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 ) );
Bitpmc1:数组[0...55]of byte =//选择替换PC-1。
( 56, 48, 40, 32, 24, 16, 8,
0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3 );
Bitpmc2:数组[0...47]of byte =//选择替换PC-2。
( 13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31 );
定义变量
子项:数组[0..TKeyByte的15];
履行
过程initPermutation(变量inData:字节数组);
定义变量
newData:数组[0..7]的字节;
I:整数;
开始
FillChar(newData,8,0);
对于i := 0到63 do
if(in data[BitIP[I]SHR 3]and(1 SHL(7-(BitIP[I]and $ 07)))& lt;& gt那么0
new data[I SHR 3]:= new data[I SHR 3]or(1 SHL(7-(I和$ 07)));
for i := 0到7 do in data[I]:= new data[I];
结束;
procedure converse permutation(var in data:字节数组);
定义变量
newData:数组[0..7]的字节;
I:整数;
开始
FillChar(newData,8,0);
对于i := 0到63 do
if(in data[BitCP[I]SHR 3]and(1 SHL(7-(BitCP[I]and $ 07)))& lt;& gt那么0
new data[I SHR 3]:= new data[I SHR 3]or(1 SHL(7-(I和$ 07)));
for i := 0到7 do in data[I]:= new data[I];
结束;
过程扩展(inData:字节数组;var outData:字节数组);
定义变量
I:整数;
开始
FillChar(outData,6,0);
对于i := 0到47 do
if(in data[BitExp[I]SHR 3]and(1 SHL(7-(BitExp[I]and $ 07))))& lt;& gt那么0
out data[I SHR 3]:= out data[I SHR 3]or(1 SHL(7-(I和$ 07)));
结束;
过程置换(变量inData:字节数组);
定义变量
newData:数组[0..3]的字节;
I:整数;
开始
FillChar(newData,4,0);
对于i := 0到31 do
if(in data[BitPM[I]SHR 3]and(1 SHL(7-(BitPM[I]and $ 07)))& lt;& gt那么0
new data[I SHR 3]:= new data[I SHR 3]or(1 SHL(7-(I和$ 07)));
for i := 0到3 do in data[I]:= new data[I];
结束;
函数si(s,inByte:Byte):Byte;
定义变量
c:字节;
开始
c:=(in byte and $ 20)or((in byte and $ 1e)SHR 1)or
((inByte和$ 01)SHL 4);
结果:= (sBox[s][c]和$ 0f);
结束;
过程permutationchoose 1(in data:字节数组;
var outData:字节数组);
定义变量
I:整数;
开始
FillChar(outData,7,0);
对于i := 0到55做什么
if(in data[bit PMC 1[I]SHR 3]和(1 shl (7-(BitPMC1[i]和$ 07)))& lt;& gt那么0
out data[I SHR 3]:= out data[I SHR 3]or(1 SHL(7-(I和$ 07)));
结束;
过程permutationChoose2(inData:字节数组;
var outData:字节数组);
定义变量
I:整数;
开始
FillChar(outData,6,0);
对于i := 0到47 do
if(in data[bitpmc 2[I]SHR 3]and(1 SHL(7-(bitpmc 2[I]and $ 07)))& lt;& gt那么0
out data[I SHR 3]:= out data[I SHR 3]or(1 SHL(7-(I和$ 07)));
结束;
procedure cycleMove(var inData:字节数组;bit move:Byte);
定义变量
I:整数;
开始
对于i := 0到bitMove - 1 do
开始
in data[0]:=(in data[0]SHL 1)或(in data[1]SHR 7);
in data[1]:=(in data[1]SHL 1)或(in data[2]SHR 7);
in data[2]:=(in data[2]SHL 1)或(in data[3]SHR 7);
in data[3]:=(in data[3]SHL 1)或((in data[0]and $ 10)SHR 4);
inData[0] := (inData[0]和$ 0f);
结束;
结束;
过程makeKey(inKey:字节数组;var out key:tkey byte的数组);
常数
bitDisplace:数组[0..15]字节=
( 1,1,2,2, 2,2,2,2, 1,2,2,2, 2,2,2,1 );
定义变量
outData56:数组[0..6]的字节;
key28l:数组[0..3]的字节;
key28r: array[0..3]的字节;
key56o: array[0..6]的字节;
I:整数;
开始
permutationChoose1(inKey,outdate 56);
key 28l[0]:= outdate 56[0]SHR 4;
key 28l[1]:=(outdate 56[0]SHL 4)或(outdate 56[1]SHR 4);
key 28l[2]:=(outdate 56[1]SHL 4)或(outdate 56[2]SHR 4);
key 28l[3]:=(outdate 56[2]SHL 4)或(outdate 56[3]SHR 4);
key 28r[0]:= outdate 56[3]和$ 0f
key 28r[1]:= outdate 56[4];
key 28r[2]:= outdate 56[5];
key 28r[3]:= outdate 56[6];
对于i := 0到15 do
开始
cycleMove(key28l,bit displace[I]);
cycleMove(key28r,bit displace[I]);
key56o[0] := (key28l[0] shl 4)或(key 28l[1]SHR 4);
key 56o[1]:=(key 28l[1]SHL 4)或(key 28l[2]SHR 4);
key56o[2] := (key28l[2] shl 4)或(key 28l[3]SHR 4);
key56o[3] := (key28l[3] shl 4)或(key 28r[0]);
key 56o[4]:= key 28r[1];
key 56o[5]:= key 28r[2];
key 56o[6]:= key 28r[3];
permutationChoose2(key56o,out key[I]);
结束;
结束;
过程加密(inData,subKey:字节数组;
var outData:字节数组);
定义变量
outBuf: array[0..5]的字节;
buf: array[0..7]的字节;
I:整数;
开始
expand(inData,out buf);
for i := 0到5 do outBuf[i] := outBuf[i] xor子密钥[I];
buf[0]:= out buf[0]SHR 2;
buf[1]:=((out buf[0]and $ 03)SHL 4)或(out buf[1]SHR 4);
buf[2]:=((out buf[1]and $ 0f)SHL 2)或(out buf[2]SHR 6);
buf[3] := outBuf[2]和$ 3f
buf[4]:= out buf[3]SHR 2;
buf[5] := ((outBuf[3] and $03) shl 4)或(out buf[4]SHR 4);
buf[6] := ((outBuf[4] and $0f) shl 2)或(out buf[5]SHR 6);
buf[7] := outBuf[5]和$ 3f
对于i := 0到7 do buf[i] := si(i,buf[I]);
for i := 0到3 do out ff[I]:=(buf[I * 2]SHL 4)或buf[I * 2+1];
排列;
for i := 0到3 do out data[I]:= out buf[I];
结束;
程序desData(desMode:TDesMode;
inData:字节数组;var outData:字节数组);
//InData和OutData都是8字节,否则会出错。
定义变量
I,j:整数;
temp,buf:数组[0..3]的字节;
开始
for i := 0到7 do out data[I]:= in data[I];
init permutation(out data);
如果desMode = dmEncry,则
开始
对于i := 0到15 do
开始
for j := 0到3 do temp[j]:= out data[j];//temp = Ln
对于j := 0到3 do out data[j]:= out data[j+4];//Ln+1 = Rn
encry(outData,subKey[i],buf);//Rn = = Kn = = & gt;buf
for j := 0到3 do out data[j+4]:= temp[j]xor buf[j];//Rn+1 = Ln^buf
结束;
for j := 0到3 do temp[j]:= out data[j+4];
对于j := 0到3 do out data[j+4]:= out data[j];
对于j := 0到3,do out data[j]:= temp[j];
结束
否则,如果desMode = dmDecry,则
开始
for i := 15 downto 0 do
开始
for j := 0到3 do temp[j]:= out data[j];
对于j := 0到3 do out data[j]:= out data[j+4];
encry(outData,subKey[i],buf);
for j := 0到3 do out data[j+4]:= temp[j]xor buf[j];
结束;
for j := 0到3 do temp[j]:= out data[j+4];
对于j := 0到3 do out data[j+4]:= out data[j];
对于j := 0到3,do out data[j]:= temp[j];
结束;
converse permutation(out data);
结束;
//////////////////////////////////////////////////////////////
函数EncryStr(Str,Key:String):String;
定义变量
StrByte,OutByte,KeyByte: array[0..7]的字节;
StrResult:字符串;
I,J:整数;
开始
if(长度(字符串)>0)和(Ord(Str[长度(Str)]= 0)那么
引发异常。Create('错误:最后一个字符为空字符。);
if长度(Key)& lt;那就8
while Length(Key)& lt;8 do键:= Key+Chr(0);
而长度(Str)mod 8 & lt;& gt0 do Str:= Str+Chr(0);
对于J := 0到7 do Key byte[J]:= Ord(Key[J+1]);
makeKey(keyByte,subKey);
StrResult:=“”;
for I:= 0 to Length(Str)div 8-1 do
开始
对于J := 0到7 do
StrByte[J]:= Ord(Str[I * 8+J+1]);
desData(dmEncry,StrByte,OutByte);
对于J := 0到7 do
StrResult:= StrResult+Chr(OutByte[J]);
结束;
结果:= StrResult
结束;
function DecryStr(Str,Key:String):String;
定义变量
StrByte,OutByte,KeyByte: array[0..7]的字节;
StrResult:字符串;
I,J:整数;
开始
if长度(Key)& lt;那就8
while Length(Key)& lt;8 do键:= Key+Chr(0);
对于J := 0到7 do Key byte[J]:= Ord(Key[J+1]);
makeKey(keyByte,subKey);
StrResult:=“”;
for I:= 0 to Length(Str)div 8-1 do
开始
对于J := 0到7 do StrByte[J]:= Ord(Str[I * 8+J+1]);
desData(dmDecry,StrByte,OutByte);
对于J := 0到7 do
StrResult:= StrResult+Chr(OutByte[J]);
结束;
while (Length(StrResult)>0)和
(Ord(StrResult[长度(StrResult)]= 0)do
Delete(StrResult,Length(StrResult),1);
结果:= StrResult
结束;
///////////////////////////////////////////////////////////
函数EncryStrHex(Str,Key:String):String;
定义变量
StrResult,TempResult,Temp:String;
I,k:整数;
开始
TempResult := EncryStr(Str,Key);
StrResult:=“”;
for I:= 0 to Length(temp result)-1 do
开始
Temp := Format('%x ',[Ord(temp result[I+1])]);
如果Length(Temp) = 1,则Temp:= ' 0 '+Temp;
StrResult:= StrResult+Temp;
结束;
k:= 0;
for I:= 0 to length(StrResult)-1 do
k:= k+ord((StrResult[I+1]);
结果:= StrResult + intToHex(Byte(k),2);
结束;
函数DecryStrHex(StrHex,Key:String):String;
函数HexToInt(Hex: String):整数;
定义变量
I,Res:整数;
ch:Char;
开始
RES:= 0;
对于I := 0到长度(十六进制)- 1 do
开始
ch:= Hex[I+1];
if(ch & gt;= '0 ')和(ch & lt= '9 ')然后
Res:= Res * 16+Ord(ch)-Ord(' 0 ')
else if(ch & gt;= 'A ')和(ch & lt= 'F ')然后
Res := Res * 16 + Ord(中国)- Ord('A') + 10
else if(ch & gt;= 'a ')和(ch & lt= 'f ')然后
Res := Res * 16 + Ord(中国)- Ord('a') + 10
否则引发异常。Create('Error:不是十六进制字符串');
结束;
结果:= Res
结束;
定义变量
Str,Temp:String;
I,k:整数;
开始
str:=“”;
如果长度(StrHex)& lt;那么=2
开始
结果:=“”;
退出;
结束;
k:= 0;
对于i := 0到长度(StrHex) - 3 do
k:= k+ord((StrHex[I+1]);
尝试
如果字节(k)& lt;& gtbyte(str toint(' $ '+rights tr(StrHex,2)))然后
开始
结果:=“”;
退出;
结束;
delete(StrHex,length(StrHex)-1,2);
对于I := 0到长度(StrHex) div 2 - 1 do
开始
Temp := Copy(StrHex,I * 2 + 1,2);
Str:= Str+Chr(HexToInt(Temp));
结束;
结果:= DecryStr(Str,Key);
除...之外
结果:=“”;
结束;
结束;
结束。