Blog Archive

RPGLE RSA encrypt string with public key using bouncy castle Java Crypto APIs

All we do here is create a Java program that will help us encrypt the String data and then wrap the Java method using RPGLE prototype definition. Click here for Java Source code - RSAEncryption.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          2000A   EXTPROC(*JAVA:                
d                                             'java.lang.String':   
d                                             'getBytes')           
d                                     Varying                       
                                                                    
 * RSAEncryption Object                                             
d RSAEncryption_  s               o   Class(*JAVA:                  
d                                           'com.as400samplecode.RSA-
d                                     Encryption')                  
                                                                     
* Prototype for RSAEncryption Object                                   
d RSAEncryption   PR              o   EXTPROC(*JAVA:                    
d                                           'com.as400samplecode.RSA-   
d                                     Encryption':                      
d                                            *CONSTRUCTOR)              
                                                                        
 * Prototype for RSAEncryption encrypt Method                           
d encrypt         PR              o   EXTPROC(*JAVA:                    
d                                           'com.as400samplecode.RSA-   
d                                     Encryption':                      
d                                            'encrypt')                 
d                                     Class   (*JAVA:'java.lang.String')
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 inputData       s               o   Class(*JAVA:'java.lang.String')   
d encryptedData   s               o   Class(*JAVA:'java.lang.String')   
                                                                        
d $pubKeyFile     s            100a                                     
d $inputData      s            100a                                     
d $encryptedData  s           2000a                               
 *                                                                
 *----- Main Routine                                              
 *                                                                
 /free                                                            
                                                                  
    Exc_Cmd('CALL PGM(BCJAVAENV)': 200);                          
                                                                  
    pubKeyFile  = crtString(%trim($pubKeyFile));                  
    inputData   = crtString(%trim($inputData));                   
                                                                  
    RSAEncryption_ = RSAEncryption();                             
    encryptedData  = encrypt(RSAEncryption_:pubKeyFile:inputData);
                                                                  
    $encryptedData = cvtToBytes(encryptedData);                   
                                                                  
    *inlr = *on;                                                  
    return;                                                       
                                                                  
 /end-free                                                        
 *                                                                
 *----- Initial Routine                                           
 *                                                                
c     *inzsr        begsr                                                 
                                                                          
c                   eval      $pubKeyFile  = 'data/publicKey.txt'         
c                   eval      $inputData                                  
c                                  = 'Encrypt my website as400samplecode' 
                                                                          
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