请问如何将以下这些VC代码变换成DELPHI代码呢?
请不吝赐教!
这写代码是DES加密解密所用的。谢谢!typedef bool (*PSubKey)[16][48];
enum ENCDEC_TYPE
{
ENCRYPT = 0, //加密
DECRYPT //解密
};class CDESEncDec
{
public:
CDESEncDec();
virtual ~CDESEncDec(); bool EncDec(char *Out,char *In,long datalen,const char *Key,int keylen,bool Type = ENCRYPT);private:
void DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type);//标准DES加/解密
void SetSubKey(PSubKey pSubKey, const char Key[8]);// 设置子密钥
private:
bool SubKey[2][16][48];// 16圈子密钥
};
void CDESEncDec::SetKey(const char* Key, int len)
{
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
{
DES(Out, In, &SubKey[0], Type);
}
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(&SubKey[0], &deskey[0]);
Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
}
请不吝赐教!
这写代码是DES加密解密所用的。谢谢!typedef bool (*PSubKey)[16][48];
enum ENCDEC_TYPE
{
ENCRYPT = 0, //加密
DECRYPT //解密
};class CDESEncDec
{
public:
CDESEncDec();
virtual ~CDESEncDec(); bool EncDec(char *Out,char *In,long datalen,const char *Key,int keylen,bool Type = ENCRYPT);private:
void DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type);//标准DES加/解密
void SetSubKey(PSubKey pSubKey, const char Key[8]);// 设置子密钥
private:
bool SubKey[2][16][48];// 16圈子密钥
};
void CDESEncDec::SetKey(const char* Key, int len)
{
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
{
DES(Out, In, &SubKey[0], Type);
}
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(&SubKey[0], &deskey[0]);
Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
}
memset,memcpy在DELPHI用什么呢?
关于参数是指针的,有什么好的办法呢?
我看到DELPHI中自己带的SOURCE中的VLC中的BUTTON.PAS等好多在参数中没有使用指针,为什么呢?
哎,,看来学得是一塌糊涂啊。
Move
因为C中没有变参的概念,所以不得不用指针来实现变参的功能。而Pascal一直都是又变参的概念的,所以不需要使用指针,而且也方便。C++不也有吗?
PSubKey:array[0..16,0..48] of bool;
type ENCDEC_TYPE =(ENCRYPT,DECRYPT); CDESEncDec=class
public:
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function EnDec(Outchar:char;Inchar:char;datalen:long;const Key:char;keylen:integer;EncType:ENCRYPT):bool;
private:
procedure DES(Outchar:array[0..8] of char;Inchar:array[0..8] of char;const pSubKey1:PSubKey;EncType:bool);//标准DES加/解密
procedure SetKey(const Key:char;len:integer);// 设置密钥
procedure SetSubKey(pSubKey1:PSubKey;const Key:array[0..8] or char); // 设置子密钥
procedure F_func(Inchar:array[0..32] of bool;const Ki:array[0..48] of char); // f 函数
procedure S_func(Outchar:array[0..32] of bool;const Inchar:array[0..48] of char); // S 盒代替
procedure Transform(Outchar:bool;Inchar:bool;const Table:char;len:integer); // 变换
procedure Xorchar(InA:bool; const InB:bool;len:integer); // 异或
procedure Rotatel(Inchar:bool;len:integer;loop:integer); // 循环左移
procedure ByteToBit(Outchar:bool;const Inchar:char;bits:integer);// 字节组转换成位组
procedure BitToByte(Outchar:bool;const Inchar:bool;bits:integer);// 位组转换成字节组
private:
SubKey:array[0..2,0..16,0..48] of bool;
Is3DES:bool;
Tmp:array[0..256] of char;
deskey:array[0..16] of char;
end;
我转换的一段定义,好象不行啊
type
PSubKey=array[0..15,0..47] of Boolean;
ENCDEC_TYPE =(ENCRYPT,DECRYPT);
TCharArray8=array[0..7] of char;
TCharArray32=array[0..31] of char;
TCharArray48=array[0..47] of char;
TBoolArray32=array[0..31] of Boolean;
TCDESEncDec=class
private
procedure DES(Outchar,Inchar:TCharArray8;const pSubKey1:PSubKey;EncType:Boolean);//标准DES加/解密
procedure SetKey(const Key:char;len:integer);// 设置密钥
procedure SetSubKey(pSubKey1:PSubKey;const Key:TCharArray8); // 设置子密钥
procedure F_func(Inchar:TBoolArray32;const Ki:TCharArray48); // f 函数
procedure S_func(Outchar:TBoolArray32;const Inchar:TCharArray48); // S 盒代替
procedure Transform(Outchar,Inchar:Boolean;const Table:char;len:integer); // 变换
procedure Xorchar(InA:Boolean; const InB:Boolean;len:integer); // 异或
procedure Rotatel(Inchar:Boolean;len:integer;loop:integer); // 循环左移
procedure ByteToBit(Outchar:Boolean;const Inchar:char;bits:integer);// 字节组转换成位组
procedure BitToByte(Outchar:Boolean;const Inchar:Boolean;bits:integer);// 位组转换成字节组
public
constructor Create();
destructor Destroy; override;
function EnDec(Outchar,Inchar:char;datalen:longint;const Key:char;keylen:integer;EncType:ENCDEC_TYPE):Boolean;
end;
implementation{ TCDESEncDec }procedure TCDESEncDec.BitToByte(Outchar: Boolean; const Inchar: Boolean;
bits: integer);
beginend;procedure TCDESEncDec.ByteToBit(Outchar: Boolean; const Inchar: char;
bits: integer);
beginend;constructor TCDESEncDec.Create;
beginend;procedure TCDESEncDec.DES(Outchar, Inchar: TCharArray8;
const pSubKey1: PSubKey; EncType: Boolean);
beginend;destructor TCDESEncDec.Destroy;
begin inherited;
end;function TCDESEncDec.EnDec(Outchar, Inchar: char; datalen: Integer;
const Key: char; keylen: integer; EncType: ENCDEC_TYPE): Boolean;
beginend;procedure TCDESEncDec.F_func(Inchar: TBoolArray32; const Ki: TCharArray48);
beginend;procedure TCDESEncDec.Rotatel(Inchar: Boolean; len, loop: integer);
beginend;procedure TCDESEncDec.S_func(Outchar: TBoolArray32;
const Inchar: TCharArray48);
beginend;procedure TCDESEncDec.SetKey(const Key: char; len: integer);
beginend;procedure TCDESEncDec.SetSubKey(pSubKey1: PSubKey; const Key: TCharArray8);
beginend;procedure TCDESEncDec.Transform(Outchar, Inchar: Boolean;
const Table: char; len: integer);
beginend;procedure TCDESEncDec.Xorchar(InA: Boolean; const InB: Boolean;
len: integer);
beginend;end.
能把下面这个函数也换一下吗?呵呵。
void CDESEncDec::SetKey(const char* Key, int len)
{
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
{
DES(Out, In, &SubKey[0], Type);
}
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(&SubKey[0], &deskey[0]);
Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
}
len>16?16:len 这句话最后的数值是多少 是16 还是len ?
memset(deskey, 0, 16);//deskey在哪里声明的
memcpy(deskey, Key, len>16?16:len);//deskey是目标吗
if (len>16) memcpy(deskey,Key,16)
else memcpy(deskey,Key,len);主要是象这些我不好把握,思路不清晰:
SetSubKey(&SubKey[0], &deskey[0]);另外memset,memcpy不知道。
private:
bool SubKey[2][16][48];// 16圈子密钥
bool Is3DES;// 3次DES标志
char Tmp[256], deskey[16];for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)怎么理解?
memset(deskey, 0, 16);//deskey在哪里声明的
memcpy(deskey, Key, len>16?16:len);//deskey是目标吗var
i,j:long;
for i:=0,j:=datalen shl 3 to i<j do
begin
i:=i+1;
outchar:=outchar+8;
Inchar:=Inchar+8;
memset(deskey, 0, 16);//
memcpy(deskey, Key, len>16?16:len);//
end;
SetSubKey(&SubKey[0], &deskey[0]);//
for i:=0,j:=datalen shl 3 to i<j do 在Delphi中没有这样的写法,不清楚什么意思
吃饭去了,一会再来
i,j:integer;
Outchar,Inchar:TCharArray8; // zai
EncType:Boolean; //
begin
//for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
// DES(Out, In, &SubKey[0], Type); //memset(deskey, 0, 16);
//memcpy(deskey, Key, len>16?16:len);
//SetSubKey(&SubKey[0], &deskey[0]);
//Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
begin
//Outchar,Inchar,EncType在哪里声明的?
//我不知道,只好在函数内声明了,应该是在外部吧
DES(Outchar, Inchar, SubKey, EncType);
end;
FillChar(Deskey,sizeof(deskey),0); //把Deskey中的每个字符填充0
if len>16 then
Move(key,Deskey,16)
else
Move(key,Deskey,len); //将key中的len个字符拷贝到Deskey
//我认为SetSubKey(&SubKey[0], &deskey[0]); 我对SubKey的类型有些迷惑
SetSubKey(SubKey,Deskey);
//Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
//(SetSubKey(&SubKey[1], &deskey[8]), true) 这句话不明白
if len>8 then else
end;
void BitToByte(char *Out, const bool *In, int bits);
void CDESEncDec::BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, bits>>3);
for(int i=0; i<bits; ++i)
Out[i>>3] |= In[i]<<(i&7);
}
我改为:
procedure BitToByte(Outchar:char;const Inchar:bool;bits:integer);
procedure CDESEncDec.BitToByte(Outchar:char;const Inchar:bool;bits:integer);
var
i:integer;
begin
FillChar(Outchar,sizeof(Outchar),0); //把Deskey中的每个字符填充0
for i:=0 to bits-1 do
begin
Outchar[i shl 3] = (Outchar[i shl 3]) or (Inchar[i] shr (i and 7));//这句有错误
end;
end;
我要怎么样改呢?
我把这个过程的声明
procedure BitToByte(Outchar:char;const Inchar:bool;bits:integer);
改为:
pbool=^boolean;
procedure BitToByte(Outchar:pchar;const Inchar:pbool;bits:integer);把里面的循环改为:
for i:=0 to bits do
begin
j:=i and 7;
k:=Inchar[i] shr j; //错误
Outchar[i shl 3]:=(Outchar[i shl 3] or k);
end;
要怎么改呢?
谢谢 cll007(gazo)
FillChar(Outchar,sizeof(Outchar),#0);//添这个好些,或者#48(#48代表0 )
以下是源程序:
// des.h : header file
///************************************
REVISION LOG ENTRY
Revision By: jar
Revised on 2004-1-14 10:32:48
Comments: ...
************************************/#ifndef _DES5_H
#define _DES5_H
typedef bool (*PSubKey)[16][48];enum ENCDEC_TYPE
{
ENCRYPT = 0, //加密
DECRYPT //解密
};// CDESEncDec class
class CDESEncDec
{
public:
CDESEncDec();
virtual ~CDESEncDec(); bool EncDec(char *Out,char *In,long datalen,const char *Key,int keylen,bool Type = ENCRYPT);private:
void DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type);//标准DES加/解密
void SetKey(const char* Key, int len);// 设置密钥
void SetSubKey(PSubKey pSubKey, const char Key[8]);// 设置子密钥
void F_func(bool In[32], const bool Ki[48]);// f 函数
void S_func(bool Out[32], const bool In[48]);// S 盒代替
void Transform(bool *Out, bool *In, const char *Table, int len);// 变换
void Xor(bool *InA, const bool *InB, int len);// 异或
void RotateL(bool *In, int len, int loop);// 循环左移
void ByteToBit(bool *Out, const char *In, int bits);// 字节组转换成位组
void BitToByte(char *Out, const bool *In, int bits);// 位组转换成字节组
private:
bool SubKey[2][16][48];// 16圈子密钥
bool Is3DES;// 3次DES标志
char Tmp[256], deskey[16];
};#endif //_DES5_H
// des.cpp : implementation file
///************************************
REVISION LOG ENTRY
Revision By: jar
Revised on 2004-1-15 16:51:32
Comments: ...
************************************/#include "StdAfx.h"
#include "des.h"// initial permutation IP
const static char IP_Table[64] = {
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
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
};// final permutation IP^-1
const static char IPR_Table[64] = {
40, 8, 48, 16, 56, 24, 64, 32, 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
};// expansion operation matrix
static const char E_Table[48] = {
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
};// 32-bit permutation function P used on the output of the S-boxes
const static char P_Table[32] = {
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
};// permuted choice table (key)
const static char PC1_Table[56] = {
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, 52, 44, 36,
63, 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
};
// permuted choice key (table)
const static char PC2_Table[48] = {
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
};// number left rotations of pc1
const static char LOOP_Table[16] = {
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};// The (in)famous S-boxes
const static char S_Box[8][4][16] = {
// S1
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,
// S2
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,
// S3
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,
// S4
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,
// S5
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,
// S6
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,
// S7
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,
// S8
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
};
///////////////////////////////////////////////////////////////////////
// ImplementationCDESEncDec::CDESEncDec()
{
}CDESEncDec::~CDESEncDec()
{
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : EncDec
// 功能描述 : 根据参数对指定的字符串进行加密或者解密
// 参数 : char *Out -- 输出缓冲区(Out)的长度 >= ((datalen+7)/8)*8,即比datalen大的且是8的倍数的最小正整数
// 参数 : char *In -- In 可以= Out,此时加/解密后将覆盖输入缓冲区(In)的内容
// 参数 : long datalen
// 参数 : const char *Key -- 当keylen>8时系统自动使用3次DES加/解密,否则使用标准DES加/解密.超过16字节后只取前16字节
// 参数 : int keylen
// 参数 : bool Type = ENCRYPT -- ENCRYPT:加密,DECRYPT:解密
// 返回值 : bool
//
///////////////////////////////////////////////////////////////////////
bool CDESEncDec::EncDec(char *Out, char *In, long datalen, const char *Key, int keylen, bool Type)
{
if( !( Out && In && Key && (datalen=(datalen+7)&0xfffffff8) ) )
return false;
SetKey(Key, keylen);
if( !Is3DES )
{ // 1次DES
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
DES(Out, In, &SubKey[0], Type);
}
else{ // 3次DES 加密:加(key0)-解(key1)-加(key0) 解密::解(key0)-加(key1)-解(key0)
for(long i=0,j=datalen>>3; i<j; ++i,Out+=8,In+=8)
{
DES(Out, In, &SubKey[0], Type);
DES(Out, Out, &SubKey[1], !Type);
DES(Out, Out, &SubKey[0], Type);
}
} return true;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::SetKey
// 功能描述 :
// 参数 : const char* Key
// 参数 : int len
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::SetKey(const char* Key, int len)
{
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(&SubKey[0], &deskey[0]);
Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::DES
// 功能描述 :
// 参数 : char Out[8]
// 参数 : char In[8]
// 参数 : const PSubKey pSubKey
// 参数 : bool Type
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::DES(char Out[8], char In[8], const PSubKey pSubKey, bool Type)
{
static bool M[64], tmp[32], *Li=&M[0], *Ri=&M[32];
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
if( Type == ENCRYPT )
{
for(int i=0; i<16; ++i)
{
memcpy(tmp, Ri, 32);
F_func(Ri, (*pSubKey)[i]);
Xor(Ri, Li, 32);
memcpy(Li, tmp, 32);
}
}
else
{
for(int i=15; i>=0; --i)
{
memcpy(tmp, Li, 32);
F_func(Li, (*pSubKey)[i]);
Xor(Li, Ri, 32);
memcpy(Ri, tmp, 32);
}
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::SetSubKey
// 功能描述 :
// 参数 : PSubKey pSubKey
// 参数 : const char Key[8]
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::SetSubKey(PSubKey pSubKey, const char Key[8])
{
static bool K[64], *KL=&K[0], *KR=&K[28];
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
for(int i=0; i<16; ++i)
{
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Transform((*pSubKey)[i], K, PC2_Table, 48);
}
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::F_func
// 功能描述 :
// 参数 : bool In[32]
// 参数 : const bool Ki[48]
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::F_func(bool In[32], const bool Ki[48])
{
static bool MR[48];
Transform(MR, In, E_Table, 48);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32);
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::S_func
// 功能描述 :
// 参数 : bool Out[32]
// 参数 : const bool In[48]
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::S_func(bool Out[32], const bool In[48])
{
for(char i=0,j,k; i<8; ++i,In+=6,Out+=4)
{
j = (In[0]<<1) + In[5];
k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
}
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::Transform
// 功能描述 :
// 参数 : bool *Out
// 参数 : bool *In
// 参数 : const char *Table
// 参数 : int len
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::Transform(bool *Out, bool *In, const char *Table, int len)
{
for(int i=0; i<len; ++i)
Tmp[i] = In[ Table[i]-1 ];
memcpy(Out, Tmp, len);
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::Xor
// 功能描述 :
// 参数 : bool *InA
// 参数 : const bool *InB
// 参数 : int len
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::Xor(bool *InA, const bool *InB, int len)
{
for(int i=0; i<len; ++i)
InA[i] ^= InB[i];
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::RotateL
// 功能描述 :
// 参数 : bool *In
// 参数 : int len
// 参数 : int loop
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::RotateL(bool *In, int len, int loop)
{
memcpy(Tmp, In, loop);
memcpy(In, In+loop, len-loop);
memcpy(In+len-loop, Tmp, loop);
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::ByteToBit
// 功能描述 :
// 参数 : bool *Out
// 参数 : const char *In
// 参数 : int bits
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::ByteToBit(bool *Out, const char *In, int bits)
{
for(int i=0; i<bits; ++i)
Out[i] = (In[i>>3]>>(i&7)) & 1;
}
///////////////////////////////////////////////////////////////////////
//
// 函数名 : CDESEncDec::BitToByte
// 功能描述 :
// 参数 : char *Out
// 参数 : const bool *In
// 参数 : int bits
// 返回值 : void
//
///////////////////////////////////////////////////////////////////////
void CDESEncDec::BitToByte(char *Out, const bool *In, int bits)
{
memset(Out, 0, bits>>3);
for(int i=0; i<bits; ++i)
Out[i>>3] |= In[i]<<(i&7);
}
比如:
Out[i>>3] |= In[i]<<(i&7);
bits>>3
j = (In[0]<<1) + In[5];
不过我先看看源程序
i>>3就是循环左移三位。
|= 或
Out[i>>3] |= In[i]<<(i&7);
改为:
out[i shl 3]:=out[i shl 3] or In[i] shr (i and 7);
我就不知道这些数组。如果out,in都是INTEGER数组还好点。
由于OUT,IN在DELPHI是关键字,所以我所有的OUT,IN都用OUTCHAR,INCHAR代替。
>>就是Pascal里面的shr
<< 就是shl。i>>3就是i shr 3
“&”就是与运算and
“|”就是或运算or
In[i] shl (i and 7)
因为 “=”是赋值,所以在pascal里面应该是 :=
所以
Out[i>>3] |= In[i]<<(i&7);
应该是:
Out[i>>3] = (Out[i>>3]) | (In[i]<<(i&7)); 改写为:Outchar[i shl 3] := (Outchar[i shl 3]) or (Inchar[i] shr (i and 7));嗬嗬,没错吧。帮个小忙。
const
// initial permutation IP
IP_Table:array[0..63] of byte =(
58,50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
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, 70);
// final permutation IP^-1
IPR_Table:array[0..63] of byte =(
40, 8, 48, 16, 56, 24, 64, 32, 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);
// expansion operation matrix
E_Table:array[0..47] of byte =(
32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1);
// 32-bit permutation function P used on the output of the S-boxes
P_Table:array[0..31] of byte =(
16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25); // permuted choice table (key)
PC1_Table:array[0..55] of byte =(
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, 52, 44, 36,
63, 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);
// permuted choice key (table)
PC2_Table:array[0..47] of byte =(
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);// number left rotations of pc1
LOOP_Table:array[0..15] of byte =(
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1); // The (in)famous S-boxes
S_Box:array[0..7,0..3,0..15]of byte =(
// S1
((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)),
// S2
((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)),
// S3
((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)),
// S4
((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)),
// S5
((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)),
// S6
((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)),
// S7
((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)),
// S8
((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)));type
TByte8=array[0..7] of byte;
TByte16=array[0..15] of byte;
TByte32=array[0..31] of byte;
TByte48=array[0..47] of byte;
TByte256=array[0..255] of byte;
TByte=array of byte; //TBool32=array[0..31] of Boolean;
//TBool48=array[0..47] of Boolean;
TSubKey=array[0..15,0..47] of byte; //PSubKey=^TSubKey;
TEncdecTYPE =(ENCRYPT,DECRYPT); TDes=class(TObject)
private
FSubKey:array[0..1,0..15,0..47] of Boolean;// 16圈子密钥
FIs3DES:Boolean;// 3次DES标志
FTmp:TByte256;
FDeskey:TByte16;
procedure DES(OuTByte,InChar:TByte8; const aSubKey: TSubKey;aType:TEncdecTYPE);//标准DES加/解密
procedure SetKey(const Key:PByte;len:integer);// 设置密钥
procedure SetSubKey(aSubKey: TSubKey; const Key:TByte8);// 设置子密钥
procedure F_func(InBool:TByte32; const KiBool:TByte48);// f 函数
procedure S_func(OutBool:TByte32; const InBool:TByte48);// S 盒代替
procedure Transform(OutBool,InBool:TByte; const Table:TByte;len:integer);// 变换
procedure SetXor(InA:TByte;const InB:TByte;len:integer);// 异或
procedure RotateL(InBool:TByte;len,loop:integer);// 循环左移
procedure ByteToBit(OutBool:TByte;const InBool:TByte;bits:Integer);// 字节组转换成位组
procedure BitToByte(OutBool:TByte;const InBool:TByte;bits:Integer);// 位组转换成字节组
protected public
constructor Create;
destructor Destroy; override;
function EncDec(OuTByte,InChar:PChar;DataLen:integer;const Key:PChar;KeyLen:integer;const aType :TEncdecTYPE= ENCRYPT):Boolean;
published end;implementationuses MaskUtils;{ TDes }
procedure TDes.BitToByte(OutBool: TByte; const InBool: TByte;
bits: Integer);
var
i:integer;
begin
{ memset(Out, 0, bits>>3);
for(int i=0; i<bits; ++i)
Out[i>>3] |= In[i]<<(i&7); }
FillChar(OutBool,bits shl 3,#0);
for i :=0 to bits-1 do
OutBool[i shl 3] := (OutBool[i shl 3]) or (InBool[i] shr (i and 7));
end;procedure TDes.ByteToBit(OutBool: TByte; const InBool: TByte;
bits: Integer);
var
i:integer;
begin
{ for(int i=0; i<bits; ++i)
Out[i] = (In[i>>3]>>(i&7)) & 1; }
for i :=0 to bits -1 do
OutBool[i]:=(InBool[i shl 3] shl (i and 7)) and 1;
end;constructor TDes.Create;
beginend;procedure TDes.DES(OuTByte, InChar: TByte8; const aSubKey: TSubKey;
aType: TEncdecTYPE);
beginend;destructor TDes.Destroy;
begin inherited;
end;function TDes.EncDec(OutByte, InChar: PChar; DataLen: integer;
const Key: PChar; KeyLen: integer; const aType: TEncdecTYPE): Boolean;
begin
Result:=false;
end;
procedure TDes.F_func(InBool: TByte32; const KiBool: TByte48);
var
aOut,aIn,E_Tab,Ki,P_Tab:tbyte;
aOutP:TByte48;
i:integer;
begin
{ static bool MR[48];
Transform(MR, In, E_Table, 48);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32); }
SetLength(aout,48);
SetLength(aIn,32);
SetLength(E_Tab,48);
SetLength(Ki,48);
SetLength(P_Tab,32);
for i:=low(aIn) to high(aIn) do
aIn[i]:=InBool[i];
for i:=low(E_Tab) to high(E_Tab) do
E_Tab[i]:=E_Table[i];
for i:=low(Ki) to high(Ki) do
Ki[i]:=Kibool[i];
for i:=low(P_Tab) to high(P_Tab) do
P_Tab[i]:=P_Table[i];
Transform(aout,aIn,E_Tab,48);
SetXor(aout,ki,48);
for i:=low(aOutP) to high(aOutP) do
aOutP[i]:=aout[i];
S_func(InBool, aOutP);
Transform(aIn, aIn, P_Tab, 32);
end;procedure TDes.RotateL(InBool: TByte; len, loop: integer);
begin
{ memcpy(Tmp, In, loop);
memcpy(In, In+loop, len-loop);
memcpy(In+len-loop, Tmp, loop); }
Move(InBool,FTmp,loop);
Move(InBool[loop],FTmp,len-loop);
Move(FTmp,InBool[len-loop],loop);
end;procedure TDes.SetKey(const Key: PByte; len: integer);
begin
{
memset(deskey, 0, 16);
memcpy(deskey, Key, len>16?16:len);
SetSubKey(&SubKey[0], &deskey[0]);
Is3DES = len>8 ? (SetSubKey(&SubKey[1], &deskey[8]), true) : false;
}
FillChar(FDeskey,sizeof(FDeskey),#0);
if len>16 then
Move(key,FDeskey,16)
else
Move(key,FDeskey,Len);
//下面的不会写
FIs3DES:=(len >8);
end;procedure TDes.SetSubKey(aSubKey: TSubKey; const Key: TByte8);
begin
{ static bool K[64], *KL=&K[0], *KR=&K[28];
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
for(int i=0; i<16; ++i)
//
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Transform((*pSubKey)[i], K, PC2_Table, 48);
//
}end;procedure TDes.SetXor(InA: TByte; const InB: TByte; len: integer);
var
i:integer;
begin
for i :=0 to len-1 do
InA[i]:=InA[i] xor InB[i];
end;procedure TDes.S_func(OutBool: TByte32; const InBool: TByte48);
var
i,j,k:integer;
begin
{ for(char i=0,j,k; i<8; ++i,In+=6,Out+=4)
//
j = (In[0]<<1) + In[5];
k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
// }
i:=0;
j:=0;
k:=0;
//写不下去了In+=6,Out+=4,不懂
for i :=0 to 7 do
begin
j:=(InBool[0] shr 1)+InBool[5];
k:=(InBool[1] shr 3)+(InBool[2] shr 2)+(InBool[3] shr 1)+InBool[4];
//ByteToBit(OutBool,S_Box[i][j][k],4);
//inc() ;
//inc();
end;end;procedure TDes.Transform(OutBool, InBool: TByte; const Table: TByte;
len: integer);
var
i:integer;
begin
{ for(int i=0; i<len; ++i)
Tmp[i] = In[ Table[i]-1 ];
memcpy(Out, Tmp, len); }
for i :=0 to len-1 do
FTmp[i]:=InBool[Table[i]-1];
Move(FTmp,outBool,len);
end;end.
-------
这个我知道
C++里面的指针可以通过指针名字直接移位,Delphi里面的不知道怎样移的
const
iLow: Integer = 0;
iHigh: Integer = 7;
var
i, j, k: integer;
begin
{ for(char i=0,j,k; i<8; ++i,In+=6,Out+=4)
//
j = (In[0]<<1) + In[5];
k = (In[1]<<3) + (In[2]<<2) + (In[3]<<1) + In[4];
ByteToBit(Out, &S_Box[i][j][k], 4);
// }
//写不下去了In+=6,Out+=4,不懂
for i := iLow to iHigh do
begin
j := (InBool[0] shr 1) + InBool[5];
k := (InBool[1] shr 3) + (InBool[2] shr 2) + (InBool[3] shr 1) + InBool[4];
ByteToBit(OutBool, @S_Box[i][j][k], 4);
Inc(InBool, 6) ;
Inc(OutBool, 4);
end;
end;
if P <> nil then
while P^ <> #0 do
begin
Start := P;
while not (P^ in [#0, #10, #13]) do Inc(P);
SetString(S, Start, P - Start);
Add(S);
if P^ = #13 then Inc(P);
if P^ = #10 then Inc(P);
end;意思就是不出现换行符的时候,就一直用指针增位访问这个串。
所以说这里有疑惑
outc:TByte;
m:TByte;
i:integer;
const IP_Table:array[0..63] of byte =(
58,50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
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, 70);
begin
for i:=0 to 63 do
begin
// m[i]:=IP_Table[i];
end;
BitToByte(outc,@IP_Table,64);
end;
我专门测试这个过程,不行,我现在在看为什么不行。
这样试试看,
var
PIn: PChar;
begin
PIn := Pointer(InBool);
Inc(PIn, 6);
...
end;这样呢?
我知道了,指针如你所说可以那样,
我用的是静态数组,还是有区别
其实这个函数
for(char i=0,j,k; i<8; ++i,In+=6,Out+=4)
{
j = (In[0]<<1) + In[5];..
我不懂下面的,当in变化的时候In[0]是否变化,我认为它应该变化了,不然in+也没用了
你的方法可以通过
没有把OutBool这个TByte这个数组初始化。里面是空值,所以运行的时候出错。
我们应该怎样初始化不知道数组大小的数组呢?
没看清楚
我这样用的
var
pInBool,POutBool:PByte;
POutBool:=@OutBool;
比如
ByteToBit(Out, &S_Box[i][j][k], 4);//&S_Box[i][j][k],
void CDESEncDec::SetSubKey(PSubKey pSubKey, const char Key[8])
{
static bool K[64], *KL=&K[0], *KR=&K[28];//智力
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
for(int i=0; i<16; ++i)
{
RotateL(KL, 28, LOOP_Table[i]);
RotateL(KR, 28, LOOP_Table[i]);
Transform((*pSubKey)[i], K, PC2_Table, 48);
}
}
我记得以前在什么地方看过,C++里面的bool有些特殊
还有就是C++ 的char类型,我用byte来代替的,我知道Delphi中的char和byte都能代替C++的char
不过最大的问题是不懂DES算法,所以我也只从表面翻译
改为这个:
ByteToBit(Out, @S_Box[i][j][k], 4);static bool K[64];
static bool *KL = &K[0];//这个弄到一起了,得改回三个。
static bool *KR = &K[28];var
K: array[0..63] of bool;
KL: ^bool;
KR: ^bool;
begin
KL := @K[0];
KR := @K[28];也就是说用两个指针访问K的左右两个地方,是不是要排序??可以很清晰地看出Pascal的严谨和C++的灵活多变。
DES,MD5,GHOST,TEAN,我说三天时间搞定,看来还差得远啊!
以前看过一点,不懂
现在想看,却没有书了,哈
我还是太水了,最大的困难还是因为C++一点都不懂
delphi还在学基础
BYTE是 0..255。
PASCAL真的是很严谨。
我知道你说的几种布尔型,它们占用的空间不同;
可是我不明白的是源程序中为什么用bool类型
DES,MD5,GHOST,TEAN 代码可否给我一份儿,我研究一下
哈哈
现在感觉好象东一下,西一下。
memcpy(deskey,Key,16)
else
memcpy(deskey,Key,len);改成这样:
if len>16 then
Move(key,FDeskey,16)
else
Move(key,FDeskey,Len);很简单嘛。不就是判断Len的大小嘛。这样不就行了……if len > 16 then len := 16;
Move(key, FDesKey, len);
用数组直接传递给指针,但是这个不符合Pascal地用法,