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

全部博文(106)

文章存档

2014年(1)

2012年(29)

2011年(32)

2010年(20)

2008年(24)

分类: 系统运维

2011-12-23 14:35:56

要想使用加密解密APIs,需要安装IBM的Cryptographic Service Provider for AS/400软件产品。在AS/400上使用DSPSFWRSC命令查看是否安装。

在V6R1M0系统下会看到如下信息,不知道为什么回事两个。呵呵。

                                 Display Software Resources

Resource

ID              Option Feature  Description

5761SS1       35     5050    CCA Cryptographic Service Provider

5761SS1       35     2924    CCA Cryptographic Service Provider

 

你可以在下面的网站了解一下加密解密API的介绍。

  • 解密API Qc3DecryptData

  • 加密API Qc3EncryptData

 

根据API文档的描述,在rpgle中的声明原型如下:

  • 解密API Qc3DecryptData

** 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 )

  • 加密API Qc3EncryptData

** 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 )

加密解密API中最重要的是根据format不同而设置的Algorithm description和Key description。本例子中使用的format是ALGD0200KEYD0200

Algorithm description和Key description是一个数据结构,用来存放算法和key的具体信息的。在rpgle中的DS如下:

** 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

本例中我们将使用AES算法来加密解密数据。那具体该怎么设置Algorithm description和Key description。下面是free格式的代码。

/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        = 'abcdefg1234567890';

/end-free

  下面是一个简单的加密解密小程序。先是把inputData(‘It is secret info’)加密,然后在解密。

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 )
**
**
d SetAlgoKeyDesFmt200...
d                 Pr
**
** 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 length          s             10I 0
d csp             s              1A   Inz('1')
d DevName         s             10A   Inz(*Blanks)
**
/free

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

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

    if ERRC0100.BytAvl = 0;
       dsply EncryptedData;
    else;
       dsply ERRC0100.MsgId;
    endif;

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

    if ERRC0100.BytAvl = 0;
       dsply DecryptedData;
    else;
       dsply ERRC0100.MsgId;
    endif;

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

**-- Subprocedure definition -------------------------------------------**
p SetAlgoKeyDesFmt200...
p                 b
/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        = 'abcdefg1234567890';

/end-free
p SetAlgoKeyDesFmt200...
p                 e

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