I'm working on the beginnings of a file encrypter / decrypter.
I've got the encryption process to work seemingly fine.
The decryption process works OK for text files.
I tried to do a jpeg and it came out shifted and discolored.
There's the same number of bytes in the original file and the decrypted file.
The program just splits a file in two, rearranges the chunks, then puts them back together.
I'm not sure why the jpeg gets messed up and the text file is fine.
Any clues?
(see attachment for the decrypted jpg)
Code can likely be skipped over until the DecryptFile() function, though there may be an issue with how I'm placing the header (which contains the location of the file split). Information on that is in the EncryptFile() function.
`---------------------------Main----------------------------------------
disable escapekey
repeat
cls
choice$ = ""
print "Press 1 to start an encryption process"
print "Press 2 to start an decryption process"
print "Press 3 to package a directory"
print "Press 4 to unpackage and decrypt"
print "press Q to quit"
print
choice$ = inkey$()
if lower$(choice$) <> "q" and choice$ <> "" then RunOptions(choice$)
until lower$(choice$) = "q"
end
`-----------------------------------------------------------------------
function RunOptions(choice$)
select choice$
case "1"
EncryptFile()
endcase
case "2"
DecryptFile()
endcase
case "3"
PackDirectory()
endcase
case "4"
UnpackAndDecrypt()
endcase
endselect
endfunction
function EncryptFile()
for EncryptFile_Num = 1 to 30
if not file open(EncryptFile_Num) then open to read EncryptFile_Num, "test01.txt"
next EncryptFile_Num
`------find available memblocks and file numbers--------
for EncryptFile_Num = 1 to 32
if not file open(EncryptFile_Num) then exit
if file open(EncryptFile_Num) and EncryptFile_Num = 32
print "ERROR: There are no available file slots for the read file."
wait 1000
exitfunction
endif
next EncryptFile_Num
make file "temp_file_delete."+str$(EncryptFile_Num)
open to read EncryptFile_Num, "temp_file_delete."+str$(EncryptFile_Num)
for FileToWrite_Num = 1 to 32
if not file open(FileToWrite_Num) then exit
if file open(FileToWrite_Num) and FileToWrite_Num = 32
print "ERROR: There are no available file slots for the write file."
wait 1000
close file EncryptFile_Num
open to write EncryptFile_Num, "temp_file_delete."+str$(EncryptFile_Num)
delete file "temp_file_delete."+str$(EncryptFile_Num)
exitfunction
endif
next FileToWrite_Num
close file EncryptFile_Num
open to write EncryptFile_Num, "temp_file_delete."+str$(EncryptFile_Num)
delete file "temp_file_delete."+str$(EncryptFile_Num)
for EncryptMemblock_Num = 1 to 255
if not memblock exist(EncryptMemblock_Num) then exit
if memblock exist(EncryptMemblock_Num) and EncryptMemblock_Num = 255
print "ERROR: There are no available memory blocks."
wait 1000
exitfunction
endif
next EncryptMemblock_Num
make memblock EncryptMemblock_Num, 1
for Memblock01_Num = 1 to 255
if not memblock exist(Memblock01_Num) then exit
if memblock exist(Memblock01_Num) and Memblock01_Num = 255
print "ERROR: There are no available memory blocks."
wait 1000
exitfunction
endif
next Memblock01_Num
make memblock Memblock01_Num, 1
for Memblock02_Num = 1 to 255
if not memblock exist(Memblock02_Num) then exit
if memblock exist(Memblock02_Num) and Memblock02_Num = 255
print "ERROR: There are no available memory blocks."
wait 1000
exitfunction
endif
next Memblock02_Num
delete memblock EncryptMemblock_Num
delete memblock Memblock01_Num
`---------------Get file to encrypt----------------------------
print "File encyption"
repeat
input "Enter filename and relative directory: ", filename$
if not file exist(filename$) //or lower$(filename$) <> "q"
print "Error: file does not exist. Enter Q to return to Main Menu."
endif
if lower$(filename$) = "q" then exitfunction
until file exist(filename$)
`-----------------Encyption process----------------------------
open to read EncryptFile_Num , filename$
make memblock from file EncryptMemblock_Num , EncryptFile_Num
close file EncryptFile_Num
EncryptMemblock_Size = file size(filename$)
Memblock01_Size = EncryptMemblock_Size / 2
Memblock02_Size = EncryptMemblock_Size - Memblock01_Size
make memblock Memblock01_Num, Memblock01_Size
make memblock Memblock02_Num, Memblock02_Size
copy memblock EncryptMemblock_Num, Memblock01_Num, 0,0, Memblock01_Size
copy memblock EncryptMemblock_Num, Memblock02_Num, Memblock01_Size,0, Memblock02_Size
delete memblock EncryptMemblock_Num
make memblock EncryptMemblock_Num, (Memblock01_Size + Memblock02_Size+4)
write memblock word EncryptMemblock_Num, 0, Memblock02_Size
copy memblock Memblock02_Num, EncryptMemblock_Num, 0,4, Memblock02_Size
copy memblock Memblock01_Num, EncryptMemblock_Num, 0,Memblock02_Size+4, Memblock01_Size
NewFile$ = left$(filename$,len(filename$)-3)+"ENC"
close file FileToWrite_Num
if file exist(NewFile$) then delete file NewFile$
open to write FileToWrite_Num, NewFile$
make file from memblock FileToWrite_Num, EncryptMemblock_Num
close file FileToWrite_Num
wait 1000
if file exist(NewFile$)
print "Encypted file, "+NewFile$+", has been created."
else
print "ERROR: Encrypted file not written."
endif
if EncryptMemblock_Size > file size(NewFile$)-4 then print "ERROR: Original file larger than new file, bytes have been lost."
if EncryptMemblock_Size < file size(NewFile$)-4 then print "ERROR: New file larger than original file, bytes have been gained."
print "Press any key to continue." : wait key
`-------Cleanup---------------
delete memblock Memblock01_Num
delete memblock Memblock02_Num
delete memblock EncryptMemblock_Num
endfunction
function DecryptFile()
for c = 1 to 32
if file open (c) then close file c
next c
for c = 1 to 255
if memblock exist(c) then delete memblock c
next c
`---------------Get file to Decrypt----------------------------
print "File Decryption"
repeat
input "Enter filename and relative directory: ", filename$
if not file exist(filename$) //or lower$(filename$) <> "q"
print "Error: file does not exist. Enter Q to return to Main Menu."
endif
if lower$(filename$) = "q" then exitfunction
until file exist(filename$)
open to read 1,filename$
read word 1, SplitLocation
make memblock from file 1,1
NewFile_Size = file size(filename$)-4
make memblock 2, NewFile_Size
copy memblock 1,2, SplitLocation+2, 0, NewFile_Size-SplitLocation
copy memblock 1,2, 3,NewFile_Size-SplitLocation, SplitLocation-1
NewFile$ = left$(filename$,len(filename$)-3)+"UNC"
if file exist(NewFile$) then delete file NewFile$
open to write 2, NewFile$
make file from memblock 2,2
print "File decrypted"
wait key
endfunction
function PackDirectory()
endfunction
function UnpackAndDecrypt()
endfunction
Thanks for the help!