DES算法请教,熟悉DES算法的高手请进!不太清楚的也进,帮我UP,UP者有分!

netprobe 2002-09-28 03:52:53
我现在急需要搞清楚DES算法的原理,老板要让我根据一个Java的DES源码改写成Delphi的源码,可我连DES的算法的实现原理都没搞清楚,看了网上的一些资料,大都讲不清楚,我无法理解,比如:一个64位的数据块,一个字符串为一个字节,那8个字符正好是64个字节,那超过8个字符的,怎么办?还有啊?怎么生成16个子密钥,初始I=1?不 太理解,请哪位高手能指教一二啊?发帖子可以,给我发邮件也行,多谢谢了!Email:Netprobe1978@hotmail.com
...全文
65 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
cgh1970 2002-09-28
  • 打赏
  • 举报
回复
up
itaolu 2002-09-28
  • 打赏
  • 举报
回复
超过8个字符的,就只能填够8个字符的倍数了,例如你有14个字符,那么你就需要多填2个空格,以便补够16个。

我国庆期间将要写一篇介绍DES原理和实现的文章,请你到时关注一下!

我的网站:
http://laoluoc.yeah.net
bengbeng24 2002-09-28
  • 打赏
  • 举报
回复
关注
netprobe 2002-09-28
  • 打赏
  • 举报
回复
谢谢大家啊!继续,UP,呵呵,最好有Delphi写的Triple Des的源码跟Demo程序!
我的Email:Netprobe1978@hotmail.com,希望与大家共同学习DES算法,共同进步!
chilavert318 2002-09-28
  • 打赏
  • 举报
回复
我给你发了,希望能帮得上你的忙,我当时也用过,只是是合作方提供Dll库。没有源码。没你这么幸运了。不过还知道大概的思路。
shishi99 2002-09-28
  • 打赏
  • 举报
回复
GZ,解决了没有?
liux_f 2002-09-28
  • 打赏
  • 举报
回复
给我一份
E-mail: liux_f@263.net
谢谢
netprobe 2002-09-28
  • 打赏
  • 举报
回复
谢谢文文,我已经发了,我的信箱Netprobe1978@hotmail.com
netprobe 2002-09-28
  • 打赏
  • 举报
回复
好的!UP 啊!
liux_f 2002-09-28
  • 打赏
  • 举报
回复
up,
关注中。
outer2000 2002-09-28
  • 打赏
  • 举报
回复
outer_star@371.net
thanks!
chilavert318 2002-09-28
  • 打赏
  • 举报
回复
你给我邮箱地址,我发给你。我这么贴不方便。这也是我以前找到的资料。
我的地址是chilavert318@sina.com和我联系
qxj 2002-09-28
  • 打赏
  • 举报
回复
up
chilavert318 2002-09-28
  • 打赏
  • 举报
回复
DES( Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法。虽然56位密钥的DES算法已经风光不在,而且常有用Des加密的明文被破译的报道,但是了解一下昔日美国的标准加密算法总是有益的,而且目前DES算法得到了广泛的应用,在某些场合,她仍然发挥着余热^_^.

1.1 密钥生成

1.1.1 取得密钥

从用户处取得一个64位(本文如未特指,均指二进制位))长的密码key ,

去除64位密码中作为奇偶校验位的第8、16、24、32、40、48、56、64位,剩下的56位作为有效输入密钥.

1.1.2 等分密钥

 

表1. 57 49 41 33 25 17 9
1 58 50 42 34 26 18
10 2 59 51 43 35 27
19 11 3 60 50 44 36


表2.
 

65 55 47 39 31 23 15
7 62 54 46 38 30 22
14 6 61 53 45 37 29
21 13 5 28 20 12 4


把在1.1.1步中生成的56位输入密钥分成均等的A,B两部分,每部分为28位,参照表1和表2把输入密钥的位值填入相应的位置. 按照表1所示A的第一位为输入的64位密钥的第57位,A的第2位为64位密钥的第49位,...,依此类推,A的最后一位最后一位是64位密钥的第36位。


1.1.3 密钥移位

表3.

i 1 2 3 4 5 6 7 8
ǿ 1 1 2 2 2 2 2 2
i 9 10 11 12 13 14 15 16
ǿ 1 2 2 2 2 2 2 1


