ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQLRPGLE base64encode and base64decode confusion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQLRPGLE base64encode and base64decode confusion


    I am a little confused as to how to properly use the base64encode and base64decode db2 functions.

    Code:
    **free
    
    ctl-opt dftactgrp(*no) actgrp(*caller)
            option(*srcstmt:*nodebugio:*nounref)
            debug;
    
    dcl-s varchar_ebcdic_1       varchar(4096) inz;
    dcl-s varchar_ascii          varchar(4096) ccsid(*UTF8) inz;
    dcl-s varchar_ascii_encoded  varchar(4096) ccsid(*UTF8) inz;
    dcl-s basic_auth_ascii       varchar(4096) ccsid(*UTF8) inz;
    
    varchar_ebcdic_1 = 'user:pass';
    varchar_ascii = varchar_ebcdic_1;
    
    exec sql set :basic_auth_ascii = systools.base64encode(:varchar_ascii);
    exec sql set :varchar_ebcdic_1 = systools.base64decode(:basic_auth_ascii);
    
    *inlr = *on;
    return;
    The result of the decode is not the same as it was when it was encoded.

  • #2
    First ASCII is not UTF-8, so naming a UTF-8 variable "ASCII" is very confusing.

    You encode UTF-8 but try to decode the result direclty to EBCDIC.
    IMHO this cannot work. You have to decode it back into UTF-8 and then convert UTF-8 to EBCDIC.

    Birgitta
    Last edited by B.Hauser; May 30, 2020, 07:31 AM.

    Comment


    • #3
      I do think i'm a little confused because I did think UTF-8 was a specific CCSID of ascci (CCSID 1208). So I do think i was confused there (I probably need to do a little more research on that).

      I re-coded my program as follows:

      Code:
      **free                                                                         
      
      ctl-opt dftactgrp(*no) actgrp(*caller)                                         
              option(*srcstmt:*nodebugio:*nounref)                                   
              debug;                                                                 
      
      dcl-s varchar_ebcdic_1       varchar(4096) inz;                                
      dcl-s varchar_ascii          varchar(4096) ccsid(1208) inz;                    
      dcl-s varchar_ascii_encoded  varchar(4096) ccsid(1208) inz;                    
      
      varchar_ebcdic_1 = 'user:pass';                                                
      varchar_ascii = varchar_ebcdic_1;                                              
      
      exec sql set :varchar_ascii_encoded = systools.base64encode(:varchar_ascii);   
      exec sql set :varchar_ebcdic_1 =                                               
              cast( systools.base64decode(:varchar_ascii_encoded) as varchar(4096) );
      
      *inlr = *on;                                                                   
      return;
      The value of varchar_ebcdic_1 is not 'user@#%@#%@#%'. I figured the cast( as varchar(4096)) would work but it did not. How do you get the binary data from base64decode() back into ebcdic?

      Comment


      • TheZenbudda
        TheZenbudda commented
        Editing a comment
        I cannot edit my posts. The was supposed to say user : pass

    • #4
      I think what Birgitta was suggesting is just to reverse the process. You took 2 steps to encode the data so follow those steps in reverse.
      Code:
      varchar_ebcdic_1 = 'user : pass';
      
      varchar_ascii = varchar_ebcdic_1;
      exec sql set :basic_auth_ascii = systools.base64encode(:varchar_ascii);
      
      exec sql set :varchar_ascii = systools.base64decode(:basic_auth_ascii);
      varchar_ebcdic_1 = varchar_ascii;

      Comment


      • Brian Rusch
        Brian Rusch commented
        Editing a comment
        The variable basic_auth_ascii was from the code in your first example. Replace it with varchar_ascii_encoded for the second example.

    • #5
      Thanks for your responses Brian.


      Your suggestion worked. I thought the cast () around the base64decode() would work, but it did not. Instead the RPG eval worked. All of this is leading to me building my own JWT generator (as well as learning how the base64xxxx functions work, on top of ccsid translations). I know that basic auth for restful services coverts the userid and password into userassword base64encoded, so I wanted to start there as a baseline to ensure my encoding/decoding logic works properly.

      Code:
      **free                                                                           
      
      ctl-opt dftactgrp(*no) actgrp(*caller)                                           
              option(*srcstmt:*nodebugio:*nounref)                                     
              debug;                                                                   
      
      dcl-s varchar_ebcdic_1       varchar(4096) inz;                                  
      dcl-s varchar_ascii          varchar(4096) ccsid(1208) inz;                      
      dcl-s varchar_ascii_encoded  varchar(4096) ccsid(1208) inz;                      
      
      varchar_ebcdic_1 = 'user:pass';                                                  
      varchar_ascii = varchar_ebcdic_1;                                                
      
      exec sql set :varchar_ascii_encoded = systools.base64encode(:varchar_ascii);     
      exec sql set :varchar_ascii =                                                    
              cast( systools.base64decode(:varchar_ascii_encoded) as varchar(4096) );  
      varchar_ebcdic_1 = varchar_ascii;                                                
      
      *inlr = *on;
      return;

      Comment


      • #6
        Originally posted by Brian Rusch View Post
        I think what Birgitta was suggesting is just to reverse the process. You took 2 steps to encode the data so follow those steps in reverse.
        Code:
        varchar_ebcdic_1 = 'user : pass';
        
        varchar_ascii = varchar_ebcdic_1;
        exec sql set :basic_auth_ascii = systools.base64encode(:varchar_ascii);
        
        exec sql set :varchar_ascii = systools.base64decode(:basic_auth_ascii);
        varchar_ebcdic_1 = varchar_ascii;
        I apologize, but my code i posted earlier falsely lead me to believe it worked. It did not work. The last sql statement failed and since it failed, the value of varchar_ascii stayed the same. Here is the error message:

        Character conversion between CCSID 65535 and CCSID 1208 not valid.

        SQL0332

        Comment


        • #7
          Here's an article that might be helpful. Be sure to read the comments as they talk about the issue you're having: https://github.com/worksofliam/blog/issues/6.

          Comment

          Working...
          X