I'm facing issue in DES decoding for few number value. Basically, The main objective is to encrypt the test case number and the test result and put it in a file then decode it later. I have written below procedure to do the above task.
Issues - Few numbers are not getting decoded properly. Ex "2" or "6". If I run the procedure using the argument value as follows
%tst_rslt_encode 11111111 4.1.4.1 PASS
and when I pass 4.1.4.2 or 4.1.4.6, the decode fails.
Am I doing mistake in reading the encrypted data from file ? Btw, the above test case works if I decode directly without storing it in a file.
Any suggestions would be greatly appreciated.
Script :-
package require des
proc tst_rslt_encode {key test_case tst_status} {
# Assign the key value and set the file locations
set t_key ""
set master_key [string trim $key]
# Find out the 64 bytes total key using test case and master key
set t_key [get_ttl_key $test_case $master_key]
# set the file location for storing the encrypted output
set key_file "D:\\test_file.txt"
set result_file "D:\\test_output.txt"
# Insure a file exists before reading
if ![file exists $key_file] {
set cr_file [open $key_file w+]
close $cr_file
}
# Encode the test case number using master key and append the file
set key_cipher [DES::des -mode ecb -dir encrypt -key $master_key $dyn_key]
if [catch {set keyFd [open $key_file a]} errmsg] {
puts "Unable to open file $key_file for write\n$errmsg"
return
}
puts $keyFd $key_cipher
close $keyFd
# Now encode the result using total key and append the result file
if [catch {set resFd [open $result_file a+]} errmsg] {
puts "Unable to open file $result_file for write\n$errmsg"
return
}
set status [string trim $tst_status]
set test_cipher [DES::des -mode ecb -dir encrypt -key $t_key $status]
puts "Here is the output of test_cipher :$test_cipher"
puts $resFd $test_cipher
close $resFd
# Now decode the test case and test results
set xfd [open $result_file r]
set outFd [open $key_file r]
while {[gets $xfd data] != -1 && [gets $outFd data1] != -1 } {
set tst_cs [string trim $data]
set tst_rslt [string trim $data1]
set tc_plain [DES::des -mode ecb -dir decrypt -key $master_key $tst_rslt]
# find out the total key
set t_key [get_ttl_key $tc_plain $master_key]
set rslt_plain [DES::des -mode ecb -dir decrypt -key $t_key $tst_cs]
puts "This should be the output after decode :$tc_plain $rslt_plain"
}
close $xfd
close $outFd
return
}
proc get_ttl_key {tst_case mst_key} {
# Remove the "." from test case
regsub -all "\\." $tst_case "" dyn_key
set dyn_key [string trim $dyn_key]
# Find out the 64 bytes total key using test case and master key
set mkey_len [expr 8 - [string length $dyn_key]]
set ms_key [string range $master_key 0 [expr $mkey_len - 1]]
append tt_key $dyn_key $ms_key
set tt_key [string trim $tt_key]
return $tt_key
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Kannan,
I am no expert in this, however, I had a similar issue. I suspect that you are running into an issue where when you are saving the resultant encryption, there are characters that are non-printable. Therefore when you read them back in order to decrypt them, your decryption fails. I have seen and worked around this issue.
The fix that I use is to convert the encrypted ASCII string to its hex equivalent, thereby ensuring that all characters are readable. To encrypt:
If you dont want to change the DES code itself (I didn't, just in case this was not what was desired by the -hex during decrypt), then here is how you can do it manually:
#Convert string to ascii:
regsub -all -nocase {([0-9a-f][0-9a-f])} $tst_rslt {[format %c 0x\1]} value
set value [subst $value]
Hi,
I'm facing issue in DES decoding for few number value. Basically, The main objective is to encrypt the test case number and the test result and put it in a file then decode it later. I have written below procedure to do the above task.
Issues - Few numbers are not getting decoded properly. Ex "2" or "6". If I run the procedure using the argument value as follows
%tst_rslt_encode 11111111 4.1.4.1 PASS
and when I pass 4.1.4.2 or 4.1.4.6, the decode fails.
Am I doing mistake in reading the encrypted data from file ? Btw, the above test case works if I decode directly without storing it in a file.
Any suggestions would be greatly appreciated.
Script :-
package require des
proc tst_rslt_encode {key test_case tst_status} {
# Assign the key value and set the file locations
set t_key ""
set master_key [string trim $key]
# Find out the 64 bytes total key using test case and master key
set t_key [get_ttl_key $test_case $master_key]
# set the file location for storing the encrypted output
set key_file "D:\\test_file.txt"
set result_file "D:\\test_output.txt"
# Insure a file exists before reading
if ![file exists $key_file] {
set cr_file [open $key_file w+]
close $cr_file
}
# Encode the test case number using master key and append the file
set key_cipher [DES::des -mode ecb -dir encrypt -key $master_key $dyn_key]
if [catch {set keyFd [open $key_file a]} errmsg] {
puts "Unable to open file $key_file for write\n$errmsg"
return
}
puts $keyFd $key_cipher
close $keyFd
# Now encode the result using total key and append the result file
if [catch {set resFd [open $result_file a+]} errmsg] {
puts "Unable to open file $result_file for write\n$errmsg"
return
}
set status [string trim $tst_status]
set test_cipher [DES::des -mode ecb -dir encrypt -key $t_key $status]
puts "Here is the output of test_cipher :$test_cipher"
puts $resFd $test_cipher
close $resFd
# Now decode the test case and test results
set xfd [open $result_file r]
set outFd [open $key_file r]
while {[gets $xfd data] != -1 && [gets $outFd data1] != -1 } {
set tst_cs [string trim $data]
set tst_rslt [string trim $data1]
set tc_plain [DES::des -mode ecb -dir decrypt -key $master_key $tst_rslt]
# find out the total key
set t_key [get_ttl_key $tc_plain $master_key]
set rslt_plain [DES::des -mode ecb -dir decrypt -key $t_key $tst_cs]
puts "This should be the output after decode :$tc_plain $rslt_plain"
}
close $xfd
close $outFd
return
}
proc get_ttl_key {tst_case mst_key} {
# Remove the "." from test case
regsub -all "\\." $tst_case "" dyn_key
set dyn_key [string trim $dyn_key]
# Find out the 64 bytes total key using test case and master key
set mkey_len [expr 8 - [string length $dyn_key]]
set ms_key [string range $master_key 0 [expr $mkey_len - 1]]
append tt_key $dyn_key $ms_key
set tt_key [string trim $tt_key]
return $tt_key
}
Hi Kannan,
I am no expert in this, however, I had a similar issue. I suspect that you are running into an issue where when you are saving the resultant encryption, there are characters that are non-printable. Therefore when you read them back in order to decrypt them, your decryption fails. I have seen and worked around this issue.
The fix that I use is to convert the encrypted ASCII string to its hex equivalent, thereby ensuring that all characters are readable. To encrypt:
set key_cipher [DES::des -mode ecb -hex -dir encrypt -key $master_key $dyn_key]
To decrypt, there seems to be an issue in tcllib, but I can get no response from the mail list (I guess you have the same issue). Basically, you have to convert back from hex to ascii before decrypting. I created a fix for this, its in my original discussion topic:
https://sourceforge.net/forum/forum.php?thread_id=3235370&forum_id=40548
If you dont want to change the DES code itself (I didn't, just in case this was not what was desired by the -hex during decrypt), then here is how you can do it manually:
#Convert string to ascii:
regsub -all -nocase {([0-9a-f][0-9a-f])} $tst_rslt {[format %c 0x\1]} value
set value [subst $value]
#Decrypt
set tc_plain [DES::des -mode ecb -dir decrypt -key $master_key $value]
Cheers
Colin