DES算法的密钥是经过16次迭代得到一组密钥的,把在1.1.2步中生成的A,B视为迭代的起始密钥,表3显示在第i次迭代时密钥循环左移的位数. 比如在第1次迭代时密钥循环左移1位,第3次迭代时密钥循环左移2位.
第9次迭代时密钥循环左移1位,第14次迭代时密钥循环左移2位.

第一次迭代:
A(1) = ǿ(1) A
B(1) = ǿ(1) B
第i次迭代:
A(i) = ǿ(i) A(i-1)
B(i) = ǿ(i) B(i-1)


1.1.4 密钥的选取

表4.
 

14 17 11 24 1 5 3 28
15 6 21 10 23 19 12 4
26 8 16 7 27 20 13 2
41 52 31 37 47 55 30 40
51 45 33 48 44 49 39 56
34 53 46 42 50 36 29 32


在1.1.3步中第i次迭代生成的两个28位长的密钥为

把合并

按照表4所示k的第一位为56位密钥的第14位,k的第2位为56位密钥的第17位,...,依此类推,k的最后一位最后一位是56位密钥的第32位。 生成与进行第i次迭代加密的数据进行按位异或的48位使用密钥:


1.1.5迭代
DES算法密钥生成需要进行16次迭代,在完成16次迭代前,循环执行1.1.3-1.1.4步.
最终形成16套加密密钥:key[0] , key[1] , key[2] ,…. key[14] , key[15] .
续。。。。


t99 2002-09-28
  • 打赏
  • 举报
回复
Up
netprobe 2002-09-28
  • 打赏
  • 举报
回复
或者说每个函数的实现原理跟功能能介绍一下吗??
RobinHZ 2002-09-28
  • 打赏
  • 举报
回复
去数据算法板搜索一下DES算法吧,那边专门讨论算法问题的!
netprobe 2002-09-28
  • 打赏
  • 举报
回复
算法我有,我是说搞清楚算法实现的原理!!
zfmich 2002-09-28
  • 打赏
  • 举报
回复
procedure encry(inData, subKey: array of Byte;
var outData: array of Byte);
var
outBuf: array[0..5] of Byte;
buf: array[0..7] of Byte;
i: Integer;
begin
expand(inData, outBuf);
for i := 0 to 5 do outBuf[i] := outBuf[i] xor subKey[i];
// outBuf xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
buf[0] := outBuf[0] shr 2; //xxxxxx -> 2
buf[1] := ((outBuf[0] and $03) shl 4) or (outBuf[1] shr 4); // 4 <- xx xxxx -> 4
buf[2] := ((outBuf[1] and $0f) shl 2) or (outBuf[2] shr 6); // 2 <- xxxx xx -> 6
buf[3] := outBuf[2] and $3f; // xxxxxx
buf[4] := outBuf[3] shr 2; // xxxxxx
buf[5] := ((outBuf[3] and $03) shl 4) or (outBuf[4] shr 4); // xx xxxx
buf[6] := ((outBuf[4] and $0f) shl 2) or (outBuf[5] shr 6); // xxxx xx
buf[7] := outBuf[5] and $3f; // xxxxxx
for i := 0 to 7 do buf[i] := si(i, buf[i]);
for i := 0 to 3 do outBuf[i] := (buf[i*2] shl 4) or buf[i*2+1];
permutation(outBuf);
for i := 0 to 3 do outData[i] := outBuf[i];
end;

procedure desData(desMode: TDesMode;
inData: array of Byte; var outData: array of Byte);
// inData, outData 都为8Bytes,否则出错
var
i, j: Integer;
temp, buf: array[0..3] of Byte;
begin
for i := 0 to 7 do outData[i] := inData[i];
initPermutation(outData);
if desMode = dmEncry then
begin
for i := 0 to 15 do
begin
for j := 0 to 3 do temp[j] := outData[j]; //temp = Ln
for j := 0 to 3 do outData[j] := outData[j + 4]; //Ln+1 = Rn
encry(outData, subKey[i], buf); //Rn ==Kn==> buf
for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j]; //Rn+1 = Ln^buf
end;

