Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063560
  • 博文数量: 106
  • 博客积分: 9093
  • 博客等级: 中将
  • 技术积分: 2770
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-01 17:22
文章分类

全部博文(106)

文章存档

2014年(1)

2012年(29)

2011年(32)

2010年(20)

2008年(24)

分类: 系统运维

2011-12-26 14:48:07

Part one中,我们使用的key是固定值(KEYD0200Des.Key        = 'abcdefg1234567890';),那没有什么方法不让key是固定的那。答案是可定的。我们可以使用API Qc3GenPRNs来产生pseudorandom numbers. 用产生的pseudorandom numbers做key。但是API Qc3GenPRNs产生的是binary stream,不容易查看,我们可以使用C函数CVTCH把它转换成十六进制,这样便于存储和查看。从十六进制转换为字符可以使用CVTHC。

你可以在下面的网站了解一下上面提到API:

 

在rpgle中使用上述API的原型如下:

**-- Generate pseudorandom numbers
D GenRndNbr       Pr                  ExtProc( 'Qc3GenPRNs' )
D  PrnDta                     1024a          Options( *VarSize )
D  PrnDtaLen                    10i 0 Const
D  PrnTyp                        1a   Const
D  PrnPar                        1a   Const
D  Error                     32767a          Options( *VarSize )
**-- Convert character to hex
D cvtch           Pr                  ExtProc( 'cvtch' )
D  RcvChr                         *   Value
D  SrcHex                         *   Value
D  SrcLen                       10i 0 Value
**-- Convert hex to character
D cvthc           Pr                  ExtProc( 'cvthc' )
D  RcvHex                         *   Value
D  SrcChr                         *   Value
D  RcvLen                       10i 0 Value

为了方便查看我们可是使用API QUILNGTX显示在pop-up窗口里。相关此API的信息参考如下:

此API在rpgle中的原型如下:

**-- Display long text:
D DspLngTxt       Pr                  ExtPgm( 'QUILNGTX' )
D  DtLngTxt                  32767a   Const  Options( *VarSize )
D  DtLngTxtLen                  10i 0 Const
D  DtMsgId                       7a   Const
D  DtMsgF                       20a   Const
D  DtError                   32767a   Const  Options( *VarSize )

 

下面是一个简单的完整的小程序。先把inputData(‘It is secret info’)加密,然后用API QUILNGTX显示出来要加密的文字,加密的key,解密后的字符,然后在解密加密后的文字显示出来。


h DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')
**
** EncryptData API prototype
d EncryptData     Pr                  Extproc('Qc3EncryptData')
d  clearData                 65535A   const Options(*Varsize)
d  clearDatalen                 10I 0 Const
d  clearDataFmt                  8A   Const
d  algoDescr                    64A   Const Options(*Varsize)
d  algoFormat                    8A   Const
d  keyDescriptor               512A   Const Options(*Varsize)
d  keyFormat                     8A   Const
d  cryptoService                 1A   Const
d  cryptoDevName                10A   Const options(*omit)
d  encryptedData...
d                            65535A   Options(*Varsize)
d  encryptedDataLen...
d                               10I 0 Const
d  encryptedDataReturnLen...
d                               10I 0
d  error                     32767A   Options( *VarSize )
**
** DecryptData API prototype
d DecryptData     Pr                  Extproc('Qc3DecryptData')
d  encryptedData...
d                            65535A   Const Options(*Varsize)
d  encryptedDataLen...
d                               10I 0 Const
d  algoDescr                    64A   Const Options(*Varsize)
d  algoFormat                    8A   Const
d  keyDescriptor               512A   Const Options(*Varsize)
d  keyFormat                     8A   Const
d  cryptoService                 1A   Const
d  cryptoDevName                10A   Const options(*omit)
d  clearData                 65535A   Options(*Varsize)
d  clearDatalen                 10I 0 Const
d  clearDataReturnLen...
d                               10I 0
d  error                     32767A   Options( *VarSize )
**
** Generate pseudorandom numbers
d GenRndNbr       Pr                  ExtProc( 'Qc3GenPRNs' )
d  PrnDta                     1024a          Options( *VarSize )
d  PrnDtaLen                    10i 0 Const
d  PrnTyp                        1a   Const
d  PrnPar                        1a   Const
d  Error                     32767a          Options( *VarSize )
**
** Convert character to hex
D cvtch           Pr                  ExtProc( 'cvtch' )
D  RcvChr                         *   Value
D  SrcHex                         *   Value
D  SrcLen                       10i 0 Value
**
** Convert hex to character
D cvthc           Pr                  ExtProc( 'cvthc' )
D  RcvHex                         *   Value
D  SrcChr                         *   Value
D  RcvLen                       10i 0 Value
**
** Display long text:
D DspLngTxt       Pr                  ExtPgm( 'QUILNGTX' )
D  DtLngTxt                  32767a   Const  Options( *VarSize )
D  DtLngTxtLen                  10i 0 Const
D  DtMsgId                       7a   Const
D  DtMsgF                       20a   Const
D  DtError                   32767a   Const  Options( *VarSize )
**
** Set algorithm and key description structure
d SetAlgoKeyDesFmt200...
d                 Pr
d keyString                     16A   Const
**
** ALGD0200 algorithm description structure
d ALGD0200Des     ds                  Qualified
d  BlkCphAlgo                   10I 0
d  BlkLen                       10I 0
d  Mode                          1A
d  PadOpt                        1A
d  PadChar                       1A
d  Resvd                         1A
d  MAClen                       10I 0
d  EffKeySiz                    10I 0
d  InitVct                      32A
**
** KEYD0200 key description format structure
d KEYD0200Des     ds                  Qualified
d  KeyType                      10I 0
d  KeyStrLen                    10I 0
d  KeyFormat                     1A
d  Resvd                         3A
d  Key                          16A

