Blog Archive

RPGLE RSA decrypt string with private key using bouncy castle Java Crypto APIs

In this example we first created a Java program that takes care of all the heavy lifting of decrypting the String data and then we call the Java method by prototyping that in our RPGLE program. Click here for Java source code - RSADecryption.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                    2000A   Const Varying                  
                                                                     
 * Prototype for Java String's getBytes method                       
d cvtToBytes      PR          2000A   EXTPROC(*JAVA:                 
d                                             'java.lang.String':    
d                                             'getBytes')            
d                                     Varying                        
                                                                     
 * RSADecryption Object                                              
d RSADecryption_  s               o   Class(*JAVA:                   
d                                           'com.as400samplecode.RSA- 
d                                     Decryption')                   
                                                                      
* Prototype for RSADecryption Object                                    
d RSADecryption   PR              o   EXTPROC(*JAVA:                     
d                                           'com.as400samplecode.RSA-    
d                                     Decryption':                       
d                                            *CONSTRUCTOR)               
                                                                         
 * Prototype for RSADecryption decrypt Method                            
d decrypt         PR              o   EXTPROC(*JAVA:                     
d                                           'com.as400samplecode.RSA-    
d                                     Decryption':                       
d                                            'decrypt')                  
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 privKeyFile     s               o   Class(*JAVA:'java.lang.String')    
d encryptedData   s               o   Class(*JAVA:'java.lang.String')    
d outputData      s               o   Class(*JAVA:'java.lang.String')    
                                                                         
d $privKeyFile    s            100a                                      
d $encryptedData  s           2000a                                      
d $outputData     s            100a                                    
 *                                                                     
 *----- Main Routine                                                   
 *                                                                     
 /free                                                                 
                                                                       
    Exc_Cmd('CALL PGM(BCJAVAENV)': 200);                               
                                                                       
    privKeyFile   = crtString(%trim($privKeyFile));                    
    encryptedData = crtString(%trim($encryptedData));                  
                                                                       
    RSADecryption_ = RSADecryption();                                  
    outputData     = decrypt(RSADecryption_:privKeyFile:encryptedData);
                                                                       
    $outputData = cvtToBytes(outputData);                              
                                                                       
    *inlr = *on;                                                       
    return;                                                            
                                                                       
 /end-free                                                             
 *                                                                     
 *----- Initial Routine                                                
 *                                                                     
c     *inzsr        begsr                                              
                                                                       
 /free                                                                 
                                                                       
    $privKeyFile  = 'data/privateKey.txt';                             
    $encryptedData =                                                   
      '60cc8b36353da40c51b8ba0102966d92bd63c057fa3f5f47db912fe0be70' + 
      '52f66ce6c2083d3c01a370481dc8abe6bf51432733247257c927f6e412d1' + 
      'e2f7a462811e03fe1953f1d056067147e05b31183bd16b3d60ef5608d175' + 
      '752837bfc47a5c0ff6390a41e4b24dfdcefaee38d17f8480220cc01257a0' + 
      'b0035d0dcf307afc';                                              
                                                                       
 /end-free                                                             
                                                                       
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