RPGLE RSA decrypt file 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 file and then we call the Java method by prototyping that in our RPGLE program. Click here for Java source code - RSADecryptFile.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                        
                                                                     
 * RSADecryptFile Object                                             
d DecryptFile_    s               o   Class(*JAVA:                   
d                                           'com.as400samplecode.RSA- 
d                                     DecryptFile')                  
                                                                      
* Prototype for RSADecryptFile Object                                   
d RSADecryptFile  PR              o   EXTPROC(*JAVA:                     
d                                           'com.as400samplecode.RSA-    
d                                     DecryptFile':                      
d                                            *CONSTRUCTOR)               
                                                                         
 * Prototype for RSADecryptFile decrypt Method                           
d decrypt         PR                  EXTPROC(*JAVA:                     
d                                           'com.as400samplecode.RSA-    
d                                     DecryptFile':                      
d                                            'decrypt')                  
d String                          o   Class   (*JAVA:'java.lang.String') 
d                                             Const                      
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 encryptedFile   s               o   Class(*JAVA:'java.lang.String')    
d outputFile      s               o   Class(*JAVA:'java.lang.String')    
                                                                         
d $privKeyFile    s            100a                                      
d $encryptedFile  s            100a                            
d $outputFile     s            100a                            
 *                                                             
 *----- Main Routine                                           
 *                                                             
 /free                                                         
                                                               
    Exc_Cmd('CALL PGM(BCJAVAENV)': 200);                       
                                                               
    privKeyFile   = crtString(%trim($privKeyFile));            
    encryptedFile = crtString(%trim($encryptedFile));          
    outputFile    = crtString(%trim($outputFile));             
                                                               
    DecryptFile_   = RSADecryptFile();                         
    decrypt(DecryptFile_:privKeyFile:encryptedFile:outputFile);
                                                               
    *inlr = *on;                                               
    return;                                                    
                                                               
 /end-free                                                     
 *                                                             
 *----- Initial Routine                                        
 *                                                             
c     *inzsr        begsr                       
                                                
 /free                                          
                                                
    $privKeyFile   = 'data/privateKey.txt';     
    $encryptedFile = 'data/encryptedData.txt';  
    $outputFile    = 'data/outputFile.xml';     
                                                
 /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         

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.