请问聊天之加密解密 ( 积分: 74 )

  • 主题发起人 主题发起人 yyyxxue
  • 开始时间 开始时间
Y

yyyxxue

Unregistered / Unconfirmed
GUEST, unregistred user!
我用Nmudp写了个局域网的聊天程序。现在,信息的发送接收过程中要求涉及到加密解密,我不知道这个部分应该怎么实现,请帮忙出出主意,十分感谢![:)]
 
我用Nmudp写了个局域网的聊天程序。现在,信息的发送接收过程中要求涉及到加密解密,我不知道这个部分应该怎么实现,请帮忙出出主意,十分感谢![:)]
 
最简单的:在发送前将要发送的内容进行加密,可以用流行的MD5,有现成控件。对方收到后再进行解密即可。
 
老大MD5不可逆的吧
 
晕,MD5加密是不行的,有去无回,试试别的算法吧,MD5做为一种数据校验还可以。
 
MD5是不可逆的.
我以前有一个可逆的加密算法的,要不?
zcjs@xinhuanet.com,自己来要
 
TEA 加解密也不错的
速度快,强度也不错
(QQ就是用的TEA加解密网络通信的)
 
我按照QQ的通信加解密算法,写了个PAS文件,要不要?
真接可以调用的
 
共享一下
 
to flfqzrj
十分感谢,请发到yyyxxue@163.com
 
请也发一份到 soft@skyour.com 谢谢!!
 
to flfqzrj
能否给小弟也发一份,renxm2003@126.com 先谢啦
 
大侠,也给我一份吧多谢
zhangchao_7622@126.com
以前我使用的3DES来加密的!速度不怎么样!!
 
unit encrypt;

interface

uses
Windows,winsock,SysUtils,unitmyvar;
Function qq_encrypt(instr:string;instrlen:integer;key:Tmydword;var outstr:string;var outstrlen_prt:integer):boolean;
procedure QQ_Encipher(V:TMyWord;k:TMyDword ;var w:TmyWORD);

