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

First we create the Java program that will help us encrypt the file and then we call the method from our RPGLE program.
Click here for the Java Source code - RSAEncryptFile.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                        
                                                                     
 * RSAEncryptFile Object                                             
d EncryptFile_    s               o   Class(*JAVA:                   
d                                           'com.as400samplecode.RSA- 
d                                     EncryptFile')                  
                                                                      
* Prototype for RSAEncryptFile Object                                  
d RSAEncryptFile  PR              o   EXTPROC(*JAVA:                    
d                                           'com.as400samplecode.RSA-   
d                                     EncryptFile':                     
d                                            *CONSTRUCTOR)              
                                                                        
 * Prototype for RSAEncryptFile encrypt Method                          
d encrypt         PR                  EXTPROC(*JAVA:                    
d                                           'com.as400samplecode.RSA-   
d                                     EncryptFile':                     
d                                            'encrypt')                 
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 pubKeyFile      s               o   Class(*JAVA:'java.lang.String')   
d inputFile       s               o   Class(*JAVA:'java.lang.String')   
d encryptedFile   s               o   Class(*JAVA:'java.lang.String')   
                                                                        
d $pubKeyFile     s            100a                                     
d $inputFile      s            100a                            
d $encryptedFile  s            100a                            
 *                                                             
 *----- Main Routine                                           
 *                                                             
 /free                                                         
                                                               
    Exc_Cmd('CALL PGM(BCJAVAENV)': 200);                       
                                                               
    pubKeyFile    = crtString(%trim($pubKeyFile));             
    inputFile     = crtString(%trim($inputFile));              
    encryptedFile = crtString(%trim($encryptedFile));          
                                                               
    EncryptFile_   = RSAEncryptFile();                         
    encrypt(EncryptFile_:pubKeyFile:inputFile:encryptedFile);  
                                                               
    *inlr = *on;                                               
    return;                                                    
                                                               
 /end-free                                                     
 *                                                             
 *----- Initial Routine                                        
 *                                                             
c     *inzsr        begsr                                                
                                                                         
c                   eval      $pubKeyFile    = 'data/publicKey.txt'      
c                   eval      $inputFile     = 'data/inputData.xml'      
c                   eval      $encryptedFile = 'data/encryptedData.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      

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.