for j := 0 to 3 do temp[j] := outData[j + 4];
for j := 0 to 3 do outData[j + 4] := outData[j];
for j := 0 to 3 do outData[j] := temp[j];
end
else if desMode = dmDecry then
begin
for i := 15 downto 0 do
begin
for j := 0 to 3 do temp[j] := outData[j];
for j := 0 to 3 do outData[j] := outData[j + 4];
encry(outData, subKey[i], buf);
for j := 0 to 3 do outData[j + 4] := temp[j] xor buf[j];
end;
for j := 0 to 3 do temp[j] := outData[j + 4];
for j := 0 to 3 do outData[j + 4] := outData[j];
for j := 0 to 3 do outData[j] := temp[j];
end;
conversePermutation(outData);
end;

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

function EncryStr(Str, Key: String): String;
var
StrByte, OutByte, KeyByte: array[0..7] of Byte;
StrResult: String;
I, J: Integer;
begin
if (Length(Str) > 0) and (Ord(Str[Length(Str)]) = 0) then
raise Exception.Create('Error: the last char is NULL char.');
if Length(Key) < 8 then
while Length(Key) < 8 do Key := Key + Chr(0);
while Length(Str) mod 8 <> 0 do Str := Str + Chr(0);

for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
makeKey(keyByte, subKey);

StrResult := '';

for I := 0 to Length(Str) div 8 - 1 do
begin
for J := 0 to 7 do
StrByte[J] := Ord(Str[I * 8 + J + 1]);
desData(dmEncry, StrByte, OutByte);
for J := 0 to 7 do
StrResult := StrResult + Chr(OutByte[J]);
end;

Result := StrResult;
end;

function DecryStr(Str, Key: String): String;
var
StrByte, OutByte, KeyByte: array[0..7] of Byte;
StrResult: String;
I, J: Integer;
begin
if Length(Key) < 8 then
while Length(Key) < 8 do Key := Key + Chr(0);

for J := 0 to 7 do KeyByte[J] := Ord(Key[J + 1]);
makeKey(keyByte, subKey);

StrResult := '';

for I := 0 to Length(Str) div 8 - 1 do
begin
for J := 0 to 7 do StrByte[J] := Ord(Str[I * 8 + J + 1]);
desData(dmDecry, StrByte, OutByte);
for J := 0 to 7 do
StrResult := StrResult + Chr(OutByte[J]);
end;
while (Length(StrResult) > 0) and
(Ord(StrResult[Length(StrResult)]) = 0) do
Delete(StrResult, Length(StrResult), 1);
Result := StrResult;
end;

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

function EncryStrHex(Str, Key: String): String;
var
StrResult, TempResult, Temp: String;
I: Integer;
begin
TempResult := EncryStr(Str, Key);
StrResult := '';
for I := 0 to Length(TempResult) - 1 do
begin
Temp := Format('%x', [Ord(TempResult[I + 1])]);
if Length(Temp) = 1 then Temp := '0' + Temp;
StrResult := StrResult + Temp;
end;
Result := StrResult;
end;

function DecryStrHex(StrHex, Key: String): String;
function HexToInt(Hex: String): Integer;
var
I, Res: Integer;
ch: Char;
begin
Res := 0;
for I := 0 to Length(Hex) - 1 do
begin
ch := Hex[I + 1];
if (ch >= '0') and (ch <= '9') then
Res := Res * 16 + Ord(ch) - Ord('0')
else if (ch >= 'A') and (ch <= 'F') then
Res := Res * 16 + Ord(ch) - Ord('A') + 10
else if (ch >= 'a') and (ch <= 'f') then
Res := Res * 16 + Ord(ch) - Ord('a') + 10
else raise Exception.Create('Error: not a Hex String');
end;
Result := Res;
end;

var
Str, Temp: String;
I: Integer;
begin
Str := '';
for I := 0 to Length(StrHex) div 2 - 1 do
begin
Temp := Copy(StrHex, I * 2 + 1, 2);
Str := Str + Chr(HexToInt(Temp));
end;
Result := DecryStr(Str, Key);
end;

end.



zfmich 2002-09-28
  • 打赏
  • 举报
