des算法,c转为delphi ( 积分: 200 )

  • 主题发起人 主题发起人 blues hu
  • 开始时间 开始时间
B

blues hu

Unregistered / Unconfirmed
GUEST, unregistred user!
#define AHPOST /* 定义模块名, 按文件名改为全大写*/

/*
* 以下定义函数需要的系统头文件
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

/*
* 以下定义本模块中的静态函数与静态变量
*/

static const int pc_1_c[28]={
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};

static const int pc_1_d[28]={
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};

static const int pc_2[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};

static const int ls_count[16]={
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};

static const int ip_tab[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};

static const int _ip_tab[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};

static const int e_r[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};

static const int P1[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};

static const int SSS[16][4][16]={
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,2,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,12,

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,0,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};

static char K[17][49];

static void str8to64(in,out)
char *in;
char *out;
{
register int i,j;
unsigned char bit;
for(i=0;i<8;i++)
{
bit=0x80;
for (j=0;j<8;j++)
{
if(in &amp;bit) *out++=1;
else *out++=0;
bit>>=1;
}
}
}

static void str64to8(in,out)
char *in;
char *out;
{
unsigned char bit;
register int i,j;

for (i=0;i<8;i++)
{
bit=0x80;
out=0;
for (j=0;j<8;j++)
{
if (*in++)
out+= bit;
bit>>=1;
}
}
}


static void LS(bits,buffer,count)
char *bits;
int count;
char *buffer;
{
int i;

for (i=0;i<28;i++)
{
buffer=bits[(i+count)%28];
}
}

static void son(cc,dd,kk)
char *cc,*dd,*kk;
{
int i;
char buffer[56];

for (i=0;i<28;i++)
buffer=*cc++;

for (i=28;i<56;i++)
buffer=*dd++;

for (i=0;i<48;i++)
*kk++=buffer[pc_2-1];
}

static void des_setkey(bits)
char bits[64];
{
int i,j;
char C[17][29],D[17][29];

for (i=0;i<28;i++)
C[0]=bits[pc_1_c-1];
for (i=0;i<28;i++)
D[0]=bits[pc_1_d-1];
for (j=0;j<16;j++)
{
LS(C[j],C[j+1],ls_count[j]);
LS(D[j],D[j+1],ls_count[j]);
son(C[j+1],D[j+1],K[j+1]);
}
}


static void ip(text,ll,rr)
char *text;
char *ll,*rr;
{
int i;
for (i=0;i<32;i++)
ll=text[ip_tab-1];

for (i=0;i<32;i++)
rr=text[ip_tab[i+32]-1];
}

static void _ip(text,ll,rr)
char *text;
char *ll,*rr;
{
int i;
char tmp[64];
for (i=0;i<32;i++)
tmp=ll;
for (i=32;i<64;i++)
tmp=rr[i-32];
for (i=0;i<64;i++)
text=tmp[_ip_tab-1];
}

static void s_box(aa,bb)
char *aa,*bb;
{
int i,j,k,m;
int y,z;
char ss[8];
m=0;
for (i=0;i<8;i++)
{
j=6*i;
y=aa[j]*2+aa[j+5];
z=aa[j+1]*8+aa[j+2]*4+aa[j+3]*2+aa[j+4];
ss=SSS[y][z];
y=0x08;
for (k=0;k<4;k++)
{
bb[m++]=(ss/y)&amp;1;
y/=2;
}

}
}

static void F(n,ll,rr,LL,RR)
char *ll,*rr,*LL,*RR;
int n;
{
int i;
char buffer[64],tmp[64];
for (i=0;i<48;i++)
buffer=rr[e_r-1];
for (i=0;i<48;i++)
buffer=(buffer+K[n])&amp;1;

s_box(buffer,tmp);

for (i=0;i<32;i++)
buffer=tmp[P1-1];

for (i=0;i<32;i++)
RR=(buffer+ll)&amp;1;

for (i=0;i<32;i++)
LL=rr;


}


static void encrypt0(text,mtext)
char *text,*mtext;
{
char ll[64],rr[64],LL[64],RR[64];
int i,j;
ip(text,ll,rr);

for (i=1;i<17;i++)
{
F(i,ll,rr,LL,RR);
for (j=0;j<32;j++)
{
ll[j]=LL[j];
rr[j]=RR[j];
}
}

_ip(mtext,rr,ll);

}



static void discrypt0(mtext,text)
char *text,*mtext;
{
char ll[64],rr[64],LL[64],RR[64];
int i,j;
ip(mtext,ll,rr);

for (i=16;i>0;i--)
{
F(i,ll,rr,LL,RR);
for (j=0;j<32;j++)
{
ll[j]=LL[j];
rr[j]=RR[j];
}
}

_ip(text,rr,ll);

}


static void DES(key,text,mtext)
char *text,*mtext,*key;
{
des_setkey(key);
encrypt0(text,mtext);
}
static void _DES(key,mtext,text)
char *text,*mtext,*key;
{
des_setkey(key);
discrypt0(mtext,text);
}

