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                                                   
    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;                                                       
 *----- Initial Routine                                                
c     *inzsr        begsr                                              
    $privKeyFile  = 'data/privateKey.txt';                             
    $encryptedData =                                                   
      '60cc8b36353da40c51b8ba0102966d92bd63c057fa3f5f47db912fe0be70' + 
      '52f66ce6c2083d3c01a370481dc8abe6bf51432733247257c927f6e412d1' + 
      'e2f7a462811e03fe1953f1d056067147e05b31183bd16b3d60ef5608d175' + 
      '752837bfc47a5c0ff6390a41e4b24dfdcefaee38d17f8480220cc01257a0' + 
c                   endsr                                              

Setup my Java home and classpath: PGM(BCJAVAENV)

     RMVENVVAR  ENVVAR(JAVA_HOME)                             
     MONMSG     MSGID(CPF0000)                                
     RMVENVVAR  ENVVAR(CLASSPATH)                             
     MONMSG     MSGID(CPF0000)                                
     CD         DIR('/directory/EncryptionDecryption')        
     ADDENVVAR  ENVVAR(JAVA_HOME) +                           
     ADDENVVAR  ENVVAR(CLASSPATH) +                           

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.