|
Hello Everyone,Apologies for the lenghty email... Good afternoon, I am trying to encrypt data with RSA with public/private key provided by third party... I am using Qc3EncryptData and Qc3DecryptData APIs to do the encryption and decryption. The below code works fine for the for the public/private key created by cryptograhic service provider on the AS400 but not for the ones that are created by third party. I have all kinds of methods but no success... (I wonder why IBM makes it so difficult to use this APIs)...
Just as note: My counterparts in Java world in the my company had same issue with third party keys... They solved it by doing (In java ofcource)
c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); Instead of c = Cipher.getInstance("RSA", "BC"); Here is the code on the AS400.... H DEBUG OPTION(*SRCSTMT:*NODEBUGIO)‚*===================================================================== ‚*=====================================================================
‚* ‚* PROGRAM: TESTPGM ‚* ‚* DESCRIPTION: Test Program ‚* ‚* CRTRPGMOD TESTPGM ‚* CRTPGM PGM(TESTPGM) BNDSRVPGM(QC3DTAEN QC3DTADE QC3PRNG) ‚*‚*=====================================================================
‚* MODIFICATION LOG:‚*=====================================================================
‚* ‚* ChangeId Who Date Reason‚* -------- ------ -------- ------------------------------------------ ‚*===================================================================== ‚*=====================================================================
‚* program file specifications‚*=====================================================================
‚* FKEYFILEP IF E K DISK ‚*‚*******************************************************************
‚*Data definitions‚*******************************************************************
‚*ALGD0400 algorithm description structure DQC3D0400 DS ‚* Qc3 Format ALGD0400 D QC3PKA 1 4B 0 ‚* Public Key Alg D QC3PKABF 5 5 ‚* PKA Block Format D QC3ERVED00 6 8 INZ(x'000000') ‚* Reserved D QC3SHA 9 12B 0 ‚* Signing Hash Alg ‚*KEYD0200 key description format structure DQC3D020000 DS ‚* Qc3 Format KEYD0200 D QC3KT 1 4B 0 ‚* Key Type D QC3KSL 5 8B 0 ‚* Key String Len D QC3KF 9 9 ‚* Key Format D QC3ERVED02 10 12 inz(x'000000') ‚* Reserved ‚*QC3KS 13 13 ‚* ‚* variable length‚*******************************************************************
‚* API error structure D APIERR DS D ERRPRV 10I 0 INZ(272) D ERRLEN 10I 0 D EXCPID 7A D RSRVD2 1A D EXCPDT 256A D ‚* ‚*Encrypt Data ‚* D Qc3EncryptData Pr ExtProc('Qc3EncryptData') D clrDta 32767a D clrDtaLen 10I 0 D clrDtaFmt 8 D algorithm like(QC3D0400) D algorithmFmt 8 D key like(KeyC) D keyFmt 8 D srvProvider 1 D deviceName 10 D encryptedData 32767a D encryptedBufL 10I 0 D encryptedRtnL 10I 0 D errcde like(APIERR) ‚* ‚* Decrypt Data ‚* D Qc3DecryptData Pr ExtProc('Qc3DecryptData') D encryptedData 32767a D encryptedDtaL 10I 0 D algorithm like(QC3D0400) D algorithmFmt 8 D key like(keyC) D keyFmt 8 D srvProvider 1 D deviceName 10 D clrDta 32767a D clrDtaBufL 10I 0 D clrDtaRtnL 10I 0 D errcde like(APIERR) D ClrDtaRtn S 32767a D clrDta S 32767a D clrDtaLen S 10I 0 D clrDtaFmt S 8 inz('DATA0100') D algorithm S like(QC3D0400) D algorithmFmt S 8 inz('ALGD0400') D key S like(KeyC) D keyFmt S 8 inz('KEYD0200') D srvProvider S 1 D deviceName S 10 D encryptedData S 32767a D encryptedDtaL S 10I 0 D encryptedBufL S 10I 0 D encryptedRtnL S 10I 0 D clrDtaBufL S 10I 0 D clrDtaRtnL S 10I 0 D ReturnString S 10000 D KeyC S 1504 D InputStrinG C 'My Test String ABCDEF' D Length S 5 0‚*=====================================================================
‚* Main Line‚*=====================================================================
C Read KeyFileP C Eval Algorithm = QC3D0400 C Exsr $Encryption C Exsr $Decryption C Seton LR C Return‚*=====================================================================
‚* $Encryption‚*=====================================================================
C $Encryption BegSR C Eval QC3PKA = 50 C Eval QC3PKABF = '0' C Eval QC3SHA = X'00000000' C Eval algorithm = QC3D0400 ‚* Set up the key now. ‚* =========================== ‚* Key Type C Eval QC3KT = 50 ‚* Key Length C Eval QC3KSL = KyLen ‚* Key Format C Eval QC3KF = '1' ‚* Key String C Eval KeyC = QC3D020000 + %Subst(Key1:1:KyLen) C Eval Key = KeyC C Eval clrDta = InputString C Eval clrDtaLen = %len(%trim(clrDta)) C Eval encryptedBufL = %size(encryptedData) C Reset EncryptedData C CallP Qc3EncryptData( C clrDta : C clrDtaLen : C clrDtaFmt : C algorithm : C algorithmFmt : C key : C keyFmt : C srvProvider : C deviceName : C encryptedData : C encryptedBufL : C encryptedRtnL : C APIERR C ) C ExSr ChkErrCde C Eval ReturnString = %Subst(EncryptedData:1: C EncryptedRtnL) C EndSR‚*=====================================================================
‚* $Decryption‚*=====================================================================
C $Decryption BegSR C Eval QC3PKA = 50 C Eval QC3PKABF = '0' C Eval QC3SHA = X'00000000' C Eval algorithm = QC3D0400 ‚* Set up the key now. ‚* =========================== ‚* Key Type C Eval QC3KT = 51 ‚* Key Length C Eval QC3KSL = KyLen2 ‚* Key Format C Eval QC3KF = '1' ‚* Key String C Eval KeyC = QC3D020000 + %Subst(Key2:1:KyLen2) C Eval Key = KeyC C Clear clrDta C Eval clrDtaBufL = %size(clrDta) C Eval encryptedDtaL = encryptedRtnL C CallP Qc3DecryptData( C encryptedData : C encryptedDtaL : C algorithm : C algorithmFmt : C key : C keyFmt : C srvProvider : C deviceName : C ClrDta : C clrDtaBufL : C clrDtaRtnL : C APIERR C ) C ExSr ChkErrCde C Eval ReturnString= %Subst(ClrDta:1:clrDtaRtnL) C EndSR‚*=====================================================================
‚* *INZSR‚*=====================================================================
C *INZSR BegSr C Eval SrvProvider = '1' C Eval DeviceName = *BLANKS C EndSr‚*=====================================================================
‚* ChkErrCde‚*=====================================================================
C ChkErrCde BegSr C If ERRLEN > 0 C EXCPID DSPLY C Dump C EndIf C EndSr‚*=====================================================================
VENKAT REDDY
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.