/************************************************************************
Porcedure Name: encrypt_8to8

Summary: encrypt a 8 byte string using a 16 byte key(the value must from
1 to F) which in bcd fromat taking 64 byte space.

Interface:
Input paramter - char key_bit[65]

char str_in[9];
Output paramter - char str_out[9]

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

static void encrypt_8to8(key_bit,str_in,str_out)
char *key_bit,*str_in,*str_out;
{
char str_bit_in[65];
char str_bit_out[65];

str8to64( str_in, str_bit_in );

DES(key_bit,str_bit_in,str_bit_out);

str64to8( str_bit_out, str_out );

}

/************************************************************************
Porcedure Name: dencrypt_8to8

Summary: dencrypt a 8 byte string using a 16 byte key(the value must from
1 to F) which in bcd fromat taking 64 byte space.

Interface:
Input paramter - char key_bit[65]

char str_in[9];
Output paramter - char str_out[9]

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

static void dencrypt_8to8(key_bit,str_in,str_out)
char *key_bit,*str_in,*str_out;
{
char str_bit_in[65];
char str_bit_out[65];

str8to64( str_in, str_bit_in );

_DES(key_bit,str_bit_in,str_bit_out);

str64to8( str_bit_out, str_out);

}

int EncryptPkg(key,in,out,size)
char * key;
char in[];
char out[];
int size;
{
int i;
char key_bit[65];

memset(key_bit,0,65);

str8to64(key,key_bit);

if ( ( size % 8 ) != 0 )
return -1


for(i=0;i<(size/8);i++)
{
encrypt_8to8(key_bit,&amp;in[i*8],&amp;out[i*8]);
}

return 0;
}

int DencryptPkg(char *key,char *in,char *out,int size)
{
int i;
char key_bit[65];

memset(key_bit,0,65);

str8to64(key,key_bit);


if ( ( size % 8 ) != 0 )
return -1


for(i=0;i<(size/8);i++)
{
dencrypt_8to8(key_bit,&amp;in[i*8],&amp;out[i*8]);
}

return 0;
}
 
太长了吧,自己转就好了

如何将C++程序转为Pascal语法
原文出处: by Paul Fulford of Komodo Software
翻译:http://delphi.ktop.com.tw

将C++ 转为 Pascal/Delphi
==============================
目录
概论
数据型态
关键词
叙述结尾
变量宣告
字符串
数组
数值的指定与比较
常数宣告
函数与程序
with ... do叙述
批注
流程控制
对象导向结构
Containers
例外处理
资料流(Streaming)
项目档的设计
如何转换
结论
=================================
概论:
这篇文章的目的是让您了解C++与Object Pascal的不同
也让您能够有能力将C++转为Object Pascal(以下称为OP)
=================================
数据型态
这一章可学到如何将 C++ 的数据型态 转成 OP 的数据型态.
变量宣告时可参考下列对照表
C++ OP 大小 (bytes) 值范围
==================================================================
char ---- 1 -128 to 127
---- char 1 1 ASCII character
int integer 2 -32,768 to 32,767
short ---- 2 -32,768 to 32,767
---- shortint 1 -128 to 127
long longint 4 -2,147,483,647 to 2,147,483,647
unsigned char byte 1 0 to 255
unsigned int word 2 0 to 65,535
unsigned short word 2 0 to 65,535
unsigned long ---- 4 0 to 4,294,967,295

float single 4 3.4E-38 TO 3.4E+38
double double 8 1.7E-308 TO 1.7E+308
long double extended 10 3.4E-4932 TO 3.4E+4932
---- comp 8 1.7E-308 TO 1.7E+308
---- real (for backwards compatibility only -- use double)

void pointer 8 n/a -- an untyped pointer
---- boolean 1 True or False
(C++ may soon have a boolean type)
String ---- a C++ standard object
---- string an array of up to 255 ASCII characters
---- PChar pointer to a null-terminated string
=================================================================

=================================
关键词
C++ 有 59 个关键词, OP 有 60 个关键词. This does not include the many vendor
C++ 大小写有分 OP 大小写不分

C++的关键词
asm auto break case catch cdecl char class const const_cast continue
default delete do double dynamic_cast else enum extern far float for
friend goto huge if inline interrupt int near new operator pascal private
protected public register reinterpret_cast return short signed sizeof
static static_cast struct switch template this throw try typedef typeid
union unsigned virtual void volatile wchar_t while
OP的关键词
and as asm array begin case class const constructor destructor div do
downto else end except exports file finally for function goto if implementation
in inherited inline initialization interface is label library mod nil not
object of or packed procedure program property raise record repeat set shl
shr string then to try type unit until uses var while with xor
=================================
叙述结尾
C++叙述结尾
大部份的叙述都用
号结尾
有一些例外如:
#include
#define MAXNAMELENGTH 35

OP叙述结尾
所有的叙述都用
号结尾
=================================
变量宣告
C++变量宣告
变量名称只有前32个字有效
变量名称宣告可在程序的任何地方宣告(当然,宣告后才可用)
// ... 如在程序中宣告
{
char i;
for (i=0;i<10;i++)
{
char sName[10]
// 在区块中也可宣告变量
int iCount, iLoop, iValToReach;
double dMaxLoan, dTotal;
float fMaxRate = 123.875;
}
}
OP变量宣告
变量名称只有前63个字有效
变量名称要宣告在var之后,且要在function或procedure开始时宣告
不可在区块内宣告变量
如:
function PayBack_d(var dAmount: double): double;
var
iCount, iLoop, iValToReach: integer;
dMaxLoan, dTotal, dMaxRate: double;
begin
dMaxRate := 123.875;
{...}

=================================
字符串
C++字符串
在C++现在也有了字符串对象,但并不和以&quot;/0&quot;结束的char相容.
大部份的字符串就是char数组,可以无限长度.
下面是一些char数组宣告的例子:
char sName[26]
// 25 个chars长
char psDemo[] = &quot;Hello, there!&quot;

char * psDemo = new char[26];
对字符串变量最常做的事就是strcpy与strcat
您要确定strcat或strcpy后的长度够不够存放.
以下是strcpy/strcat的例子.
class Ticker
{
...
public:
char sBuf[10],
sLatestTrade[TRADELENGTHBUF],
saTradeRecords[50] [TRADELENGTHBUF];
...
void OptnFormat2_v(unsigned long &amp
ulQuantity,
CompanyC * poC,
int &amp
iSeries);
...
};
...
void TickerC::OptnFormat2_v(unsigned long &amp
ulQuantity,
CompanyC * poC,
int &amp
iSeries)
{
ultoa(ulQuantity, sBuf, 10);
strcpy(sLatestTrade, sBuf);
AddMosToString_v(sLatestTrade,
poC->oSOS.oSeries[iSeries].oExpDate.enMonth);
itoa(poC->oSOS.oSeries[iSeries].oExpDate.iDay, sBuf, 10);
strcat(sLatestTrade, sBuf);
strcat(sLatestTrade, poC->oS.sTicker);
double dStrike = poC->oSOS.oSeries[iSeries].dStrike;
gcvt(dStrike, 3, sBuf);
strcat(sLatestTrade, sBuf);
if(poC->oSOS.oSeries[iSeries].enCallPut == Call)
strcat(sLatestTrade, &quot;Calls&quot;);
else strcat(sLatestTrade, &quot;Puts&quot;);
}

OP字符串
Delphi 1.0字符串最多255bytes,Delphi 2.0以后无此限.
在OP内的String所占的记忆空间会比您宣告的大小大.
因为字符串为数组,在数组[0]放长度.
例:
var
sMyName: string[25]
{最大25字符}
sYourName: string;
begin
sMyName := 'Paul Fulford'
{单引号表字符串}
在OP中,您可用:=和+处理字符串运算,例:
TickerC = class
...
public
sLatestTrade: string[TRADELENGTHBUF];
saTradeRecords: TStringList;
...
procedure TickerC.OptnFormat2(var lQuantity: longint;
poC: CompanyC;
var iSeries: integer);
...
end;
...
procedure TickerC.OptnFormat2(var lQuantity: longint;
poC: CompanyC;
var iSeries: integer);
begin
sLatestTrade := IntToStr(lQuantity);
AddMosToString(sLatestTrade,
poC.oSOS.oSeries[iSeries].oExpDate.enMonth);
sLatestTrade := sLatestTrade +
IntToStr(poC.oSOS.oSeries[iSeries].oExpDate.iDay) +
poC.oS.sTicker +
FloatToStr(poC.oSOS.oSeries[iSeries].dStrike);
if poC.oSOS.oSeries[iSeries].enCallPut = Call then
sLatestTrade := sLatestTrade + 'Calls'
else sLatestTrade := sLatestTrade + 'Puts';
end
{OptnFormat2}

您会发现OP处理字符串容易得多.
C++中字符串以&quot;/0&quot;结尾,OP中数组[0]放字符串长度.
=================================
数组
数组是数据的一序列排列.
C++与OP的数组宣告很不同.
但取用方式很像.

C++数组
数组从0开始,第一个元素为 [0], 第二个为[1]...类推,初学者要小心
// 宣告 数组...
DateC aoCANHolidays[SIZE_HOLIDAYARRAY];
double dAverageLast31Days[31];
// 取用数组
for(int i = 30, j = 29
i > 0
i--,j--)
dAverageLast31Days = dAverageLast31Days[j];

OP数组
数组通常从[1]开始,但也不全然.
在Delphi中您也可以发现某些对象也是从[0]开始的.
像TList就是从TList[0]开始,我们要小心看对象的说明文件.
例子:
var
aoCANHolidays: array[1..SIZE_HOLIDAYARRAY] of DateC;
dAverageLast31Days: array[1..31] of double;
i,j: integer;
begin
j := 30;
for i := 31 downto 2 do
begin
dAverageLast31Days = dAverageLast31Days[j];
Dec(j)
{ or j := j-1
}
end;


两个语言都可用多维数组,见下例:

C++
double dMatrix[50] [20];

OP
var
dMatrix: array[1..50, 1..20] of double;


=================================
数值的指定与比较
C++数值的指定与比较
比较相等时用 == 号
给定值时用 = 号
如:
if (dMaxRate == 15.75)
{ ...
dMaxRate = 12.5;
...
}

OP数值的指定与比较
比较相等时用 = 号
给定值时用 := 号
如:
if dMaxRate = 15.75 then
begin
...
dMaxRate := 12.5;
...
end;
有一个例外就是OP在Const的值的给定是用=号,如:
const max=100;
=================================
常数宣告
常数是无法改变值的,所以一定要在宣告时给值.
C++常数宣告
const int iMax = 2000;
const double dMaxValue = 1234.75;
也可用宏定义常数
#define nMAXAMOUNT 1000

OP常数宣告
function PayBack_d(var dAmount: double): double;
const
iMAX = 2000
{notice value assigned with single equal sign this time}
dMAXVALUE = 1234.75;
var
iCount, iLoop, iValToReach: integer;
dMaxLoan, dTotal, dMaxRate: double;
begin
dMaxRate := dMAXVALUE;
{...}
=================================
函数与程序
在C++中所有的Function一定会传回值.
在OP中function要传回值,procedure不用
在C++中所有的function一定要()号,就算无参数
在OP中function无参数不用()号
C++函数与程序
例:
double IntSinceLastAddDate_d(double &amp;dAvailCash)
// prototype
void SetNewIntRate()
// no parameters or return value
...
double LoanC::IntSinceLastAddDate_d(double &amp;dAvailCash)
{
double dSomething;
...
return dSomething;
}

void LoanC::SetNewIntRate()
{ ... }


OP函数与程序
例:
function IntSinceLastAddDate_d(var dAvailCash: double): double;
procedure SetNewIntRate
{无参数也无传回值}
...
function LoanC.IntSinceLastAddDate_d(var dAvailCash: double): double;
var
dSomething: double;
begin
...
result := dSomething

{the global variable &quot;result&quot
is assigned the return value!}
end;

procedure LoanC.SetNewIntRate;
begin
...
end;

两个语言可以在参数传递时采用 pass by valye 或 pass by reference:

C++ pass by value ... double IntSinceLastAddDate_d(double dAvailCash);
OP pass by value ... function IntSinceLastAddDate_d(dAvailCash: double): double;
C++ pass by reference ... double IntSinceLastAddDate_d(double &amp;dAvailCash);
OP pass by reference ... function IntSinceLastAddDate_d(var dAvailCash: double): double;
C++ pass constant ... double IntSinceLastAddDate_d(const double dAvailCash);
OP pass constant ... function IntSinceLastAddDate_d(const dAvailCash: double): double;

=================================
with ... do叙述
C++无 with .. DO叙述
在C++中当您要取用资料时:
poC.oStock.aoTradesLast130Days[0].lVol = 0;
poC.oStock.aoTradesLast130Days[0].dHigh = 0;
poC.oStock.aoTradesLast130Days[0].dLow = 0;
poC.oStock.aoTradesLast130Days[0].dClose = 0;
但在OP中可以简化成:
with poC.oStock.aoTradesLast130Days[0] do
begin
lVol := 0;
dHigh := 0;
dLow := 0;
dClose := 0;
end;

=================================
批注
C++
有两种
// 双斜线后为批注
/* 批注 */