**
** Error structure
D ERRC0100        ds                  Qualified
D  BytPrv                       10i 0 Inz( %Size( ERRC0100 ))
D  BytAvl                       10i 0
D  MsgId                         7a
D                                1a
D  MsgDta                      128a
**
d InputData       s             17A   Inz('It is secret info')
d EncryptedData   s             32A   Inz(*Blanks)
d DecryptedData   s             32A   Inz(*Blanks)
d rtnLen          s             10I 0
d csp             s              1A   Inz('1')
d KeyStrChr       s             16A   Inz(*Blanks)
d KeyStrHex       s             32A   Inz(*Blanks)
d KeyStrChrC      s             16A   Inz(*Blanks)
d MsgStr          s            590A   Inz(*Blanks)
D Blanks          s             48a
**
/free
    //set algorithm and key description format
    CallP GenRndNbr( KeyStrChr
                   : %size( KeyStrChr )
                   : '0'
                   : '0'
                   : ERRC0100
                   );

    // convert char to hex
    CallP cvthc( %Addr( KeyStrHex )
               : %Addr( KeyStrChr )
               : %Len( %trim( KeyStrChr )) * 2
               );

    //set algorithm and key description format
    CallP SetAlgoKeyDesFmt200( KeyStrChr
                             );

    //encrypt data
    CallP EncryptData( InputData
                     : %len(InputData)
                     : 'DATA0100'
                     : ALGD0200Des
                     : 'ALGD0200'
                     : KEYD0200Des
                     : 'KEYD0200'
                     : csp
                     : *omit
                     : EncryptedData
                     : %size(EncryptedData)
                     : rtnLen
                     : ERRC0100
                     );

    if ERRC0100.BytAvl = 0;
       MsgStr = 'Data string . . : ' + %Trim( InputData ) + Blanks +
                'Key string C. . : ' + %Trim( KeyStrChr ) + Blanks +
                'Key string H. . : ' + %Trim( KeyStrHex ) + Blanks +
                'Cipher string . : ' + %Trim( EncryptedData );
       CallP DspLngTxt( MsgStr
                      : %Len( MsgStr )
                      : *Blanks
                      : *Blanks
                      : ERRC0100
                      );
    else;
       dsply ERRC0100.MsgId;
    endif;

    // convert hex to char
    CallP cvtch( %Addr( KeyStrChrC )
               : %Addr( KeyStrhex )
               : %Len( %trim( KeyStrHex ))
               );

    //decrypt data
    CallP DecryptData( EncryptedData
                     : %len(EncryptedData)
                     : ALGD0200Des
                     : 'ALGD0200'
                     : KEYD0200Des
                     : 'KEYD0200'
                     : csp
                     : *omit
                     : DecryptedData
                     : %size(DecryptedData)
                     : rtnLen
                     : ERRC0100
                     );

    if ERRC0100.BytAvl = 0;
       MsgStr = 'Data string . . : ' + %Trim( InputData ) + Blanks +
                'Key string C. . : ' + %Trim( KeyStrChr ) + Blanks +
                'Key string H. . : ' + %Trim( KeyStrHex ) + Blanks +
                'Cipher string . : ' + %Trim( EncryptedData ) + Blanks +
                'Key string H. . : ' + %Trim( KeyStrHex ) + Blanks +
                'Key string C. . : ' + %Trim( KeyStrChrC ) + Blanks +
                'Clear text  . . : ' + %Trim( DecryptedData );
       CallP DspLngTxt( MsgStr
                      : %Len( MsgStr )
                      : *Blanks
                      : *Blanks
                      : ERRC0100
                      );
    else;
       dsply ERRC0100.MsgId;
    endif;

    *inlr = *on;
    return;
/end-free

**-- Subprocedure definition -------------------------------------------**
p SetAlgoKeyDesFmt200...
p                 b
d SetAlgoKeyDesFmt200...
d                 pi
d keyString                     16A   Const
/free
    //set Algorithm Description
    ALGD0200Des.BlkCphAlgo = 22;
    ALGD0200Des.BlkLen     = 16;
    ALGD0200Des.Mode       = '0';
    ALGD0200Des.PadOpt     = '1';
    ALGD0200Des.PadChar    = x'00';
    ALGD0200Des.Resvd      = x'00';
    ALGD0200Des.MAClen     = *zero;
    ALGD0200Des.EffKeySiz  = *zero;
    ALGD0200Des.InitVct    = *Allx'00';

    //set Key Description
    KEYD0200Des.KeyType    = 22;
    KEYD0200Des.KeyStrLen  = 16;
    KEYD0200Des.KeyFormat  = '0';
    KEYD0200Des.Resvd      = *Allx'00';
    KEYD0200Des.Key        = keyString;

/end-free
p SetAlgoKeyDesFmt200...
p                 e 

阅读(2213) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~