APP下载

实现Javascript-Delphi高强度加密传输安全

2018-02-05曾程胜

软件 2018年1期

曾程胜

(江西省抚州市临川区政务信息化工作办公室,江西 抚州 344000)

1 设计思想

为了保证应用前端向后端传输数据的安全,我们通常采用SSL加密方式,本文是在通常HTTP上实现前台Javascript向后台Delphi开发的服务器端传输数据的加密[1],防止由于第三方攻击导致重要信息泄露。由于RSA加密[2]对明文字符长度有限制,所以设计思想是:客户端首先向服务器端发出AJAX请求,获取后台随机生的RSA加密算法的公钥,然后客户端随机生成 AES对称加密算法[3]的Key及 iv向量,当客户端提交表单数据时,利用AES加密表单数据,再用RSA公钥加密AES的Key、iv向量,并以jQuery POST[4]方式向后台提交这些加密数据,最后在后台使用RSA私钥解密Key、iv,从而解密表单数据。

2 Web前端的实现。

前端使用了JAVASCRIPT加密库FORGE[5],下载地址 https://github.com/digitalbazaar/forge以及forge浏览器支持库 https://github.com/digitalbazaar/forge-dist,具体实现代码如下:

//引入JS库

3 后端的实现

下面我将分别探讨使用 OPENSSL[6]动态链接库及商用组件SecureBlackbox[7]实现后端处理。我这里服务器端开发采用 IIS[8]+RealThinClient[9]+ISAPI[10]方式,仅供参考。

3.1 使用OPENSSL动态链接库实现后端

由于直接使用openssl动态链接库不是很方便,我借助开源组件 ICS[11]进行开发,该组件完整实现了对 OPENSSL的封装,下载地址为 http://wiki.overbyte.eu/arch/icsv8w.zip。该方案优点是性能好,处理速度快,缺点是部署时须附带 OPENSSL动态链接库。

uses

System.SysUtils, System.Classes, rtcConn,rtcDataSrv, rtcISAPISrv, rtcInfo,

rtcSys

tem,OverbyteIcsSSLEAY,OverbyteIcsMimeUtils,Over byteIcsWSocket,

Over

byteIcsSslX509Utils,OverbyteIcsLibeay,OverByteIcs MD5,OverbyteIcsTypes,

OverbyteIcsLogger,OverbyteIcsUtils;

type

TWebModule = class(TDataModule)

GetPubKey: TRtcDataProvider;

PrdWebServer: TRtcISAPIServer;

SendEncTxt: TRtcDataProvider;

procedure GetPubKeyCheckRequest(Sender:TRtcConnection);

procedure SendEncTxtCheckRequest(Sender: TRtcConnection);

procedure SendEncTxtDataReceived(Sender:TRtcConnection);

procedure DataModuleCreate(Sender: Tobject);

procedure DataModuleDestroy(Sender: Tobject);

private

{ Private declarations }

FProgDir:string;

FSslCertTools: TSslCertTools;

public

{ Public declarations }

end;

const

AES_BLOCK_SIZE = 16;

type

TAesType = (aesCbc, aesEcb);

TAesContext = packed record

Ctx : PEVP_CIPHER_CTX;

Aes : PEVP_CIPHER;

Encrypt : Boolean;//True为加密False为解密

end;

PAesContext = ^TAesContext;

function StrToHex(AStr: AnsiString): AnsiString;

var

len : Integer;

begin

len:=Length(AStr);

SetLength(Result, len*2);

BinToHex(@AStr[1], PAnsiChar(Result), len);

Result:=string.LowerCase(Result);

end;

function HexToStr(AHex: AnsiString): AnsiS-

tring;

var

len : Integer;

begin

len:=Length(AHex)div 2;

SetLength(Result, len);

HexToBin(PAnsiChar(AHex),Result[1] , len);

end;

procedure AesFinalize(var AesCtx: TAesContext);

begin

if Assigned(AesCtx.Ctx) then begin

f_EVP_CIPHER_CTX_cleanup(AesCtx.Ctx);