OP
有三种
// 双斜线后为批注
{ 批注 }
(* 批注 *)
=================================
流程控制
有五种流程控制指令,C++与OP都有,用法满像的.
~~~~~~~~~~~~~~~~~~~~~~~~~
1) if ... else 指令

C++

if(<逻辑表达式>) // 一定要有()号
{ ...
}
else if(<逻辑表达式>)
{...
}
else
{...
}

OP

if <逻辑表达式> then // 不一定要有()号,因有then
begin
...
end
else if <逻辑表达式> then
begin
....
end
else
begin
...
end
{最后一个end才要加;分号}

~~~~~~~~~~~~~~~~~~~~~~~~~
2) switch/case ... 指令
C++

switch()
{
case iX: ... break;
case iY: ... break;
default: ...
}

OP

case of
{no &quot;begin&quot
here}
iX:
begin
...
end
{每个end都要;分号}
iY:
begin
...
end;
else {此处不用;号}
begin
...
end;
end
{这个end要;分号}


~~~~~~~~~~~~~~~~~~~~~~~~~
3) for ... loop 指令
C++

for(iCount = 0
iCount <= 10
iCount++)
{
// iCount++, 每次循环值加一
...
break
// 中断循环
continue
// 跳到下一个循环
...
}


OP

for iCount := 1 to 10 do
begin
... {iCount每次自动加 1}
break
{ 中断循环 }
continue
{ 跳至下一个循环 }
...
end;

