Qc3EncryptData – encrypting data using system API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | H BNDDIR('QC2LE') H DFTACTGRP(*NO) H OPTION(*NODEBUGIO:*SRCSTMT) *------------------------------------------------ ** E N T R Y P A R M S ** *------------------------------------------------ d encrypt pr d inmode 1a d invalue 30a d inkey 30a d outvalue 30a d encrypt pi d inmode 1a d invalue 30a d inkey 30a d outvalue 30a d workmode s 1a d workinvalue s 30a d workinkey s 30a d workoutvalue s 30a *------------------------------------------------ ** E N C R Y P T D A T A ** *------------------------------------------------ D Qc3EncryptData PR ExtProc('Qc3EncryptData') D szClearData 65535A OPTIONS(*VARSIZE) D nLenClearData 10I 0 Const D clearDataFmt 8A Const D AlgoDescript 64A Const OPTIONS(*VARSIZE) D szAlgoFormat 8A Const D KeyDescriptor 512A Const OPTIONS(*VARSIZE) D szKeyFormat 8A Const ** 0=Best choice, 1=Software, 2=Hardware D CryptoService 1A Const ** Hardware Cryptography device name or *BLANKS D CryptoDevName 10A Const D szEncryptedData... D 65535A OPTIONS(*VARSIZE) D nEncryptedDataVarLen... D 10I 0 Const D nEncryptedDataRtnLen... D 10I 0 D api_ErrorDS LikeDS(API_ErrorDS_T) D OPTIONS(*VARSIZE) *------------------------------------------------ ** D E C R Y P T D A T A ** *------------------------------------------------ D Qc3DecryptData PR ExtProc('Qc3DecryptData') D szEncData 65535A OPTIONS(*VARSIZE) D nLenEncData 10I 0 Const D AlgoDescript 64A Const OPTIONS(*VARSIZE) D szAlgoFormat 8A Const D KeyDescriptor 512A Const OPTIONS(*VARSIZE) D szKeyFormat 8A Const ** 0=Best choice, 1=Software, 2=Hardware D CryptoService 1A Const ** Hardware Cryptography device name or *BLANKS D CryptoDevName 10A Const D szClearData 65535A OPTIONS(*VARSIZE) D nClearVarLen 10I 0 Const D nRtnClearLen 10I 0 D api_ErrorDS LikeDS(API_ErrorDS_T) D OPTIONS(*VARSIZE) *------------------------------------------------ ** Message Digest/Hash *------------------------------------------------ D Qc3CalcHash PR ExtProc('Qc3CalculateHash') D szClearData 65535A OPTIONS(*VARSIZE) D nLenClearData 10I 0 Const D clearDataFmt 8A Const D AlgoDescr 64A Const OPTIONS(*VARSIZE) D szAlgoFormat 8A Const ** 0=Best choice, 1=Software, 2=Hardware D CryptoService 1A Const ** Hardware Cryptography device name or *BLANKS D CryptoDevName 10A Const D rtnHash 64A OPTIONS(*VARSIZE) D api_ErrorDS LikeDS(API_ErrorDS_T) D OPTIONS(*VARSIZE) *------------------------------------------------ ** Cryptography API Algorithm ALGD0300 Structure *------------------------------------------------ D ALGD0300_T DS Qualified D BASED(DS_TEMPL) ** Stream algorithm: 30 = RC4 D Algorithm 10I 0 D Qc3CreateAlgorithmContext... D PR ExtProc('Qc3CreateAlgorithmContext') D AlgoDescription... D 64A Const OPTIONS(*VARSIZE) D szAlgoFormat 8A Const D contextToken 8A D api_ErrorDS LikeDS(API_ErrorDS_T) D OPTIONS(*VARSIZE) ** Encryption Data Structures D KEYD0100_T DS Qualified D BASED(DS_TEMPL) D keyContext 8A D KEYD0200_T DS Qualified D BASED(DS_TEMPL) D type 10I 0 D length 10I 0 D format 1A D value 256A /IF DEFINED(*V5R1M0) D API_ErrorDS_T DS Qualified D dsLen 10I 0 Inz D rtnLen 10I 0 Inz D cpfMsgID 7A D apiResv1 1A Inz(X'00') D apiExcDta1 64A /ENDIF ** New IBM API Error DS D XT_api_ErrorEx DS Inz D XT_apiKey 10I 0 D XT_apiDSLen 10I 0 D XT_apiRtnLenEx... D 10I 0 D XT_apiMsgIDEx 7A D XT_apiResvdEx 1A D XT_apiCCSID 10I 0 D XT_apiOffExc 10I 0 D XT_apiExcLen 10I 0 D XT_apiExcData 64A D Qc3DestroyAlgorithmContext... D PR ExtProc('Qc3DestroyAlgorithmContext') D ContextToken 8A Const D api_ErrorDS LikeDS(API_ErrorDS_T) D OPTIONS(*VARSIZE) ** API Error Data structure D QUSEC_EX DS Qualified D Based(TEMPLATE_T) D charKey 10I 0 D nErrorDSLen 10I 0 D nRtnLen 10I 0 D msgid 7A D Reserved 1A D CCSID 10I 0 D OffsetExcp 10I 0 D excpLen 10I 0 D excpData 128A D ALGO_DES C Const(20) D ALGO_TDES C Const(21) D ALGO_AES C Const(22) D ALGO_RC4 C Const(30) D ALGO_RSA_PUB C Const(50) D ALGO_RSA_PRIV C Const(51) D ANY_CRYPTO_SRV C Const('0') D SWF_CRYPTO_SRV C Const('1') D HWD_CRYPTO_SRV C Const('2') D CRYPTO_SRV S 10A Inz(*BLANKS) ** Cipher API data structures. D myAlgo DS LikeDS(ALGD0300_T) D myKey DS LikeDS(KEYD0200_T) D apiError DS LikeDS(qusec_ex) ** The clear text (data to be encrypted) ** The length of the data returned by the APIs D nRtnLen S 10I 0 ** The encrypted data variable D encData S 500A /free workinvalue = invalue; workinkey = inkey; workoutvalue = outvalue; myAlgo.Algorithm = ALGO_RC4; myKey.type = ALGO_RC4; myKey.length = %Len(%TrimR(workinkey)); myKey.Format = '0'; myKey.value = %TrimR(workinkey); apiError = *ALLX'00'; apiError.nErrorDSLen=%size(apiError); select; when inmode = 'E'; Qc3EncryptData(workinvalue:%len(%TrimR(workinvalue)):'DATA0100': myAlgo : 'ALGD0300' : myKey : 'KEYD0200' : ANY_CRYPTO_SRV : CRYPTO_SRV : encData : %size(encData) : nRtnLen : apiError ); outvalue = encData; apiError = *ALLX'00'; apiError.nErrorDSLen=%size(apiError); when inmode = 'D'; Qc3DecryptData(workinvalue : %len(%TrimR(workinvalue)) : myAlgo : 'ALGD0300' : myKey : 'KEYD0200' : ANY_CRYPTO_SRV : CRYPTO_SRV : outvalue : %size(outvalue) : nRtnLen : apiError ); endsl; *inlr = *on; /end-free |