f_EVP_CIPHER_CTX_free(AesCtx.Ctx);

end;

FillChar(AesCtx, SizeOf(AesCtx), #0);

end;

procedure AesInitialize(

var AesCtx : TAesContext;

Key : PAnsiChar; // if not nil Pwd is ignored and the user is responsible to provide a valid Key and IV

IV : PAnsiChar;

AesType : TAesType;

Enc : Boolean);

begin

AesFinalize(AesCtx);

AesCtx.Encrypt := Enc;

case AesType of

aesCbc, aesEcb :

begin

if AesType = aesCbc then

AesCtx.Aes := f_EVP_aes_256_cbc//CBC加密方式

else begin

AesCtx.Aes := f_EVP_aes_256_ecb;//ECB加密

end;

end;

else

raise Exception.Create('Not implemented');

end;

AesCtx.Ctx := f_EVP_CIPHER_CTX_new;

if ICS_OPENSSL_VERSION_NUMBER

f_EVP_CIPHER_CTX_init(AesCtx.Ctx)

else

f_EVP_CIPHER_CTX_reset(AesCtx.Ctx);

if not f_EVP_CipherInit_ex(AesCtx.Ctx,AesCtx.Aes, nil,

@key[0],

@IV[0],

Ord(Enc)) then

raise Exception.Create('Function f_EVP_CipherInit_ex');

end;

procedure AesUpdate(

const InBuf;

InLen : Integer;

const OutBuf;

var OutLen : Integer;

AesCtx : TAesContext);

begin

if not Assigned(AesCtx.Ctx) then

raise Exception.Create('Aes context not initialized');

if not f_EVP_CipherUpdate(AesCtx.Ctx,PAnsiChar(@OutBuf), OutLen,

AnsiChar(@InBuf),InLen) then

raise Exception.Create('f_EVP_Cipher-Update')

end;

procedure AesFinal(

const OutBuf;

var OutLen : Integer;

AesCtx : TAesContext);

begin

if not Assigned(AesCtx.Ctx) then

raise Exception.Create('Aes context not initialized');

if not f_EVP_CipherFinal_ex(AesCtx.Ctx,

PAnsiChar(@OutBuf), OutLen) then raise Exception.Create('Function f_

EVP_CipherFinal_ex:');

end;

{ Takes plain text, returns an encrypted string,optionally base64 encoded }

function StrEncAES(

const S : AnsiString;

Key : PAnsiChar;

IV : PAnsiChar;

B64 : Boolean): AnsiString;

var

Len, TmpLen : Integer;

AesCtx : TAesContext;

begin

FillChar(AesCtx, SizeOf(AesCtx), #0);

AesInitialize(AesCtx, Key, IV, aesCbc, True);

try

Len := Length(S);

SetLength(Result, Len + AES_BLOCK_SIZE);

AesUpdate(S[1], Length(S), Result[1],Len, AesCtx);

AesFinal(Result[Len + 1], TmpLen,AesCtx);

Inc(Len, TmpLen);

SetLength(Result, Len);

finally

AesFinalize(AesCtx);

end;

if B64 then

Result := Base64Encode(Result)

else

Result := StrToHex(Result)

end;

function StrDecAES(

S : AnsiString;

Key : PAnsiChar;

IV : PAnsiChar;

B64 : Boolean): AnsiString;

var

Len, TmpLen : Integer;

AesCtx : TAesContext;

begin

FillChar(AesCtx, SizeOf(AesCtx), #0);

AesInitialize(AesCtx, Key, IV, aesCbc,False);

try

if B64 then

S := Base64Decode(S);

Len := Length(S);

SetLength(Result, Len + AES_BLOCK_SIZE);

AesUpdate(S[1], Length(S), Result[1],Len, AesCtx);

AesFinal(Result[Len + 1], TmpLen,

AesCtx);

Inc(Len, TmpLen);

SetLength(Result, Len);finally AesFinalize(AesCtx);

end;end;

procedure TWebModule.DataModuleCreate(Sender: TObject);

begin

//挂载OPENSSL的动态链接库,这里须将动态……

登录APP查看全文