~~~~~~~~~~~~~~~~~~~~~~~~~
4) while ... loop 指令
C++

while(<逻辑表达式>)
{
...
break
// 中断循环
continue
// 跳到下一个循环
...
}


OP

while <逻辑表达式> do
begin
...
break
{ 中断循环 }
continue
{ 跳至下一个循环 }
...
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
5) do/repeat ... 指令
C++

do
{
// 逻辑表达式 False循环终止
...
break
// 中断循环
continue
// 跳到下一个循环
...
}while(<逻辑表达式>);


OP

repeat
// 逻辑表达式 True循环终止
...
break
{ 中断循环 }
continue
{ 跳至下一个循环 }
...
until <逻辑表达式>;
=================================
对象导向结构
C++与OP都可称为是对象导向式的语言.
C++可以用多重继承(multiple inheritance),OP只能单一继承(single inheritance)
让我们来看看建立对象的基本语法:

C++
Classes必须定义在header檔中...
class LoanC // 未有继承时
{
private: // 内定为private
...
protected: // 要有冒号
...
public:
...
}
//分号结尾

在C++中单一继承的语法如下:
class B: A
{...};

在C++中多重继承的语法如下:
class D: A, B, C
{...};

classes继承对象时可标明public, protected, 或 private.
内定是为 private
class D: public A, public B, private C
{...};