implementation
var
plain:array[1..8] of char;
plain_pre_8:array[1..8] of char;
crypted:string;
crypted_pre_8:array[1..8] of char;
pos_in_byte,count:integer;
is_header:boolean;
padding:integer;
SessionKey:TmyDword;
procedure QQ_Encipher(V:TMyWord;k:TMyDword ;var w:TmyWORD);
var
y,Z,a,b,c,d:Longword;
n:Longword;
sum:Longword;
delta:Longword;
begin
y:=V[0];
z:=V[1];
a:=k[0];
b:=k[1];
c:=k[2];
d:=k[3];
n:=$10;
sum:=$0;
delta:=$9E3779B9;
while not (n = 0) do
begin
sum:=sum+delta;
inc(y,(((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b)));
inc(z,(((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d)));
n:=n-1;
end;
W[0]:=y;
W[1]:=z;
end;

procedure encrypt_every_8_byte(); //8个字节8个字节的加密
var
i:integer;
outtempstr:array[1..8] of char ;
inmyword:Tmyword;
outmyword:Tmyword;
begin
For i:=1 to 8 do
begin
if is_header then //如果是头8个BYTE就进行如下操作
plain:= char(byte(plain) xor byte(plain_pre_8)) //把PLAIN 与 PLAIN_PRE_8 异或 这里的plain_pre_8是 0x00
else
plain:=char( byte(plain) xor byte(crypted_pre_8))
//把PLAIN 与 CRYPTED_PRE_8 异或 crypted_pre_8 是上次加密得到的8个字节
end;
inmyword:= Tmyword(plain);
inmyword[0]:=ntohl(inmyword[0]);
inmyword[1]:=ntohl(inmyword[1]);
qq_encipher(inmyword,Sessionkey,outmyword); //加密PLAIN 并传到 OUTTEMSTR
outtempstr[1]:=Char((outmyword[0] and $FF000000) div $1000000);
outtempstr[2]:=Char((outmyword[0] and $00FF0000) div $10000);
outtempstr[3]:=Char((outmyword[0] and $0000FF00) div $100);
outtempstr[4]:=Char(outmyword[0] and $000000FF);
outtempstr[5]:=Char((outmyword[1] and $FF000000) div $1000000);
outtempstr[6]:=Char((outmyword[1] and $00FF0000) div $10000);
outtempstr[7]:=Char((outmyword[1] and $0000FF00) div $100);
outtempstr[8]:=Char(outmyword[1] and $000000FF);
for i:=1 to 8 do //把OUTTEMPSTR 与 PLAIN_PRE_8 异或并传给 CRYPTE_PRE_8
begin
crypted_pre_8:=char(byte(outtempstr) xor byte(plain_pre_8));
crypted:=crypted+char(crypted_pre_8);
end;
for i:=1 to 8 do
plain_pre_8:= plain;


//inc(crypted ,8);
count:=count+8;
pos_in_byte:=1; //pos_in_byte 清零
is_header:=False;

end; //加密每8个BYTE 结束

function rand():integer;
begin
rand:=$dead;
end;

Function qq_encrypt(instr:string;instrlen:integer;key:Tmydword;
var outstr:string;var outstrlen_prt:integer):boolean;
var
inp:string;
a:byte;
b:byte;
i:integer;
begin
pos_in_byte:=1;
is_header:=True;
count:=0;
padding:=0;
crypted:='';
Sessionkey:=key;

pos_in_byte:=(instrlen+$a) mod 8; //加上输入长度 + 10以后 MOD 8 得到多余的长度
if (pos_in_byte) <>0 then pos_in_byte:=8-pos_in_byte; //得到要补充的长度
a:= byte(rand() and $F8);
b:= byte(pos_in_byte);
plain[1]:=char(a or b); //在plain[1] 里放补充的长度
for i:=2 to pos_in_byte +1 do
begin
fillmemory(@plain,1,(rand() and $FF) ); //填充字符
end;
pos_in_byte:=pos_in_byte+2 ;
fillmemory(@plain_pre_8,8,0); //把 plain_pre_8 的8字节填充成 0x00

padding:=1;

while (padding<=2) do //如果长度还不满 8位再填充 最多只能填充 2 BYTE
begin
if pos_in_byte <9 then
begin
plain[pos_in_byte]:=char(byte( rand() and $FF));
pos_in_byte:=pos_in_byte+1;
padding:=padding+1;
end;
if pos_in_byte=9 then encrypt_every_8_byte(); //如果满8位就加密
end;

inp:=instr; //把传入的 字符传给 INP

while (instrlen >0) do //把所有的字符进行加密 多出来的字符给下一步操作
begin
if pos_in_byte<9 then
begin
plain[pos_in_byte]:=inp[1];
pos_in_byte:=pos_in_byte+1;
delete(inp,1,1); //指针下移 inp[0]=INP[1]
instrlen:=instrlen-1;
end;
if pos_in_byte=9 then encrypt_every_8_byte();
end;

padding:=1;
while(padding<=8) do
begin
if (pos_in_byte <9) then
begin
plain[pos_in_byte]:=char($0);
pos_in_byte:=pos_in_byte+1;
padding:=padding+1;
end;
if (pos_in_byte=9) then encrypt_every_8_byte();
end;
outstrlen_prt:=count;
outstr:= crypted;
result:=True;
end;

end.
 
不能用不可逆加密法.
 
unit decrypt;

interface
uses
Windows,winsock,SysUtils,unitmyvar;

Function QQ_Decrypt(instr:string;instrlen:integer;key:TMyDword;var Outstr:string;var Outstrlen_ptr:integer):boolean ;


implementation



var
Decrypted:array[1..8] of char;
Crypt_Buff:string;
crypt_buff_ft8:string;
Pos_in_byte,Context_start:integer;
SessionKey:TMyDword;

procedure QQ_Decipher(V:TmyWORD;k:TmyDword;var w:TmyWORD);
var
y,Z,a,b,c,d:Longword;
n:Longword;
sum:Longword;
delta:Longword;
begin
y:=V[0];
z:=V[1];
a:=k[0];
b:=k[1];
c:=k[2];
d:=k[3];
n:=$10;
sum:=$E3779B90;
delta:=$9E3779B9;
while not (n = 0) do
begin
z:=z-(((y shl 4) + c) xor (y + sum )xor ((y shr 5) + d));
y:=y-(((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
//dec(z,(((y shl 4) + c) xor (y + sum )xor ((y shr 5) + d)));
//dec(y,(((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b)));
sum:=sum - delta;
n:=n-1;
end;
W[0]:=y;
W[1]:=z;
end;

Function Decrypt_every_8_byte(instrlenm:integer):Boolean;
var
i:integer;
a:byte;
b:byte;
outstrtemp:array[1..8] of char;
inmyword:Tmyword;
outmyword:Tmyword;
begin
result:=False;
for i:=1 to 8 do
begin
if ((context_start+i-1)>= instrlenm ) then exit;
a:= byte(decrypted);
b:= byte(crypt_buff);
decrypted:=char(a xor b);
end;

for i:=1 to 8 do
outstrtemp:=decrypted;

inmyword:= Tmyword(outstrtemp);
inmyword[0]:=ntohl(inmyword[0]);
inmyword[1]:=ntohl(inmyword[1]);
QQ_Decipher(inmyword,SessionKey,outmyword);
outstrtemp[1]:=Char((outmyword[0] and $FF000000) div $1000000);
outstrtemp[2]:=Char((outmyword[0] and $00FF0000) div $10000);
outstrtemp[3]:=Char((outmyword[0] and $0000FF00) div $100);
outstrtemp[4]:=Char(outmyword[0] and $000000FF);
outstrtemp[5]:=Char((outmyword[1] and $FF000000) div $1000000);
outstrtemp[6]:=Char((outmyword[1] and $00FF0000) div $10000);
outstrtemp[7]:=Char((outmyword[1] and $0000FF00) div $100);
outstrtemp[8]:=Char(outmyword[1] and $000000FF);
for i:=1 to 8 do
decrypted:= outstrtemp;


context_start:= context_start+8;
//inc(crypt_buff,8);
crypt_buff_ft8:=crypt_buff;
delete(crypt_buff,1,8);
pos_in_byte:=1;
result:=True;
end;

Function QQ_Decrypt(instr:string;instrlen:integer;key:TMyDword;var Outstr:string;var Outstrlen_ptr:integer):boolean ;
var
m:string[8];
Crypt_Buff_Pre_8:string[8];
OutP:string;
Counts,Padding:integer;
strword:string[8] ;
strchartemp:array[1..8] of char;
i:integer;
inmyword:Tmyword;
outmyword:Tmyword;
begin
result:=False;
Sessionkey:=key; //初始化 SESSIONKEY 值
for i:=1 to 8 do
Decrypted:=#0;
Crypt_Buff:='';
crypt_buff_ft8:='';
Pos_in_byte:=1;
Context_start:=1;
if (instrlen <16) or ((instrlen mod 8)<>0) then exit;//如果长度小于16或者不为8的整数倍跳出
strword:=instr; //传入头8个BYTE

for i:=1 to 8 do
strchartemp:=strword;

inmyword:= Tmyword(strchartemp);
inmyword[0]:=ntohl(inmyword[0]);
inmyword[1]:=ntohl(inmyword[1]);
qq_decipher(inmyword,Sessionkey,outmyword); //解密头8个BYTE

decrypted[1]:=Char((outmyword[0] and $FF000000) div $1000000);
decrypted[2]:=Char((outmyword[0] and $00FF0000) div $10000);
decrypted[3]:=Char((outmyword[0] and $0000FF00) div $100);
decrypted[4]:=Char(outmyword[0] and $000000FF);
decrypted[5]:=Char((outmyword[1] and $FF000000) div $1000000);
decrypted[6]:=Char((outmyword[1] and $00FF0000) div $10000);
decrypted[7]:=Char((outmyword[1] and $0000FF00) div $100);
decrypted[8]:=Char(outmyword[1] and $000000FF);

pos_in_byte:= (integer(decrypted[1]) and $7); //得到填充的长度
counts:=instrlen-pos_in_byte-10; //得到实际有效内容的长度
if counts<0 then exit; //如果实际有效内容的长度小于0跳出
FillMemory(@m,8,0); //用0x00 填充M 8个BYTE
FillMemory(@crypt_buff_pre_8,8,0);
//crypt_buff_pre_8:=m; //传给CRYPT_BUFF_PRE_8作为异或参数
outstrlen_ptr:=counts; //实际文字的长度

crypt_buff:=instr; //这儿出错了
CRYPT_BUFF_FT8:=INSTR;
Delete(crypt_buff,1,8); //向下移8位
context_start:=8;
pos_in_byte:=pos_in_byte+2; //这里要+2 因为下界为1 + POS_IN_BYTE
padding:=1;
while (padding<=2) do
begin
if (pos_in_byte <9) then
begin
pos_in_byte:= pos_in_byte+1;
padding:=padding+1;
end;
if (pos_in_byte=9) then
begin
crypt_buff_pre_8:=instr;
if not decrypt_every_8_byte(instrlen) then exit;
end;
end;

outp:='';
while(counts <> 0) do
begin
if(pos_in_byte <9) then
begin
outp:=outp+char(byte(crypt_buff_pre_8[pos_in_byte]) xor byte(decrypted[pos_in_byte]));
counts:=counts-1;
pos_in_byte:=pos_in_byte+1;
end;
if (pos_in_byte=9) then
begin
crypt_buff_pre_8:=crypt_buff_ft8;
if not decrypt_every_8_byte(instrlen) then exit;
end;
end;

for padding:=1 to 7 do
begin
if pos_in_byte <9 then
begin
if integer(byte(Crypt_Buff_Pre_8[pos_in_byte]) xor byte(decrypted[pos_in_byte]))<>0 then break;
pos_in_byte:=pos_in_byte+1;
end;
if pos_in_byte=9 then
begin
crypt_buff_pre_8:=crypt_buff;
if not decrypt_every_8_byte(instrlen) then break;
end;
end;
outstr:=outp;
result:=True;
end;


end.
 
这是以前写的
还没有好好优化整理过

大家拿去了自已优化整理一下吧
(这是我第一次拿起DELPHI的时候写的程序,现在看看简直像坨屎)
 
后退
顶部