JD Edwards

JD Edwards
Lokesh Reddy Blog

Friday, June 20, 2014

Encryption&Decryption of a Password using API’s in a C Business Function

I Created a Custom Business function to Encrypt and Decrypt the data using the API's

Firstly  created the corresponding data structure  & Created the Custom Table  F55ENC and store the encrypted value in the form of BLOBS.In Order the remain the backward compatible,Load library.
Created the Internal Function for encrypting & Decrypting tha data, by using the switch case.By using TripleDES(3DES) to open the handle library and define the Environment and finally close the handle library.
Triple DES (3DES) is the common name for the Triple Data Encryption Algorithm (TDEA or Triple DEA).
Code as Follows.

#include <jde.h>
typedef int (*LPFNEncryption3DES)(BYTE *outbuf, int *poutlen, JCHAR *inbuf,int inlen,JCHAR *encryptkey, int keylen,int type);
typedef int (*LPFNDecryption3DES)(JCHAR *outbuf, int *poutlen, BYTE *inbuf,  int inlen,JCHAR *encryptkey, int keylen,int type);
#define b57enc_c


/*****************************************************************************
 *    Source File:  b57enc
 *
 *    Description:  Encryption&Decryption Source File
 *
 *        History:
 *          Date        Programmer  SAR# - Description
 *          ----------  ----------  -------------------------------------------
 *   Author 28-04-2014  Unkno       Unknown  - Created  
 *
 * Copyright (c) J.D. Edwards World Source Company, 1996
 *
 * This unpublished material is proprietary to J.D. Edwards World Source Company.
 * All rights reserved.  The methods and techniques described herein are
 * considered trade secrets and/or confidential.  Reproduction or
 * distribution, in whole or in part, is forbidden except by express
 * written permission of J.D. Edwards World Source Company.
 ****************************************************************************/
/**************************************************************************
 * Notes:
 *
 **************************************************************************/

#include <b57enc.h>


/**************************************************************************
 *  Business Function:  EncryptionsDecryptions
 *
 *        Description:  Encryption and Decryption
 *
 *         Parameters:
 *           LPBHVRCOM           lpBhvrCom    Business Function Communications
 *           LPVOID              lpVoid       Void Parameter - DO NOT USE!
 *           LPDSD56             lpDS         Parameter Data Structure Pointer  
 *
 *************************************************************************/

JDEBFRTN (ID) JDEBFWINAPI EncryptionsDecryptions (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD56ENC lpDS) 
 {
   /************************************************************************
    *  Variable declarations
    ************************************************************************/
       HUSER                hUser = (HUSER)NULL;
       ID                         idJDBReturn = JDEDB_PASSED;
       HREQUEST             hRequestF55ENC = (HREQUEST) NULL;
       void                 *blob;       
       ID                         idEncryptionError = ER_SUCCESS;
       MATH_NUMERIC         mnInsertcounter;
   /************************************************************************
    * Declare structures
    ************************************************************************/
       F55ENC               dsF55ENC = {0};
       DSD0000564           dsGetInternalNextNum;
   /************************************************************************
    * Declare pointers
    ************************************************************************/

   /************************************************************************
    * Check for NULL pointers
    ************************************************************************/
   if ((lpBhvrCom == (LPBHVRCOM) NULL) ||
       (lpVoid    == (LPVOID)    NULL) ||
       (lpDS      == (LPDSD56ENC) NULL))
   {
     jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);
     return ER_ERROR;
   }
 /**************************************************************************     
 * Call to the Initialize Behavior Routine     
 **************************************************************************/
   idJDBReturn = JDB_InitBhvr(lpBhvrCom, &hUser, (JCHAR *)NULL, JDEDB_COMMIT_AUTO); 
   if (idJDBReturn == JDEDB_FAILED)
   {                             
          jdeErrorSet(lpBhvrCom, lpVoid, (ID)0, _J("078S"), (LPVOID)NULL);
          return ER_ERROR; 
   }
   /************************************************************************
    * Set pointers
    ************************************************************************/

   /************************************************************************
    * Main Processing
    ************************************************************************/
       lpDS->cErrorCode = _J('0');
       idJDBReturn = JDB_OpenTable(hUser, NID_F55ENC, ID_F55ENC_JOBS_INERTC, NULL, 0, NULL, &hRequestF55ENC);
       if(hRequestF55ENC != (HREQUEST) NULL)
        {
              jdeCallObject(_J("GetInternalNextNumber"), NULL, lpBhvrCom, (void *) lpVoid, (void *) &dsGetInternalNextNum, (CALLMAP *) NULL, (int) 0, (JCHAR *) NULL,(JCHAR *) NULL, (int) 0);
              MathCopy(&dsF55ENC.enjobs, &dsGetInternalNextNum.mnJobnumberA);
                     IncrementMathNumeric(&mnInsertcounter, 1);
                     MathCopy(&dsF55ENC.eninsertc,&mnInsertcounter);
        
               /* Set values*/
               jdeStrcat(lpDS->szPassword, _J("\0"));                      
               idEncryptionError = I57enc_EncryptDecrypt(Encrypt_Operation, lpDS->szPassword, &dsF55ENC, hRequestF55ENC, lpBhvrCom, lpVoid);
               if(idEncryptionError == ER_SUCCESS)           
               {                                             
                      jdeStrncpy (dsF55ENC.enapluser, lpDS->szCreditCardNumber,DIM(dsF55ENC.enapluser));
                      jdeStrncpy (dsF55ENC.enjobn, lpDS->szMachine, DIM(dsF55ENC.enjobn));     
                      dsF55ENC.enupmj = lpDS->jdDateUpdated;                      
                      MathCopy(&dsF55ENC.enupmt, &lpDS->mnTimeLastUpdated);
                      idJDBReturn = JDB_InsertTable(hRequestF55ENC, NID_F55ENC, (ID) 0,   &dsF55ENC);             
                      if(idJDBReturn != JDEDB_PASSED)        
                      {                        
                            /*Insert Failed Set error*/     
                            lpDS->cErrorCode = _J('1');     
                      }           
               }           
               else        
               {
                      /*Encryption Failed Set error*/        
                      lpDS->cErrorCode = _J('4');                                              
        }           
}

      
   /************************************************************************
    * Function Clean Up
    ************************************************************************/