OP
Classes是在unit/file中的 &quot;type&quot
结构中宣告...
type
LoanC = class(TObject)
{这表是从TObject继承下来. }
{此处不用 &quot;begin&quot
指令}
private
...
protected {此处不用冒号}
...
public
...
published
...
end;
关键词 private, protected, public, 及 published (只有OP有) 指明了
每一个数据元素的数据范围宽广.
private:只能给在Class内部使用
protected:只能给这个Class或演生的Class使用
public:可以在程序所有的地方使用
Published:Delphi专用,用来建立接口
每一个Class需要一个建立者(constructor)
在C++理,建立者与class同名.
建立者可加参数:
ie:
LoanC()
// 无参数
LoanC(double &amp;dCurrentBal)
// 一个参数
LoanC(double &amp;dBalOne, double &amp;dBalTwo)
// 两个参数

在OP理,建立者与class不同名.
ie:
constructor MyLoanOne
{no parameters}
constructor MyLoanTwo(var dCurrentBal: double);
constructor MyLoanThree(var dBalOne, dBalTwo: double);
C++与OP都提供解构者(destructors),也可称为释放内存(free memory)
在C++理, 解构者与建构者一样,与class同名.
如:
~LoanC()
// C++ destructor

在OP理, 解构者与class不同名.
如:
destructor LoanC.Destroy;
begin
oLoanDate.Free;
...
inherited Destroy
{&quot;inherited&quot
is a keyword}
end;
建立一个新对象,宣告一个变量:
double dAmount = 1515.75;
LoanC oMyLoan(dAmount);
如此做会配置一块内存,
如果用只标方式可以如下宣告:
double dAmount = 1515.75;
LoanC * poMyLoan = new LoanC(dAmount);
在OP理则不同,每一个都是指标.
var
dAmount: double;
oMyLoan: LoanC;
begin
{oMyLoan does not yet exist!}
dAmount := 1515.75;
oMyLoan := LoanC.MyLoanTwo(dAmount)
{now it does}
在OP理也可也 Override.
type
LoanC = class
...
constructor Create
{overrides TObject's Create}
...
end;
... and call the inherited Create constructor in the LoanC definition...
constructor LoanC.Create;
begin
inherited Create
{calls TObject's Create}
...
end;

C++与OP理引用内部元素采用如下语法:
oMyLoan.dPrincipal;
在C++中,使用指针引用内部元素采用如下语法:
poMyLoan->dPrincipal;
在C++中有三个运操作数:
1. &amp
取地址
2. * 取指标
3. -> 印用元素
在OP中都用.(逗点号)
=================================
Containers
C++:Containers
typedef TISetAsVector tdCompanySet;
typedef TISetAsVectorIterator tdCSetIter;
...
int OwlMain(int, char*[])
{...
tdCompanySet oCompColl;
...
}
...
void MainForm::AddNewCompany()
{
CompanyC * poC = new CompanyC;
oCompColl.Add(new CompanyC(poC));
...
// now iterate
tdCSetIter oCIter(oCompColl);
while(oCIter)
{
poC = oCIter++;
// now use poC
...
}
}
OP:Containers
TMainForm = class(TForm)
...
public
oCompColl: TList;
...
end;
...
procedure TMainForm.AddNewCompany;
var
poC: CompanyC;
iCount: integer;
begin
poC := CompanyC.Create;
...
oCompColl.Add(poC);
...
{now iterate}
for iCount := 0 to oCompColl.Count - 1 do
begin
poC := oCompColl.Items[iCount];
{now use poC}
...
end;

=================================
例外处理
C++:例外处理
try
{
...
}
catch()
{
...
}
catch(...)
{
...
}


OP:例外处理
try
{不用 begin ... end
指令}
...
finally
{例外处理}
...
end;

取得Run Time 错误讯息时:
try
...
except
on do ... ;
end;

=================================
资料流(Streaming)

C++:资料流
class CompanyC
{
...
friend ofstream &amp
operator <<(ofstream &amp;oS, CompanyC * poC);
...
};
...
friend ofstream &amp
operator <<(ofstream &amp;oS, CompanyC * poC)
{
oS << poC->dVersion
<< poC->dLastDivPerShare
<< poC->enIndustry
...
<< poC->sName;
return oS;
}

friend ofstream &amp
operator <<(ofstream &amp;oS, CompanyC * poC)
{
oS.write( (char*) &amp
(*poC), sizeof(*poC));
return oS;
}
// 打开 a stream
ofstream ofS(&quot;gamedata.gam&quot;);
if(!ofS)
return;
...
// 存档
tdCSetIter oCIter(oCompColl)
// see container section
while(oCIter)
{
poC = oCIter;
ofS << poC;
}
ofS.close();


OP:资料流
type
CompanyC = class
public
procedure Read(var f:file);
procedure Write(var f:file);
...
end;
...
procedure CompanyC.Write(var f:file);
begin
BlockWrite(f, dVersion, sizeof(dVersion));
BlockWrite(f, dLastDivPerShare, sizeof(dLastDivPerShare));
BlockWrite(f, enIndustry, sizeof(enIndustry));
...
BlockWrite(f, sName, sizeof(sName));
end;

开档:
procedure TMainForm.FileSaveItemClick(Sender: TObject);
var
oGameFile: file;
iCount: integer;
poC: CompanyC;
sNameFile: string[13];
begin
...
sNameFile := 'gamedata.gam';
AssignFile(oGameFile, sFileName);
Rewrite(oGameFile, 1)
{the 1 means 1 byte at a time}
...
for iCount := 0 to oCompColl.Count - 1 do
begin
poC := oCompColl.Items[iCount];
poC.Write(oGameFile);
end;
CloseFile(oGameFile);
end;

=================================
项目档的设计
在 C++中,常数定义放在.h文件中,程序放在.cpp中(会用#include .h檔)
在 Delphi中,常数定义与程序都在一个文件.pas中
不同的.pas可用uses指令引用别的.pas宣告个变量
=================================
如何转换
如果要转有画面的C++程序要比转无画面的C++程序困难的多.
如果要转有画面的C++程序,要先在Dlephi开一Form,再将c++的程序代码转入.
要将程序copy 至delphi中再来改.
1) 先整体性的转换 ,将 C++ 的{...} 改为 begin...end;
double CompanyC::NewAnnualReport_v(Economy &amp
oE)
{ ... // delimiter starts on a new line
}

...versus this...
double CompanyC::NewAnnualReport_v(Economy &amp
oE){ // delimiter at end of line
...
}

2) a) 将 C++ 的 || 转为 &quot;or&quot;
b) 将 C++ 的 &amp;&amp
转为 &quot;and&quot;
c) (1)将 C++ 的 == 转为 =
(2)将 C++ 的 = 转为 :=
d) 将 C++ 的 /* */ 转为 { }
e) C++的 // 不用转
f) 将 双引号转为单引号 &quot
-> '

