ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Encrypt AES 256 using Qc3EncryptData

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

  • Encrypt AES 256 using Qc3EncryptData

    Hi,

    I want to ask help and advice regarding how to do Encrypt AES 256 using Qc3EncryptData correctly.
    I have tried but the result still not same with online utility at https://www.devglan.com/online-tools...ion-decryption
    I already try to translate the string and password to ASCII but still not same result.
    I also try not using pad char, but still not same result.


    Code:
    H BNDDIR('QC2LE')                                                                              
    H DFTACTGRP(*NO)                                                                              
    H OPTION(*NODEBUGIO:*SRCSTMT)                                                                  
     *------------------------------------------------                                            
     ** E N T R Y P A R M S **                                                                    
     *------------------------------------------------                                            
    
    d XAES600R        pr                                                                          
    d inmode                         1a                                                            
    d invalue                       32a                                                            
    d inkey                         32a                                                            
    d inXLTASC                       1a                                                            
    d outvalue                      32a                                                            
    
    d XAES600R        pi                                                                          
    d inmode                         1a                                                            
    d invalue                       32a                                                            
    d inkey                         32a                                                            
    d inXLTASC                       1a                                                            
    d outvalue                      32a                                                            
    
    d workmode        s              1a                                                            
    d workinvalue     s             32a                                                            
    d workinkey       s             32a                                                            
    d workoutvalue    s             32a                                                            
    d bufDta          s           1024                                                            
    d bufLen          s              5  0                                                          
    d xlttblA         s             10    INZ('QASCII')                                            
    d xlttblE         s             10    INZ('QEBCDIC')                                          
    
     *------------------------------------------------                                            
     ** E N C R Y P T D A T A **                                                                  
     *------------------------------------------------                                            
    D Qc3EncryptData  PR                  ExtProc('Qc3EncryptData')                                
    D szClearData                65535A   OPTIONS(*VARSIZE)                                        
    D nLenClearData                 10I 0 Const                                                    
    D clearDataFmt                   8A   Const                                                    
    
    D AlgoDescript                  64A   Const OPTIONS(*VARSIZE)                                  
    D szAlgoFormat                   8A   Const                                                    
    
    D KeyDescriptor                512A   Const OPTIONS(*VARSIZE)                                  
    D szKeyFormat                    8A   Const                                                    
    
    ** 0=Best choice, 1=Software, 2=Hardware                                                      
    D CryptoService                  1A   Const                                                    
    ** Hardware Cryptography device name or *BLANKS                                                
    D CryptoDevName                 10A   Const                                                    
    
    D szEncryptedData...                                                                          
    D                            65535A   OPTIONS(*VARSIZE)                                        
    D nEncryptedDataVarLen...                                                                      
    D                               10I 0 Const                                                    
    D nEncryptedDataRtnLen...                                                                      
    D                               10I 0                                                          
    D api_ErrorDS                         LikeDS(API_ErrorDS_T)                                    
    D                                     OPTIONS(*VARSIZE)                                        
    
     *------------------------------------------------                                            
     ** D E C R Y P T D A T A **                                                                  
     *------------------------------------------------                                            
    D Qc3DecryptData  PR                  ExtProc('Qc3DecryptData')                                
    D szEncData                  65535A   OPTIONS(*VARSIZE)                                        
    D nLenEncData                   10I 0 Const                                                    
    
    D AlgoDescript                  64A   Const OPTIONS(*VARSIZE)                                  
    D szAlgoFormat                   8A   Const                                                    
    
    D KeyDescriptor                512A   Const OPTIONS(*VARSIZE)                                  
    D szKeyFormat                    8A   Const                                                    
    
    ** 0=Best choice, 1=Software, 2=Hardware                                                      
    D CryptoService                  1A   Const                                                    
    ** Hardware Cryptography device name or *BLANKS                                                
    D CryptoDevName                 10A   Const                                                    
    
    D szClearData                65535A   OPTIONS(*VARSIZE)                                        
    D nClearVarLen                  10I 0 Const                                                    
    D nRtnClearLen                  10I 0                                                          
    D api_ErrorDS                         LikeDS(API_ErrorDS_T)                                    
    D                                     OPTIONS(*VARSIZE)                                        
    
     *------------------------------------------------                                            
     ** Message Digest/Hash                                                                        
     *------------------------------------------------                                            
    D Qc3CalcHash     PR                  ExtProc('Qc3CalculateHash')                              
    D szClearData                65535A   OPTIONS(*VARSIZE)                                        
    D nLenClearData                 10I 0 Const                                                    
    D clearDataFmt                   8A   Const                                                    
    
    D AlgoDescr                     64A   Const OPTIONS(*VARSIZE)                                  
    D szAlgoFormat                   8A   Const                                                    
    
    ** 0=Best choice, 1=Software, 2=Hardware                                                      
    D CryptoService                  1A   Const                                                    
    ** Hardware Cryptography device name or *BLANKS                                                
    D CryptoDevName                 10A   Const                                                    
    
    D rtnHash                       64A   OPTIONS(*VARSIZE)                                        
    D api_ErrorDS                         LikeDS(API_ErrorDS_T)                                    
    D                                     OPTIONS(*VARSIZE)                                        
     *------------------------------------------------                                            
     ** Cryptography API Algorithm ALGD0200 Structure                                              
     *------------------------------------------------                                            
    D ALGD0200_T      DS                  Qualified                                                
    D                                     BASED(DS_TEMPL)                                          
    D Algorithm                     10I 0                                                          
    D blockLen                      10I 0                                                          
    D mode                           1A                                                            
    D padOpt                         1A                                                            
    D padChar                        1A                                                            
    D reserved                       1A                                                            
    D macLen                        10I 0                                                          
    D effKeySiz                     10I 0                                                          
    D intVec                        16A                                                            
    D*intVec                        32A                                                            
    
    D Qc3CreateAlgorithmContext...                                                                
    D                 PR                  ExtProc('Qc3CreateAlgorithmContext')                    
    D AlgoDescription...                                                                          
    D                               64A   Const OPTIONS(*VARSIZE)                                  
    D szAlgoFormat                   8A   Const                                                    
    D contextToken                   8A                                                            
    D api_ErrorDS                         LikeDS(API_ErrorDS_T)                                    
    D                                     OPTIONS(*VARSIZE)                                        
    ** Encryption Data Structures                                                                  
    D KEYD0100_T      DS                  Qualified                                                
    D                                     BASED(DS_TEMPL)                                          
    D keyContext                     8A                                                            
    
    D KEYD0200_T      DS                  Qualified                                                
    D                                     BASED(DS_TEMPL)                                          
    D type                          10I 0                                                          
    D length                        10I 0                                                          
    D format                         1A                                                            
    D value                        256A                                                            
    
     /IF DEFINED(*V5R1M0)                                                                          
    D API_ErrorDS_T   DS                  Qualified                                                
    D dsLen                         10I 0 Inz                                                      
    D rtnLen                        10I 0 Inz                                                      
    D cpfMsgID                       7A                                                            
    D apiResv1                       1A   Inz(X'00')                                              
    D apiExcDta1                    64A                                                            
     /ENDIF                                                                                        
    
    ** New IBM API Error DS                                                                        
    D XT_api_ErrorEx  DS                  Inz                                                      
    D XT_apiKey                     10I 0                                                          
    D XT_apiDSLen                   10I 0                                                          
    D XT_apiRtnLenEx...                                                                            
    D                               10I 0                                                          
    D XT_apiMsgIDEx                  7A                                                            
    D XT_apiResvdEx                  1A                                                            
    D XT_apiCCSID                   10I 0                                                          
    D XT_apiOffExc                  10I 0                                                          
    D XT_apiExcLen                  10I 0                                                          
    D XT_apiExcData                 64A                                                            
    
    D Qc3DestroyAlgorithmContext...                                                                
    D                 PR                  ExtProc('Qc3DestroyAlgorithmContext')                    
    D ContextToken                   8A   Const                                                    
    D api_ErrorDS                         LikeDS(API_ErrorDS_T)                                    
    D                                     OPTIONS(*VARSIZE)                                        
    
    ** API Error Data structure                                                                    
    D QUSEC_EX        DS                  Qualified                                                
    D                                     Based(TEMPLATE_T)                                        
    D charKey                       10I 0                                                          
    D nErrorDSLen                   10I 0                                                          
    D nRtnLen                       10I 0                                                          
    D msgid                          7A                                                            
    D Reserved                       1A                                                            
    D CCSID                         10I 0                                                          
    D OffsetExcp                    10I 0                                                          
    D excpLen                       10I 0                                                          
    D excpData                     128A                                                            
    
    D ALGO_DES        C                   Const(20)                                                
    D ALGO_TDES       C                   Const(21)                                                
    D ALGO_AES        C                   Const(22)                                                
    D ALGO_RC4        C                   Const(30)                                                
    D ALGO_RSA_PUB    C                   Const(50)                                                
    D ALGO_RSA_PRIV   C                   Const(51)                                                
    
    D ANY_CRYPTO_SRV  C                   Const('0')                                              
    D SWF_CRYPTO_SRV  C                   Const('1')                                              
    D HWD_CRYPTO_SRV  C                   Const('2')                                              
    D CRYPTO_SRV      S             10A   Inz(*BLANKS)                                            
    
    ** Cipher API data structures.                                                                
    D myAlgo          DS                  LikeDS(ALGD0200_T)                                      
    D myKey           DS                  LikeDS(KEYD0200_T)                                      
    D apiError        DS                  LikeDS(qusec_ex)                                        
    
    ** The clear text (data to be encrypted)                                                      
    
    ** The length of the data returned by the APIs                                                
    D nRtnLen         S             10I 0                                                          
    ** The encrypted data variable                                                                
    D encData         S            500A                                                            
    
    c                   if        inXLTASC = 'Y' and inmode = 'E'                                  
     *               -- translate to ASCII                                                        
    c                   eval      bufLen=%len(invalue)                                            
    c                   eval      bufDta=invalue                                                  
    c                   call      'QDCXLATE'                                                      
    c                   parm                    bufLen                                            
    c                   parm                    bufDta                                            
    c                   parm                    xlttblA                                            
    c                   eval      invalue=bufDta                                                  
     *                                                                                            
     *               -- translate key to ASCII                                                        
    c                   eval      bufLen=%len(inkey)                                              
    c                   eval      bufDta=inkey                                                    
    c                   call      'QDCXLATE'                                                      
    c                   parm                    bufLen                                            
    c                   parm                    bufDta                                            
    c                   parm                    xlttblA                                            
    c                   eval      inkey=bufDta                                                    
    c                   endif                                                                      
     *                                                                                            
     /free                                                                                        
       workinvalue = invalue;                                                                      
       workinkey = inkey;                                                                          
       workoutvalue = outvalue;                                                                    
       myAlgo.Algorithm = ALGO_AES;                                                                                                                              
       myAlgo.blockLen = 16;                                                                      
       myAlgo.padOpt = '1';                                                                        
       myAlgo.padChar = x'00';                                                                    
       myAlgo.reserved = x'00';                                                                    
       myAlgo.macLen = X'00000000';                                                                
       myAlgo.effKeySiz = 0;                                                                      
    
       // ECB                                                                                      
       //myAlgo.mode = '0';                                                                        
       //myAlgo.intVec = *allx'00';                                                                
    
       // CBC                                                                                      
       myAlgo.mode = '1';                                                                          
       myAlgo.intVec = *allx'00';                                                                  
    
       myKey.type = ALGO_AES;                                                                      
       myKey.length = %Len(%TrimR(workinkey));                                                    
       myKey.Format = '0';                                                                        
       myKey.value = %TrimR(workinkey);                                                            
       apiError = *ALLX'00';                                                                      
       apiError.nErrorDSLen=%size(apiError);                                                      
       select;                                                                                    
         when inmode='E';                                                                          
            Qc3EncryptData(workinvalue:32:'DATA0100':                                              
            myAlgo : 'ALGD0200' :                                                                  
            myKey : 'KEYD0200' :                                                                  
            ANY_CRYPTO_SRV : CRYPTO_SRV :                                                          
            encData : %size(encData) : nRtnLen :                                                  
            apiError );                                                                            
            outvalue = encData;                                                                    
            apiError = *ALLX'00';                                                                  
            apiError.nErrorDSLen=%size(apiError);                                                  
         when inmode='D';                                                                          
           Qc3DecryptData(workinvalue : %len(%TrimR(workinvalue)) :                                
           myAlgo : 'ALGD0200' :                                                                  
           myKey : 'KEYD0200' :                                                                    
           ANY_CRYPTO_SRV : CRYPTO_SRV :                                                          
           outvalue : %size(outvalue) : nRtnLen :                                                  
           apiError );                                                                            
       endsl;                                                                                      
       *inlr=*on;                                                                                  
     /end-free
    INVALUE = 'testtt1234567890testtt1234567890'
    INKEY = 'testtt1234567890testtt1234567890'

    Translate Result :
    INVALUE ASCII = X'746573747474313233343536373839307465737474743132 3334353637383930'
    INKEY ASCII = X'746573747474313233343536373839307465737474743132 3334353637383930'

    Encrypted Result in Hexa =
    X'3BAF9DD2BFC510F36B14F464148C9E12F919E28DA76A599E 4E51448017539C83FD4602EE9D0C832348211BE3C00946C3'


    At https://www.devglan.com/online-tools...ion-decryption , Here is my input :
    #) text to be Encrypted = testtt1234567890testtt1234567890
    #) Select Mode = CBC
    #) Key Size in Bits = 256
    #) IV = I don't fill anything
    #) Secret Key = testtt1234567890testtt1234567890
    #) Output text format in Hex = X'5ED5688FAF56810CEA47E7E3545D531FE58D2AA96A57DEFE E5B00751D107F454B657809B42B5CB3A20E4023E864F23D5'


    Thanks and Regards,
    Randy
Working...
X