Kbase P100444: How to convert the binary contents of the MEMPTR returned by the ENCRYPT function to a Base64 charac
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  01/09/2009 |
|
Status: Verified
GOAL:
How to convert the binary contents of the MEMPTR returned by the ENCRYPT function to a Base64 character string and visa-versa?
GOAL:
How to ENCRYPT and DECRYPT character data.
FACT(s) (Environment):
OpenEdge 10.x
FIX:
The following code encodes the contents of the MEMPTR returned by the ENCRYPT function to a Base64 character string and decodes that Base64 character string back the original encrypted string:
DEFINE VARIABLE binary-key AS RAW NO-UNDO.
DEFINE VARIABLE c_enc_value AS CHARACTER NO-UNDO.
DEFINE VARIABLE c_dec_value AS CHARACTER NO-UNDO.
DEFINE VARIABLE c_value AS CHARACTER NO-UNDO.
DEFINE VARIABLE v_count AS INT NO-UNDO.
ASSIGN
binary-key = GENERATE-RANDOM-KEY
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-KEY = binary-key.
ETIME(YES).
OUTPUT TO c:\temp\vpk.csv.
PUT UNFORMATTED
"ETIME" AT 1
"VALUE TO BE ENCRYPTED" AT 7
"BASE64 DECODED VALUE" AT 40
"ENC = DEC?" AT 67
"BASE64 ENCODED STRING" AT 78 SKIP.
DO v_count = 1 TO 1000:
c_value = string(RANDOM(1,999999999)) + string(RANDOM(1,999999999)) + string(RANDOM(1,999999999)).
RUN vpk_encrypt(c_value,OUTPUT c_enc_value).
RUN vpk_decrypt(c_enc_value,OUTPUT c_dec_value).
PUT UNFORMATTED
ETIME AT 1
c_value AT 7
c_dec_value AT 40
c_value = c_dec_value AT 70
c_enc_value AT 78 SKIP.
END.
OUTPUT CLOSE.
PROCEDURE vpk_encrypt:
DEFINE INPUT PARAMETER ip-value-to-enc AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER op-char-value AS CHARACTER NO-UNDO.
DEFINE VARIABLE crypto-value AS RAW NO-UNDO.
ASSIGN
crypto-value = ENCRYPT(ip-value-to-enc)
op-char-value = BASE64-ENCODE(crypto-value).
END PROCEDURE.
PROCEDURE vpk_decrypt:
DEFINE INPUT PARAMETER ip-value-to-dec AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER op-char-value AS CHARACTER NO-UNDO.
DEFINE VARIABLE decrypt-value AS RAW NO-UNDO.
DEFINE VARIABLE long-char-value AS LONGCHAR NO-UNDO.
ASSIGN
long-char-value = ip-value-to-dec
op-char-value = GET-STRING(DECRYPT(BASE64-DECODE(long-char-value)),1).
END PROCEDURE.