3) C++的每个if要加 then
如:
C++
if(oE.enDirection == Up &amp;&amp;
oE.uNumMosUpYr >= oE.uNumMosDownYr)
{
...
}

OP
if (oE.enDirection = Up) and
(oE.iNumMosUpYr >= oE.iNumMosDownYr) then
begin
...
end;

4) 将控制指令修改

5) C++的 overloaded 运算可 re-written,那就看您在OP中要如何处理.
6) 将 C++ 的 :: 转为 .
procedure TForm1.Memo1Change(Sender: TObject)
{要加上分号}
begin
...
end;
7) 将 C++ 的 ++ 和 decrement -- 改为inc(x) 与dec(
8) 将C++在区块 中宣告的变量全部移到var区中.
9) 将C++的函数名称前加上 &quot;function&quot
或 &quot;procedure&quot;
10) 将 C++ 的 -> 转为 . (句点)
DateC oBirthDate;
var
oBirthDate: DateC;
begin
oBirthDate := DateC.Create;
11) 将 C++ 的strcpy(...) 与 strcat(...) 转为 := 与 +

12) 那多重继承呢?( multiple inheritance).
用Class B继承Class A,再用class C继承 B的方式 .
=================================
结论
C++ 是简短的语言, 而 OP 比较像英文.
然而C++较紧密,但不易阅读.

============完结=============
 
你这个没有点ecchi文件, 不知道哪些函数和符号需要提供在单元文件里面.
发一个简单的makefile吧, make出omf的o文件用的, 在这个代码测试通过了(楼主的一个中括号8成了笑脸8表情符要改回去)

CC = GCC
CCOPTS = -O3 -s -c

.PHONY: all
all: des_gcc_omf.o

des_gcc_omf.o: des.c
$(CC) $(CCOPTS) -o $@ $^

%_omf.o: %_coff.o
cp -f $^ $@
/c/DM/bin/coff2omf $@
/c/DM/bin/omf2d $@ //U_* //U*_

.PHONY: clean
clean:
@rm -f *.o
 
如果是DES算法,找TurboPower 的 LockBox,用你的 C 加密一个东东,用Lockbox解一下,如果能用就可以了,不用自己转了。
 
文件:desobj.pas
unit desobj;

interface

procedure _memset (s: Pointer
c: Byte
n: Integer)
cdecl;

{$LINK 'des_gcc_421_omf.o'}
//{$LINK 'des_bcc58.obj'}