/* I57enc_CloseF55ENC (hRequestF55ENC);*/
        JDB_CloseTable(hRequestF55ENC); 
        JDB_FreeBhvr(hUser);
   return (!idJDBReturn);
}
 /**************************************************************************** *
 Function:    I55enc_CloseF55ENC *
 *         Notes:    Closes the table *
 *       Returns:    Success / Failure *
 * Parameters:       hrequest
 ****************************************************************************/
/*void I55enc_CloseF55ENC (HREQUEST hRequestF55ENC)
{
       if (hRequestF55ENC  != (HREQUEST) NULL)
       {            
              JDB_CloseTable(hRequestF55ENC);           
       }     
}
/* Internal function comment block */
/**************************************************************************
 *   Function:  I57enc_a   // Replace "xxxxxxx" with source file number
 *                           // and "a" with the function name
 *      Notes:
 *
 *    Returns:
 *
 * Parameters:
 **************************************************************************/
 ID I57enc_EncryptDecrypt(int Operation, JCHAR* szPassword, F55ENC* dsF55ENC, HREQUEST hRequestF55ENC, LPBHVRCOM lpBhvrCom, LPVOID lpVoid)
 {    
        JCHAR EKey[71]={"12345678967"};
        /*JCHAR      EncryptionKey={"12345678967"};*/
        int          EKeyLen=11;  
        int          PasswordLen=0;      
        BYTE         EncryptedPassword[1024]={0};            
        int          EncryptedPasswordLen= 0;  
        JCHAR DecryptedPassword[70]={0};
        int          DecryptedPasswordLen=0;          
        JCHAR               szLib[256]           = {0};
        JCHAR               szConvertLib[256]    = {0};
        HINSTANCE       hLibrary;
        LPFNEncryption3DES          lpDoEncryption     = NULL; 
        LPFNDecryption3DES          lpDoDecryption     = NULL;      
        int iResult = 0;   
        ENCRYPTEDBLOB       myblobstructure = {0};
        DBREF dbRef = {0}; 
        int          iKeyVersion=0;      
        
        /*In order to remain backwards compatable, LoadLib*/      
       #if defined (JDENV_UNIX)         
        jdeStrcpy(szConvertLib, _J("jdeknet"));  
        jdeWriteLogEntry(JDEDEBUGLOG, _J("B9801000"), __FILE__, __LINE__, 0, _J("F98OWSEC Maintain Security LoadLib jdeknet"));
#else     
        jdeStrcpy(szConvertLib, _J("JDEKRNL")); 
        jdeWriteLogEntry(JDEDEBUGLOG, _J("B9801000"), __FILE__, __LINE__, 0, _J("F98OWSEC Maintain Security LoadLib JDEKRNL"));   
#endif      
        BuildFullLibName( szConvertLib, szLib );    
        hLibrary = LoadLibrary ( szLib );     
        if( !hLibrary )    
        {      
               jdeWriteLogEntry(JDEDEBUGLOG, _J("B9801000"), __FILE__, __LINE__, 0, _J("F98OWSEC Maintain Security LoadLib Failed"));
               return ER_ERROR;     
        }      
#if defined (JDENV_PC)
        lpDoEncryption = (LPFNEncryption3DES) jdeGetProcAddress(hLibrary, _J("_jdeEncryptWKey@28"));  
        lpDoDecryption = (LPFNDecryption3DES) jdeGetProcAddress(hLibrary, _J("_jdeDecryptWKey@28"));  
#else       
        lpDoEncryption = (LPFNEncryption3DES) jdeGetProcAddress(hLibrary, _J("jdeEncryptWKey"));  
        lpDoDecryption = (LPFNDecryption3DES) jdeGetProcAddress(hLibrary, _J("jdeDecryptWKey"));  
#endif              
        /**********Close hlibrary handle******************/  
        FreeLibrary(hLibrary);   
        hLibrary = 0L;           
        if (lpDoEncryption != NULL && lpDoDecryption != NULL)  
        {
               /*Do encryption/decryption*/    
               switch (Operation)       
               {                  
               case Encrypt_Operation:
                     /* if(lpDS->cErrorCode == _J('0'))
                           {
                                  jdeStrncpy(EKey, EncryptionKey, DIM(EKey));
                                  EKeyLen = jdeStrlen(EKey);*/
                                                      
                            /*Encrypt the Password*/               
                            PasswordLen = jdeStrlen(szPassword);
                            /*ensure to pass the reference address of EncryptedLen */          
                            iResult=lpDoEncryption(EncryptedPassword,&EncryptedPasswordLen,szPassword,PasswordLen,EKey,EKeyLen,1);      
                            /*return the encrypted value and length in dstr*/                  
                            jdeNIDcpy(dbRef.szTable,NID_F55ENC);          
                            jdeNIDcpy(dbRef.szDict,NID_APLPSWD);          
                             dbRef.idInstance = 0;                  
                            dsF55ENC->enaplpswd.lpValue = JDB_SetBLOBSize(hRequestF55ENC,dbRef,sizeof(myblobstructure));      
                            dsF55ENC->enaplpswd.lSize = sizeof(myblobstructure);                      
                            dsF55ENC->enaplpswd.lMaxSize= 2048;                                
                            memcpy(myblobstructure.bEncryptedPassword,EncryptedPassword,EncryptedPasswordLen);      
                            myblobstructure.iEncPasswordSize=EncryptedPasswordLen;                           
                           /* myblobstructure.iKeyVersion=dsD55enc.nEncryptionKeyVersion;       */      
                            memcpy(dsF55ENC->enaplpswd.lpValue,&myblobstructure,sizeof(myblobstructure));
                     /* }
                      else
                      {
                            return ER_ERROR;   
                      } */
                       /*return ER_ERROR;*/
                      break;      
               case Decrypt_Operation:  
                      /*set the encrypted value*/
                      memcpy(EncryptedPassword,((ENCRYPTEDBLOB*)(dsF55ENC->enaplpswd.lpValue))->bEncryptedPassword,sizeof(EncryptedPassword));
                      EncryptedPasswordLen = ((ENCRYPTEDBLOB*)(dsF55ENC->enaplpswd.lpValue))->iEncPasswordSize;                             
                            /*ensure to pass the reference address of DecryptedLen */                 
                            iResult=lpDoDecryption(DecryptedPassword,&DecryptedPasswordLen,EncryptedPassword,EncryptedPasswordLen,EKey,EKeyLen,1);                                 
                            /*return the Password*/                       
                            jdeStrcpy (szPassword, DecryptedPassword);                  
                            return ER_ERROR;   
                          
                      break;             
               default:           
                      jdeWriteLogEntry(JDEDEBUGLOG, _J("B9801000"), __FILE__, __LINE__, 0, _J("Invalid action"));    
                      return ER_ERROR;                
                      break;             
               }
               if(iResult==0)
               {                  
                      jdeVWriteLogEntry(_J("B9801000"), __FILE__, __LINE__, 0, _J("Failed on encryption/decryption")); 
                      return ER_ERROR;         
               }    
        }    
        else 
        {           
               /*do normal thing without encryption/decryption*/    
               /*No Encryption Available so just save or return it as is*/
               switch (Operation)       
               {                  
               case Encrypt_Operation:         
                      PasswordLen = (jdeStrlen(szPassword)+1) *  sizeof(JCHAR);   
                      jdeNIDcpy(dbRef.szTable,NID_F55ENC);                        
                      jdeNIDcpy(dbRef.szDict,NID_APLPSWD);   
                      dbRef.idInstance = 0;                                
                      dsF55ENC->enaplpswd.lpValue = JDB_SetBLOBSize(hRequestF55ENC, dbRef, PasswordLen);
                      memcpy(dsF55ENC->enaplpswd.lpValue,szPassword,PasswordLen);                     
                      dsF55ENC->enaplpswd.lSize = PasswordLen;                                               
                      break;                          
               case Decrypt_Operation:
                      jdeStrcpy (szPassword, ((JCHAR*)dsF55ENC->enaplpswd.lpValue));                                
                      break;                    
               default:           
                      break;             
               }    
        }           
        return ER_SUCCESS;
}
/* Internal function comment block */
/**************************************************************************
 *   Function:  Ixxxxxxx_a   // Replace "xxxxxxx" with source file number
 *                           // and "a" with the function name
 *      Notes:
 *
 *    Returns:
 *
 * Parameters:
 **************************************************************************/


  

and