OpenVMS Source Code Demos
ssl_aes_demo01
1000 !==================================================================================
! title : SSL_AES_DEMO01.BAS
! author : Neil Rieck
! created: 2017-12-12
! notes : 1. this demo program is much larger than it needs to be. Additional code
! was added here to double-check the output of this logic against AES
! demo websites (see embedded html links below)
! 2. never place this code into production without extensive testing. You
! would not want to store encrypted data only to discover that future
! changes left older data unusable.
!==================================================================================
option type=explicit ! no kid stuff
!
! home brewed c code (see: SSL_AES_API_DEMO01.C)
!
external long function NSR_AES_ENCRYPT(string,string,string,string,string,long by value)
external long function NSR_AES_DECRYPT(string,string,string,string, long by value)
!
! home brewed BASIC code
!
external string function plain_to_hex(string)
!
! data declarations
!
declare string constant k_bell = '7'C !
!
declare string plaintext$ ,! plain text &
hex_iv$ ,! initialization vector &
key$ ,! key phrase &
hex_encrypted$ ,! encrypted text (hex) &
b64_encrypted$ ,! encrypted text (base64) &
expected$ ,! hex -or- base64 &
decrypted$ ,! decrypted plain text &
long i% ,! &
test% ,! &
debug% !
!
!=======================================================================
! main
!=======================================================================
main:
debug% = 0
debug% = 1
!
! test case #1
!
! notes: 1) these data values were hard-coded into file "sys$examples:SSL$AES.C"
! 2) the results were visible in the dump of RMS file "TEST.ENC"
!
print
print "test case #1 ==================================================="
plaintext$ = "1234567890123456789012345678901234567890123456789012345678901234567890123456789"
hex_iv$ = "" ! blank means do not set IV
key$ = "This is my simple encryption key" !
print "-i-calling external function to encrypt"
i% = NSR_AES_ENCRYPT( plaintext$, ! out &
hex_iv$, ! out &
key$, ! out &
hex_encrypted$, ! in &
b64_encrypted$, ! in &
debug%) ! out
expected$ = "a9db19d1ccc67451482ad2eb156d18b8a43faf6a" +&
"844e300755478f207f0058ba3109568c1a088f70" +&
"7a2871fbba7f0f4d9caf99a2c15d2c8658630837" +&
"803582ab112a6c93b6bb8a047963006455729d43"
gosub display_results1 !
!
! okay now see if we can decrypt
!
decrypted$ = "" ! init
print "-i-calling external function to decrypt"
i% = NSR_AES_DECRYPT( hex_encrypted$, ! out &
hex_iv$, ! out &
key$, ! out &
decrypted$, ! in &
debug%) ! out
gosub display_results2 !
!=======================================================================
!
! test case #2 (should produce the same results as above)
!
print
print "test case #2 ==================================================="
plaintext$ = "1234567890123456789012345678901234567890123456789012345678901234567890123456789"
hex_iv$ = "0000000000000000000000" !
key$ = "This is my simple encryption key" !
print "-i-calling external function to encrypt"
i% = NSR_AES_ENCRYPT( plaintext$, &
hex_iv$, &
key$, &
hex_encrypted$, ! in &
b64_encrypted$, ! in &
debug%) ! out
print "-i-back in BASIC"
gosub display_results1
!
! okay now see if we can decrypt
!
decrypted$ = "" ! init
print "-i-calling external function to decrypt"
i% = NSR_AES_DECRYPT( hex_encrypted$, ! out &
hex_iv$, ! out &
key$, ! out &
decrypted$, ! in &
debug%) ! out
gosub display_results2 !
!=======================================================================
!
! test case #3
!
! aes verification sites:
! 1) http://rubbingalcoholic.github.io/cowcrypt/demos/aes.html
! 2) http://aes.online-domain-tools.com (be sure to select "CBC")
!
print
print "test case #3 ==================================================="
plaintext$ = "this is a test" !
hex_iv$ = "9876543210fedcba9876543210fedcba" ! hex representation of IV
key$ = "This is my simple encryption key" !
print "-i-calling external function to encrypt"
i% = NSR_AES_ENCRYPT( plaintext$, &
hex_iv$, &
key$, &
hex_encrypted$, ! in &
b64_encrypted$, ! in &
debug%) ! out
expected$ = "fyZhAlzK7dTDL+LMrE4cGw=="
gosub display_results1
!
! okay now see if we can decrypt
!
decrypted$ = "" ! init
print "-i-calling external function to decrypt"
i% = NSR_AES_DECRYPT( hex_encrypted$, ! out &
hex_iv$, ! out &
key$, ! out &
decrypted$, ! in &
debug%) ! out
gosub display_results2 !
!=======================================================================
goto fini ! ***--->>>
!
!=======================================================================
! display results 1 (after encryption)
!=======================================================================
display_results1:
print "-i-back in BASIC"
print "-i-encryp status: " ;str$(i%);
if i% = 1 then
print " (success)"
else
print " (failed)"+ k_bell
end if
print "-i-plain text : "; plaintext$
print "-i-hex iv : "; hex_iv$
print "-i-plain key : "; key$
print "-i-hex key : "; plain_to_hex(key$)
print "-i-hex encryped : "; hex_encrypted$ !
print "-i-b64 encryped : "; b64_encrypted$ !
print "-i-expected : "; expected$ !
if expected$ <> "" then
test% = 0
test% = 1 if hex_encrypted$ = expected$
test% = 1 if b64_encrypted$ = expected$
if test% = 0 then
print "-e-expected encrypted data test FAILED"+ k_bell
else
print "-i-expected encrypted data test PASSED"
end if
else
print "-w-expected data test NOT PERFORMED"
end if
sleep 1
return
!=======================================================================
! display results 2 (after decryption)
!=======================================================================
display_results2:
print "-i-back in BASIC"
print "-i-decryp status:"; i%;
if i% = 1 then
print "(success)"
else
print "(failed)"+ k_bell
end if
print "-i-decrypted : "; decrypted$
print "-i-original : "; plaintext$
if plaintext$ = decrypted$ then
print "-i-data match - decryption was sucessful"
else
print "-e-data mismatch - decryption FAILED"+ k_bell
end if
return
!=======================================================================
! that's all folks
!=======================================================================
fini:
print "-i-program exiting"
32000 end
!===============================================================================
! I need this because one AES web site requires me to enter the key in HEX
!===============================================================================
32100 function string plain_to_hex(string inbound$)
option type=explicit
declare string constant k_hex = "0123456789abcdef"
declare long i,x,y,z, &
string temp$, &
junk$ !
for i = 1 to len(inbound$) !
junk$ = mid$(inbound$,i,1) !
x = ascii(junk$) !
y = x / 16 !
z = mod(x,16) !
temp$ = temp$ + mid$(k_hex, y+1, 1)
temp$ = temp$ + mid$(k_hex, z+1, 1)
next i !
plain_to_hex = temp$ !
end function
Back to
Home
Neil Rieck
Waterloo, Ontario, Canada.