Blog Archive

RPGLE generate Public and Private Key Pairs using bouncy castle Java Crypto APIs

In this example all we do is call the Java methods that help us generate the RSA key pairs.
Click here for the Java Source code - GenerateRSAKeys.java
h DftActgrp(*NO) ActGrp(*Caller)                                         
                                                                         
D Exc_Cmd         PR                  extpgm('QCMDEXC')                  
D  command                     200A   const                              
D  length                       15P 5 const                              
                                                                         
 * Prototype for Java String Object                                      
d crtString       PR              o   EXTPROC(*JAVA:                     
d                                             'java.lang.String':        
d                                             *CONSTRUCTOR)              
d RPGBytes                     100A   Const Varying                      
                                                                         
 * Prototype for Java String's getBytes method                           
d cvtToBytes      PR            30A   EXTPROC(*JAVA:                     
d                                             'java.lang.String':        
d                                             'getBytes')                
d                                     Varying                            
                                                                         
 * GenerateRSAKeys Object                                                
d GenRSAkeys_     s               o   Class(*JAVA:                       
d                                           'com.as400samplecode.Generate-
d                                     RSAKeys')                          
                                                                          
* Prototype for GenerateRSAKeys Object                                   
d GenRSAkeys      PR              o   EXTPROC(*JAVA:                      
d                                           'com.as400samplecode.Generate-
d                                     RSAKeys':                           
d                                            *CONSTRUCTOR)                
                                                                          
 * Prototype for GenerateRSAKeys generate Method                          
d generate        PR                  EXTPROC(*JAVA:                      
d                                           'com.as400samplecode.Generate-
d                                     RSAKeys':                           
d                                            'generate')                  
d String                          o   Class   (*JAVA:'java.lang.String')  
d                                             Const                       
d String                          o   Class   (*JAVA:'java.lang.String')  
d                                             Const                       
                                                                          
d pubKeyFile      s               o   Class(*JAVA:'java.lang.String')     
d privKeyFile     s               o   Class(*JAVA:'java.lang.String')     
                                                                          
d $pubKeyFile     s            100a                                       
d $privKeyFile    s            100a                                       
 *                                                                        
 *----- Main Routine                                                      
*                                                                
 /free                                                            
                                                                  
    Exc_Cmd('CALL PGM(BCJAVAENV)': 200);                          
                                                                  
    pubKeyFile  = crtString(%trim($pubKeyFile));                  
    privKeyFile = crtString(%trim($privKeyFile));                 
                                                                  
    GenRSAkeys_ = GenRSAkeys();                                   
    generate(GenRSAkeys_:pubKeyFile:privKeyFile);                 
                                                                  
    *inlr = *on;                                                  
    return;                                                       
                                                                  
 /end-free                                                        
 *                                                                
 *----- Initial Routine                                           
 *                                                                
c     *inzsr        begsr                                         
                                                                  
c                   eval      $pubKeyFile  = 'data/publicKey.txt' 
c                   eval      $privKeyFile = 'data/privateKey.txt'
                         
c                   endsr

Setup my Java home and classpath: PGM(BCJAVAENV)

     PGM                                                      
                                                              
     RMVENVVAR  ENVVAR(JAVA_HOME)                             
     MONMSG     MSGID(CPF0000)                                
     RMVENVVAR  ENVVAR(CLASSPATH)                             
     MONMSG     MSGID(CPF0000)                                
                                                              
     CD         DIR('/directory/EncryptionDecryption')        
                                                              
     ADDENVVAR  ENVVAR(JAVA_HOME) +                           
                  VALUE('/qibm/ProdData/Java400/jdk15')       
                                                              
     ADDENVVAR  ENVVAR(CLASSPATH) +                           
                  VALUE('.:/QIBM/ProdData/HTTP/Public/jt400/l+
                  ib/jt400.zip:/QIBM/ProdData/HTTP/Public/jt4+
                  00/utilities:/directory/EncryptionDecryptio+
                  n:/das/EncryptionDecryption/lib/bcprov-jdk1+
                  5-146.jar:/directory/EncryptionDecryption/l+
                  ib/log4j-1.2.16.jar')                       
                                                              
     ENDPGM