encryption/decryption by REXX

797 views Asked by At

I study z/OS and REXX and now have code, which takes public and private keys from MY.DATA.SET (PUBLIC, PRIVATE) and encrypts the message (MSG):

"ALLOC FI(pubkey) DA('MY.DATA.SET(PUBLIC)') SHR REUSE"   
"ALLOC FI(prikey) DA('MY.DATA.SET(PRIVATE)') SHR REUSE"  
"ALLOC FI(msgin)  DA(‘MY.DATA.SET(MSG)') SHR REUSE"  
"ALLOC FI(msgout) DA(*) SHR REUSE"                                      
"EXECIO 1 DISKR pubkey (STEM pub. FINIS"                                
"EXECIO 1 DISKR prikey (STEM pri. FINIS"                                
"EXECIO * DISKR msgin  (STEM msg. FINIS"                                
"EXECIO 0 DISKW msgout (STEM enc_msg. OPEN"                             
enc_msg.1 = pub.1                                                       
"EXECIO 1 DISKW msgout (STEM enc_msg. "                                 
do i=1 to msg.0                                                     
   do j=1 to length(msg.i)                                          
    letter.j = substr(msg.i,j,1)                                    
    encrypt.j = translate(letter.j,pri.1,pub.1)                     
   end                                                              
call write_encrypted_line                                           
end                                                                 
"EXECIO 0 DISKW msgout (FINIS"                                      
"FREE FI(pubkey)"                                                   
"FREE FI(prikey)"                                                   
"FREE FI(msgin)"                                                    
"FREE FI(msgout)"                                                   
exit 0                                                              
write_encrypted_line:                                               
charout = ''                                                        
newchar = ''                                                        
  do j=1 to length(msg.i)                                           
   newchar = encrypt.j                                              
   charout = charout||newchar                                       
  end                                                               
enc_msg.1 = charout                                                 
"EXECIO 1 DISKW msgout (STEM enc_msg. "                             
return                                                              

And I'd like to transfer it to DEsrypting code, which can DEcrypt the encrypted result from above (the result is stored by name MSGEN) to normal text, using, of course, same key-pair. Please, help: what should I change in my encpypring code to make it decrypting? The line

"ALLOC FI(msgin)  DA(‘MY.DATA.SET(MSGEN)') SHR REUSE"

is already changed (MSG->MSGEN)

Thanks for any all help and response!

3

There are 3 answers

0
paulywill On BEST ANSWER

Without knowing what your keys are I think you need to reverse the keys in your TRANSLATE line.

http://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.ikja300/transl.htm

Here are some examples: 
TRANSLATE('abcdef')                    ->    'ABCDEF'
TRANSLATE('abbc','&','b')              ->    'a&&c'
TRANSLATE('abcdef','12','ec')          ->    'ab2d1f'
TRANSLATE('abcdef','12','abcd','.')    ->    '12..ef'
TRANSLATE('APQRV',,'PR')               ->    'A Q V'
TRANSLATE('APQRV',XRANGE('00'X,'Q'))   ->    'APQ  '
TRANSLATE('4123','abcd','1234')        ->    'dabc'



do i=1 to msg.0                                                     
   do j=1 to length(msg.i)                                          
     letter.j = substr(msg.i,j,1)                                    
     decrypt.j = translate(letter.j,pub.1,pri.1)                     
   end                                                              
   call write_dencrypted_line                                           
end
1
Alan Spicer On

This is from IBM Master the Mainframe 2016. It's the training system for the contest. The 2016 contest is already over with.

The stuff about outputting the 1st line can be commented out. The I variable made higher in do i=2 to msg.0 to be like mine "2" to skip the first line of output.

The Public Key is already in the MSG input so just need - pub.1 = msg.1

Delete or comment out the ALLOC, FREE, and EXECIO for Public Key.

It will work if you don't do that, but it's not a complete task.

0
Alan Spicer On
/* REXX */                                                            
 /******************************************************************/  
 /**                                                              **/  
 /** Get access to private key, public key, and message           **/  
 /**                                                              **/  
 /******************************************************************/  
 "ALLOC FI(prikey) DA('ZOS.PUBLIC.SECRET.MESSAGES(PRIVATE)') SHR REUSE"
 "ALLOC FI(msgin)  DA('ZOS.PUBLIC.SECRET.MESSAGES(SECRET)') SHR REUSE" 
 /* "ALLOC FI(msgout) DA(*) SHR REUSE" */                              
 /******************************************************************/  
 /**                                                              **/  
 /** In the DECRYPT REXX routine, upon successful decryption      **/  
 /** of ZOS.PUBLIC.SECRET.MESSAGES(SECRET), uncomment msgout      **/  
 /** ALLOC below and comment the msgout ALLOC above               **/  
 /**                                                              **/  
 /******************************************************************/  
 "ALLOC FI(msgout) DA(P3.OUTPUT(#14)) SHR REUSE"                       
 /******************************************************************/  
 /**                                                              **/  
 /** Read private key, public key, and message                    **/  
 /**                                                              **/  
 /******************************************************************/  
 "EXECIO 1 DISKR prikey (STEM pri. FINIS"                              
 "EXECIO * DISKR msgin  (STEM msg. FINIS"                              
 pub.1 = msg.1                                                         
 /* say msg.1 */                                                       
 /******************************************************************/  
 /**                                                              **/  
 /** Parse  encrypted message using (REVERSE) private and public  **/  
 /** key pair to create decrypted message                         **/  
 /**                                                              **/  
 /******************************************************************/  
 do i=2 to msg.0                                                       
    do j=1 to length(msg.i)                                            
     letter.j = substr(msg.i,j,1)                                      
     decrypt.j = translate(letter.j,pub.1,pri.1)                       
    end                                                                
 call write_decrypted_line