|
If this can help, the following sample program encrypt with public key and decrypt with private key: H DftActGrp(*No) bnddir('ENCRYPT':'QC2LE') D/Copy QSYSINC/QRPGLESRC,QUSEC D/Copy QSYSINC/QRPGLESRC,QC3CCI DQc3EncryptData pr extproc('Qc3EncryptData') D clrData 64 const D clrDataSize 10i 0 const D clrDataFmt 8 const D algDesc 1 const D algDescFmt 8 const D keyDesc 1 const D keyDescFmt 8 const D csp 1 const D cspDevNam 10A const D EncDta 64 D DtaLenPrv 10i 0 const D DtaLenRtn 10i 0 D errCod 1 DQc3DecryptData pr extproc('Qc3DecryptData') D EncDta 64 const D EncDtaLen 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 64 D DtaLenPrv 10i 0 const D DtaLenRtn 10i 0 D errCod 1 DClrDta S 64 DClrDtaLen S 10I 0 DDesDta S 64 DDesDtaLen S 10I 0 DDtaLenPrv S 10I 0 D RSA Ds qualified D KeyType 10I 0 D KeyLen 10I 0 D KeyFormat 1 D KeyRes 3 D KeyString 2000 D csp s 1 inz('0') D rtnLen s 10i 0 D cipherLen s 10i 0 // Variabili per Read() e Close() APIs D FileDescI S 10i 0 Inz(0) D bufferI S 65000a Inz(*blank) D bufferIp S * Inz(%addr(bufferI)) D nByteSet S 10u 0 Inz(%len(bufferI)) D nByteRead S 10i 0 Inz(0) D inputLng S 10I 0 D Cpybla pr ExtProc('cpybla') D Receiver * value D Source * value D Size 10i 0 value // Prototipi API e relative costanti /COPY QSOAPY,IFSCOPY /Free //******************************************************************* //* CIFRATURA CON CHIAVE PUBBLICA //******************************************************************* // Apre la chiave pubblica FileDescI = open(%trimr('/home/costagliol/myPubKey') : O_RDONLY); if FileDescI = -1; return; endif; Dou nByteRead <= 0; nByteRead = read(FileDescI : bufferIp : nByteSet); inputLng += nByteRead; Enddo; callp close(FileDescI); ClrDta = '1234567890123456'; ClrDtaLen = 16; DtaLenPrv = 256; RSA.KeyType = 50; RSA.KeyLen = inputLng; RSA.KeyFormat = '1'; RSA.KeyRes = *AllX'00'; cpybla(%addr(RSA.KeyString):bufferIp:inputLng); QC3D0400 = *loval; QC3PKA = 50; QC3PKABF = '2'; QC3ERVED00 = *allx'00'; QC3SHA = 0; Qc3EncryptData (ClrDta : // Clear data ClrDtaLen : // Length of clear data 'DATA0100': // Clear data format name QC3D0400 : // Algorithm description 'ALGD0400': // Algorithm desc format name RSA : // Key description 'KEYD0200': // Key desc format name csp : // Crypto Service Provider *BLANK : // Crypto Device Name DesDta : // Encrypted data DtaLenPrv : // Length of encrypted data DesDtaLen : // Len of enc data returned QUSEC); // Error Code If QUsei <> *Blanks; DesDta = QUsei; DesDtaLen = 0; EndIf; //******************************************************************* //* DECIFRATURA CON CHIAVE PRIVATA //******************************************************************* // Apre la chiave privata FileDescI = open(%trimr('/home/costagliol/myPriKey') : O_RDONLY); if FileDescI = -1; return; endif; Dou nByteRead <= 0; nByteRead = read(FileDescI : bufferIp : nByteSet); inputLng += nByteRead; Enddo; callp close(FileDescI); RSA.KeyType = 51; RSA.KeyLen = inputLng; RSA.KeyFormat = '1'; RSA.KeyRes = *AllX'00'; cpybla(%addr(RSA.KeyString):bufferIp:inputLng); Qc3DecryptData (DesDta : DesDtaLen : QC3D0400 : 'ALGD0400': RSA : 'KEYD0200': csp : *OMIT : ClrDta : DtaLenPrv : // Length of encrypted data ClrDtaLen : QUSEC); If QUsei <> *Blanks; ClrDta = QUsei; ClrDtaLen = 0; EndIf; *InLr = *On; /end-free ======================= The publik key can be extracted from the certificate (without the password) with this java code (runnng on as400): import java.io.*; import java.security.KeyStore; import java.security.cert.Certificate; import java.security.PublicKey; public class ExtPubKey { public static void main(String[] args) { try { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream("myKeyStore.jks"), "myPass".toCharArray()); String alias = (String)ks.aliases().nextElement(); Certificate c = ks.getCertificate(alias); PublicKey pub = c.getPublicKey(); FileOutputStream fos = new FileOutputStream("myPubKey"); byte[] encPubKey = pub.getEncoded(); fos.write(encPubKey); fos.close(); } catch (Exception de) { de.printStackTrace(); //System.err.println(de.getMessage()); } } } ==================================== The private key can be extracted from the certificate (with the password): import java.io.*; import java.security.KeyStore; import java.security.cert.Certificate; import java.security.PrivateKey; public class ExtPriKey { public static void main(String[] args) { try { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream("myKeyStore.jks"), "myPass".toCharArray()); String alias = (String)ks.aliases().nextElement(); PrivateKey priv = (PrivateKey)ks.getKey(alias, "myPass".toCharArray()); FileOutputStream fos = new FileOutputStream("myPriKey"); byte[] encPrivKey = priv.getEncoded(); fos.write(encPrivKey); fos.close(); } catch (Exception de) { de.printStackTrace(); //System.err.println(de.getMessage()); } } }
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.