回复
procedure initPermutation(var inData: array of Byte);
var
newData: array[0..7] of Byte;
i: Integer;
begin
FillChar(newData, 8, 0);
for i := 0 to 63 do
if (inData[BitIP[i] shr 3] and (1 shl (7- (BitIP[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 7 do inData[i] := newData[i];
end;

procedure conversePermutation(var inData: array of Byte);
var
newData: array[0..7] of Byte;
i: Integer;
begin
FillChar(newData, 8, 0);
for i := 0 to 63 do
if (inData[BitCP[i] shr 3] and (1 shl (7-(BitCP[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 7 do inData[i] := newData[i];
end;

procedure expand(inData: array of Byte; var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 6, 0);
for i := 0 to 47 do
if (inData[BitExp[i] shr 3] and (1 shl (7-(BitExp[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;

procedure permutation(var inData: array of Byte);
var
newData: array[0..3] of Byte;
i: Integer;
begin
FillChar(newData, 4, 0);
for i := 0 to 31 do
if (inData[BitPM[i] shr 3] and (1 shl (7-(BitPM[i] and $07)))) <> 0 then
newData[i shr 3] := newData[i shr 3] or (1 shl (7-(i and $07)));
for i := 0 to 3 do inData[i] := newData[i];
end;

function si(s,inByte: Byte): Byte;
var
c: Byte;
begin
c := (inByte and $20) or ((inByte and $1e) shr 1) or
((inByte and $01) shl 4);
Result := (sBox[s][c] and $0f);
end;

procedure permutationChoose1(inData: array of Byte;
var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 7, 0);
for i := 0 to 55 do
if (inData[BitPMC1[i] shr 3] and (1 shl (7-(BitPMC1[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;

procedure permutationChoose2(inData: array of Byte;
var outData: array of Byte);
var
i: Integer;
begin
FillChar(outData, 6, 0);
for i := 0 to 47 do
if (inData[BitPMC2[i] shr 3] and (1 shl (7-(BitPMC2[i] and $07)))) <> 0 then
outData[i shr 3] := outData[i shr 3] or (1 shl (7-(i and $07)));
end;

procedure cycleMove(var inData: array of Byte; bitMove: Byte);
var
i: Integer;
begin
for i := 0 to bitMove - 1 do
begin
inData[0] := (inData[0] shl 1) or (inData[1] shr 7);
inData[1] := (inData[1] shl 1) or (inData[2] shr 7);
inData[2] := (inData[2] shl 1) or (inData[3] shr 7);
inData[3] := (inData[3] shl 1) or ((inData[0] and $10) shr 4);
inData[0] := (inData[0] and $0f);
end;
end;

procedure makeKey(inKey: array of Byte; var outKey: array of TKeyByte);
const
bitDisplace: array[0..15] of Byte =
( 1,1,2,2, 2,2,2,2, 1,2,2,2, 2,2,2,1 );
var
outData56: array[0..6] of Byte;
key28l: array[0..3] of Byte;
key28r: array[0..3] of Byte;
key56o: array[0..6] of Byte;
i: Integer;
begin
permutationChoose1(inKey, outData56);

key28l[0] := outData56[0] shr 4;
key28l[1] := (outData56[0] shl 4) or (outData56[1] shr 4);
key28l[2] := (outData56[1] shl 4) or (outData56[2] shr 4);
key28l[3] := (outData56[2] shl 4) or (outData56[3] shr 4);
key28r[0] := outData56[3] and $0f;
key28r[1] := outData56[4];
key28r[2] := outData56[5];
key28r[3] := outData56[6];

for i := 0 to 15 do
begin
cycleMove(key28l, bitDisplace[i]);
cycleMove(key28r, bitDisplace[i]);
key56o[0] := (key28l[0] shl 4) or (key28l[1] shr 4);
key56o[1] := (key28l[1] shl 4) or (key28l[2] shr 4);
key56o[2] := (key28l[2] shl 4) or (key28l[3] shr 4);
key56o[3] := (key28l[3] shl 4) or (key28r[0]);
key56o[4] := key28r[1];
key56o[5] := key28r[2];
key56o[6] := key28r[3];
permutationChoose2(key56o, outKey[i]);
end;
end;
加载更多回复(4)

5,931

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