function _EncryptPkg(key:PChar
input, output:Array of Char
size:Integer):Integer
cdecl
external;
function _DencryptPkg(key, input, output:PChar
size:Integer):Integer
cdecl
external;

var
EncryptPkg: function(key:PChar
input, output:Array of Char
size:Integer):Integer
cdecl = _EncryptPkg;
DencryptPkg: function(key, input, output:PChar
size:Integer):Integer
cdecl = _DencryptPkg;

implementation

procedure _memset(s: Pointer
c: Byte
n: Integer)
cdecl;
begin
FillChar(s^, n, c);
end;

end.
文件:Makefile (昨天第7行打错了)
CC = GCC
CCOPTS = -O3 -s -c

.PHONY: all
all: des_gcc_omf.o

des_gcc_coff.o: des.c
$(CC) $(CCOPTS) -o $@ $^

%_omf.o: %_coff.o
cp -f $^ $@
/c/DM/bin/coff2omf $@
/c/DM/bin/omf2d $@ //U_* //U*_

.PHONY: clean
clean:
@rm -f *.o


gcc 4.21/4.11,bcc55/58,D7/D2007测试通过.
貌似找不到上传附件的地方, 楼主可以自行编译
 
我要的是上面c的对应的delphi写的东东
还请各位帮帮忙
 
转了 自己测试
unit Unit2;

interface

(*
#define AHPOST /* 定义模块名, 按文件名改为全大写*/

/*
* 以下定义函数需要的系统头文件
*/

#include <stdio.h>
#include <string.h>
#include <math.h>

/*
* 以下定义本模块中的静态函数与静态变量
*/
*)

const pc_1_c : array [0..27] of Integer =(
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);

const pc_1_d : array [0..27] of Integer =(
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);

const pc_2 : array [0..47] of Integer =(
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);

const ls_count : array [0..15] of Integer =(
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1);

const ip_tab : array [0..63] of Integer =(
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);

const _ip_tab : array [0..63] of Integer =(
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);

const e_r : array [0..47] of Integer =(
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);

const P1 : array [0..31] of Integer =(
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);

var
SSS : array [0..15, 0..3, 0..15] of Integer;

const sss_11xxx : array [0..511] of Integer=(
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,2,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,12,

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,0,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);

var
K : array [0..17, 0..48] of char;

implementation

procedure str8to64(_in, _out: PChar);
var
i, j: Integer;
bit : Byte;
begin
for i := 0 to 8 - 1 do
begin
bit := $80;
for j := 0 to 8 - 1 do
begin
Inc(_out);
if (Byte(_in) and bit) <> 0 then
_out[0] := #1
else
_out[0] := #0;
bit := bit shr 1;
end;
end;
end;

procedure str64to8(_in, _out: PChar);
var
bit : Byte;
i, j: Integer;
begin
for i := 0 to 8 - 1 do
begin
bit := $80;
_out := #0;
for j := 0 to 8 - 1 do
begin
Inc(_in);
if _in[0] <> #0 then
Inc(_out, bit);
bit := bit shr 1;
end;
end;
end;

procedure LS(bits : PChar
buffer: PChar
count: Integer);
var
i: Integer;
begin
for i := 0 to 28 - 1 do
begin
buffer := bits[(i + count) mod 28];
end;
end;

procedure son(cc,dd,kk : PChar);
var
i: Integer;
buffer : array [0..55] of char;
begin
for i := 0 to 28 - 1 do
begin
Inc(cc);
buffer := cc[0];
end;
for i := 28 to 56 - 1 do
begin
Inc(dd);
buffer := dd[0];
end;
for i := 0 to 48 - 1 do
begin
Inc(kk);
kk[0] := buffer[pc_2-1];
end;
end;

procedure des_setkey(bits :PChar);
var
i, j: Integer;
C : array [0..16, 0..28] of char;
D : array [0..16, 0..28] of char;
begin
for i := 0 to 28 - 1 do
C[0, i] := bits[pc_1_c - 1];
for i := 0 to 28 - 1 do
D[0, i] := bits[pc_1_d - 1];
for j := 0 to 16 - 1 do
begin
LS(@C[j], @C[j + 1], ls_count[j]);
LS(@C[j], @C[j+1], ls_count[j]);
LS(@D[j], @D[j+1], ls_count[j]);
son(@C[j+1], @D[j+1], @K[j+1]);
end;
end;


procedure ip(text,ll,rr: PChar);
var
i: Integer;
begin
for i :=0 to 32 - 1 do
ll := text[ip_tab - 1];
for i :=0 to 32 - 1 do
rr := text[ip_tab[i+32]-1];
end;

procedure _ip(text,ll,rr: PChar);
var
i: Integer;
tmp: array [0..63] of char;
begin
for i := 0 to 32 - 1 do
tmp := ll;
for i := 32 to 64 - 1 do
tmp := rr[i - 32];
for i := 0 to 64 - 1 do
text := tmp[_ip_tab-1];
end;

