h nomain bnddir ('QC2LE') FCUSPI UF E DISK USROPN FCUSDTA UF A E DISK PREFIX(C) USROPN D/COPY QSYSINC/QRPGLESRC,QUSEC D/COPY QSYSINC/QRPGLESRC,QC3CCI DBILL_CUS Pr 10I 0 EXTPROC('Bill_Cus') d*create_bill pr 10i 0 extproc('create_bill') d cusdta 1 const d balance 10 2 value dcrtalgctx pr extproc('Qc3CreateAlgorithmConte+ d t') dalgd 1 const dalgFormat 8 const dAESctx 8 derrCod 1 dCrtKeyCtx PR extproc('Qc3CreateKeyContext') DKEY 1 CONST DKEYSIZE 10I 0 CONST DKEYFORMAT 1 CONST DKEYTYPE 10I 0 CONST DKEYFORM 1 CONST DKEYENCKEY 8 CONST OPTIONS(*OMIT) DKEYENCALG 8 CONST OPTIONS(*OMIT) DKEYTKN 8 DERRCOD 1 DDESTROYKEYCTX PR EXTPROC ('Qc3DestroyKeyContext') DKEYTKN 8 CONST DERRCOD 1 DDestroyAlgCtx PR EXTPROC('Qc3DestroyAlgorithmCont+ D ext') dAESTkn 8 CONST Derrcod 1 dDecryptData pr extproc('Qc3DecryptData') Dencdata 1 const D encDataSize 10i 0 const D algDesc 1 const D algDescFmt 8 const D keyDesc 1 const D keyDescFmt 8 const D csp 1 const D cspDevNam 10 const options(*omit) D clrDta 1 D clrLenPrv 10i 0 const D clrLenRtn 10i 0 D clrLenPrv 10i 0 const D clrLenRtn 10i 0 D errCod 1 DPrint pr 10i 0 extproc('printf') D charString 1 const options(*nopass) PBill_Cus b export DBill_Cus pi 10i 0 D csp s 1 inz('0') D error s 10i 0 inz(-1) D ok s 10i 0 inz(0) D rtn s 10i 0 D rtnLen s 10i 0 D plainLen s 10i 0 D cipherLen s 10i 0 D kekTkn s 8 D AESctx s 8 D KEKctx s 8 D FKctx s 8 D keySize s 10i 0 D keyType s 10i 0 D keyFormat s 1 D keyForm s 1 D inCusInfo s 80 D inCusNum s 8 0 D ECUSDTA s 80 C eval QUSBPRV = 0 * * Create an AES algorithm context for the key-encrypting key * *(KEK) C eval QC3D0200 = *loval C eval QC3BCA = 22 C eval QC3BL = 16 C eval QC3MODE = '1' C eval QC3PO = '0' C callp CrtAlgCtx( QC3D0200 :'ALGD0200' C :AESctx :QUSEC) * Create a key context for the key-encrypting key (KEK) C eval keySize = %size(QC3D040000) C eval keyFormat = '0' C eval keyType = 22 C eval keyForm = '0' C eval QC3D040000 = *loval C eval QC3KS00 = 'CUSKEYFILEMY_LIB' C eval QC3RL = 'CUSDTAKEK' C callp CrtKeyCtx( QC3D040000 :keySize :'4' C :keyType :keyForm :*omit c :*omit :kekctx :QUSEC) * Open CUSPI file C open(e) cuspi C if %error = '1' C callp Print('Open of Customer Processing - * Information File (CUSPI) failed') C return error C endif c*Read first (only) record to get encrypted file key C read(e) cuspirec C if %eof = '1' C callp Print('Customer Processing information cuspi- C record missing') C close cuspi c return error C endif C close cuspi * Create a key context for the file key C eval keySize = %size(KEY) C eval keyFormat = '0' C eval keyType = 22 C eval keyForm = '1' C callp CrtKeyCtx( KEY :keySize :keyFormat + c :keytype :keyform :KEKctx + c AESctx :FKctx :QUSEC) c*Wipe out the encrypted file key value from program storage C eval Key = *loval * Open CUSDTA C open(e) cusdta C if %error = '1' C callp Print('Open of CUSDTA file failed') c close cuspi c return error c endif * read each record of CUSDTA C READ(E) CUSDTAREC C DOW %EOF <> '1' C*If accounts receivable balance > 0, decrypt customer data and * create a bill C* if CARBAL > 0 * Decrypt customer information C eval QC3IV = CIV C eval plainLen = %size(CCUSDTA) C eval cipherLen = %size(ECUSDTA) C callp DecryptData( CCUSDTA :cipherLen C :QC3D0200 :'ALGD0200' C :FKctx :'KEYD0100' C :csp :*OMIT C :ECUSDTA :plainLen C :rtnLen :QUSEC) C* callp Create_Bill( ECUSDTA :CARBAL) C endif C read(e) cusdtarec C enddo * Cleanup C eval ecusdta = *loval C callp DestroyKeyCtx( FKctx :QUSEC) C callp DestroyKeyCtx( KEKctx :QUSEC) C callp DestroyAlgCtx( AESctx :QUSEC) C close cusdta C return ok c eval *inlr = *on P e