Skip to main content
Gary Rhodes
Participating Frequently
Aug 18, 2023 4:29 PM UTC

ENCRYPT() FUNCTION AES

I need to encrypt a string with the aes-256-cbc. My encryption is not matching what the other end needs which is a php endpoint.  In the documentation there was definitely a statement that makes me think I did at least one thing wrong but I am not sure.  Converting the aeskey to a hexidecimal format is what I think I may not understand. You see that I am taking each character in aeskey and building a string of the hex values for the character.  Below is a bit of the code for the encryption.  Once again I am missing something. Thanks all.

BASE.JSON = '{"customer_id":"1000"}'
ALG = "aes-256-cbc"
ACT=1
AESKEY= "31e92H1^FiERFgv7^nXg6DfC66rGpFb7"
GOSUB HEX.AES.KEY
IV.HEX = "31313131313131313131313131313131"
RESULT=""
RES =  ENCRYPT(ALG,ACT,BASE.JSON,1,AESKEY,1,3,"",IV.HEX,RESULT,1)
DEBUG
STOP
HEX.AES.KEY: 
NKEY=""
KEY.LEN = LEN(AESKEY)
FOR X = 1 TO KEY.LEN
  KL = AESKEY[X,1]
  HKL = OCONV(KL,'MX')
  NKEY:=HKL
NEXT X
AESKEY = NKEY
RETURN



------------------------------
[Gary] [Rhodes]
[Universe Developer]
[NPW Companies]
[Hialeah] [FL] [USA]
------------------------------

    2 replies

    Doug Averch
    Inspiring
    Aug 19, 2023 6:22 PM UTC

    I need to encrypt a string with the aes-256-cbc. My encryption is not matching what the other end needs which is a php endpoint.  In the documentation there was definitely a statement that makes me think I did at least one thing wrong but I am not sure.  Converting the aeskey to a hexidecimal format is what I think I may not understand. You see that I am taking each character in aeskey and building a string of the hex values for the character.  Below is a bit of the code for the encryption.  Once again I am missing something. Thanks all.

    BASE.JSON = '{"customer_id":"1000"}'
    ALG = "aes-256-cbc"
    ACT=1
    AESKEY= "31e92H1^FiERFgv7^nXg6DfC66rGpFb7"
    GOSUB HEX.AES.KEY
    IV.HEX = "31313131313131313131313131313131"
    RESULT=""
    RES =  ENCRYPT(ALG,ACT,BASE.JSON,1,AESKEY,1,3,"",IV.HEX,RESULT,1)
    DEBUG
    STOP
    HEX.AES.KEY: 
    NKEY=""
    KEY.LEN = LEN(AESKEY)
    FOR X = 1 TO KEY.LEN
      KL = AESKEY[X,1]
      HKL = OCONV(KL,'MX')
      NKEY:=HKL
    NEXT X
    AESKEY = NKEY
    RETURN



    ------------------------------
    [Gary] [Rhodes]
    [Universe Developer]
    [NPW Companies]
    [Hialeah] [FL] [USA]
    ------------------------------

    This might be your KeyAction. Try 1 or 2 instead of 3.

    Posted: 08-19-2023 18:21



    ------------------------------
    Doug Averch
    Owner
    U2 Logic
    ------------------------------

    Doug Averch Owner https://www.u2logic.com/
    Gary Rhodes
    Participating Frequently
    Aug 20, 2023 7:56 AM UTC

    I need to encrypt a string with the aes-256-cbc. My encryption is not matching what the other end needs which is a php endpoint.  In the documentation there was definitely a statement that makes me think I did at least one thing wrong but I am not sure.  Converting the aeskey to a hexidecimal format is what I think I may not understand. You see that I am taking each character in aeskey and building a string of the hex values for the character.  Below is a bit of the code for the encryption.  Once again I am missing something. Thanks all.

    BASE.JSON = '{"customer_id":"1000"}'
    ALG = "aes-256-cbc"
    ACT=1
    AESKEY= "31e92H1^FiERFgv7^nXg6DfC66rGpFb7"
    GOSUB HEX.AES.KEY
    IV.HEX = "31313131313131313131313131313131"
    RESULT=""
    RES =  ENCRYPT(ALG,ACT,BASE.JSON,1,AESKEY,1,3,"",IV.HEX,RESULT,1)
    DEBUG
    STOP
    HEX.AES.KEY: 
    NKEY=""
    KEY.LEN = LEN(AESKEY)
    FOR X = 1 TO KEY.LEN
      KL = AESKEY[X,1]
      HKL = OCONV(KL,'MX')
      NKEY:=HKL
    NEXT X
    AESKEY = NKEY
    RETURN



    ------------------------------
    [Gary] [Rhodes]
    [Universe Developer]
    [NPW Companies]
    [Hialeah] [FL] [USA]
    ------------------------------

    I want to follow up on my own question. I was able to accomplish my task. The original question I had was to make sure I correctly interpreted one of the statements in the documentation and I did.  I did create the hexadecimal string correctly.   I took some irrelevant code out but my code is below. This program takes some json and encrypts it before launching a web page that will decrypt it and display it in a browser. As you can see the end point is a php site and I don't know what functions they are using for decryption.  We both have a keys to properly encrypt and decrypt the message to see the json.

    ESC = CHAR(27);STX=CHAR(2);CR = CHAR(13)
    STMT = ESC:STX:"<"
    BASE.JSON='{"customer_id":"1000","name":"Best Customer","address":"111 Main st","city":"Anywhere","state":"CA","zip":"22222"}'
    *
    ALG = "aes-256-cbc"
    ACT=5
    AESKEY= "11111111111111111111111111111111"
    GOSUB HEX.AES.KEY
    GOSUB GEN.IV
    RESULT=""
    RES =  ENCRYPT(ALG,ACT,BASE.JSON,1,AESKEY,1,3,"",IV.HEX,RESULT,1)
    DAT = RESULT:"::":IV
    RESULT=""
    ERES = ENCODE("BASE64A",1,DAT,1,RESULT,1)
    ENDPOINT = "https://api.com/p1/p2/p3.php?data="
    URL=ENDPOINT:RESULT
    STMT:=URL:CR
    CRT STMT
    *
    DEBUG
    STOP
    GEN.IV: 
    IV=""
    IV.HEX=""
    FOR X = 1 TO 16
      RN = RND(93)
      RN+=32
      C = CHAR(RN)
      HC = OCONV(C,'MX')
      IV:=C
      IV.HEX:=HC
    NEXT X
    RETURN
    HEX.AES.KEY: 
    NKEY=""
    KEY.LEN = LEN(AESKEY)
    FOR X = 1 TO KEY.LEN
      KL = AESKEY[X,1]
      HKL = OCONV(KL,'MX')
      NKEY:=HKL
    NEXT X
    AESKEY = NKEY
    RETURN



    ------------------------------
    [Gary] [Rhodes]
    [Universe Developer]
    [NPW Companies]
    [Hialeah] [FL] [USA]
    ------------------------------