procedure s_box(aa,bb: PChar);
var
i, j, k, m, y, z: Integer;
ss: array [0..7] of char;
begin
m := 0;
for i := 0 to 8 - 1 do
begin
j := 6 * i;
y := Byte(aa[j])*2 + Byte(aa[j+5]);
z := Byte(aa[j+1])*8+ Byte(aa[j+2])*4 + Byte(aa[j+3])*2+Byte(aa[j+4]);
ss := Char(SSS[i, y, z]);
y := $08;
for k := 0 to 4 - 1 do
begin
Inc(m);
bb[m] := Char((Byte(ss) div y) and 1);
y := y div 2;
end;
end;
end;

procedure F(n : Integer
ll,rr,ll_1,rr_1: PChar);
var
i: Integer;
buffer : array [0..63] of char;
tmp : array [0..63] of char;
begin
for i := 0 to 48 - 1 do
buffer := rr[e_r - 1];
for i := 0 to 48 - 1 do
buffer := Char((Byte(buffer)+Byte(K[n])) and 1);

s_box(buffer,tmp);

for i := 0 to 32 - 1 do
buffer :=tmp[P1-1];

for i := 0 to 32 - 1 do
rr_1 := Char((Byte(buffer)+Byte(ll)) and 1);

for i := 0 to 32 - 1 do
ll_1 := rr;
end;


procedure encrypt0(text,mtext: PChar);
var
i, j: Integer;
ll, rr, ll_1, rr_1: array [0..63] of char;
begin
ip(text,ll,rr);

for i := 1 to 17 - 1 do
begin
F(i, ll, rr, ll_1, rr_1);
for j := 0 to 32 - 1 do
begin
ll[j] := ll_1[j];
rr[j] := rr_1[j];
end;
end;

_ip(mtext,rr,ll);
end;



procedure discrypt0(mtext,text: PChar);
var
i, j: Integer;
ll, rr, ll_1, rr_1: array [0..63] of char;
begin
ip(mtext,ll,rr);


for i := 16 downto 1 do
begin
F(i, ll, rr, ll_1, rr_1);
for j := 0 to 32 - 1 do
begin
ll[j] := ll_1[j];
rr[j] := rr_1[j];
end;
end;

_ip(text,rr,ll);
end;


procedure DES(key,text,mtext: PChar);
begin
des_setkey(key);
encrypt0(text,mtext);
end;

procedure _DES(key,mtext,text: PChar);
begin
des_setkey(key);
discrypt0(mtext,text);
end;

(************************************************************************
Porcedure Name: encrypt_8to8

Summary: encrypt a 8 byte string using a 16 byte key(the value must from
1 to F) which in bcd fromat taking 64 byte space.

Interface:
Input paramter - char key_bit[65]

char str_in[9];
Output paramter - char str_out[9]

***********************************************************************)

procedure encrypt_8to8(key_bit,str_in,str_out: PChar);
var
str_bit_in : array [0..64] of char;
str_bit_out: array [0..64] of char

begin
str8to64( str_in, str_bit_in );

DES(key_bit,str_bit_in,str_bit_out);

str64to8( str_bit_out, str_out );
end;

(************************************************************************
Porcedure Name: dencrypt_8to8

Summary: dencrypt a 8 byte string using a 16 byte key(the value must from
1 to F) which in bcd fromat taking 64 byte space.

Interface:
Input paramter - char key_bit[65]

char str_in[9];
Output paramter - char str_out[9]

***********************************************************************)

procedure dencrypt_8to8(key_bit,str_in,str_out:PChar);
var
str_bit_in : array [0..64] of char;
str_bit_out: array [0..64] of char

begin
str8to64( str_in, str_bit_in );

_DES(key_bit,str_bit_in,str_bit_out);

str64to8( str_bit_out, str_out);
end;

function EncryptPkg(key, _in, _out: PChar
size: Integer):Integer;
var
i : integer;
key_bit: array [0..64] of char;
begin
Result := -1;
FillChar(key_bit[0], 65, 0);

str8to64(key,key_bit);

if ( ( size mod 8 ) <> 0 ) then
Exit


for i := 0 to (size div 8) - 1 do
encrypt_8to8(key_bit,@_in[i*8],@_out[i*8]);

Result := 0;
end;

function DencryptPkg(key, _in, _out:PChar
size: Integer):Integer;
var
i : integer;
key_bit: array [0..64] of char;
begin
Result := -1;
FillChar(key_bit[0], 65, 0);

str8to64(key,key_bit);

if ( ( size mod 8 ) <> 0 ) then
Exit


for i := 0 to (size div 8) - 1 do
dencrypt_8to8(key_bit,@_in[i*8],@_out[i*8]);

Result := 0;
end;

initialization
Move(sss_11xxx[0], SSS[0], sizeof(sss_11xxx));

end.
 
http://my1993.com/downloads/source/desobj.rar
我传到偶的网站了
说实话, 没见过DES这么写的c单元, 可能是故意改的, 而且没有.h文件, 而且dencrypt名字可能是故意从decrypt写这样的, 为防止其他手脚, 用D直接链接C的代码可能比发给你Delphi版的DES更可靠, 虽然好的des单元一抓一堆.
如果原来c代码有什么bug, 保留会得到同样的结果.
 
后退
顶部