xcoder
Member in Training
Posts: 56
|
Post by xcoder on Feb 4, 2023 10:15:47 GMT
Now that is the style of Elon Musk. Never give up! My code was not the best, but I am moving forward.
|
|
|
Post by xxgeek on Feb 4, 2023 20:32:08 GMT
Now that is the style of Elon Musk. Never give up! My code was not the best, but I am moving forward. I have some bad news. I was fooled by my own code again. Your code was ok, however you had everything coded twice, and there was no need. As you will see I have removed half the functions. There was no need to use separate functions for encrypt and decrypt since they were both doing the exact same thing to the text sent to them. The bad news - After checking my code again this morning, I realized I had left the code in that was getting the text from the file, and not what I was pasting into the editor so I cannot paste encrypted text into the editor and have it come back intact when decrypted, but that's not your codes fault . Well not exactly anyway. Rod is right I believe, and to have the encrypted text able to be decrypted when pasting into the editor you will have to stick to ASCII characters from 32 - 127. It has got to be the problem, I have done so many tests I don't believe it could be anything else BUT that, that's all that is left it COULD be. The good news is we can send the encrypted text to a file, and then reintroduce it to the editor, even after closing the app. So we CAN use this in code, and we can use it between 2 different users by sending the file it creates. Keep in mind this app 'as is' is just the app for testing and not the finished product yet. We will need to save every A$() array to a file as well as the encrypted text, and the key used to create it and reunite them with each other to decrypt. I believe I can do that, it will be a while though, I'm working on quite a few apps presently. Here's the code that is working. Play with it. If the encrypted file exists on startup, it copies to text editor and can be decrypted. If the file does not exist, you get an empty editor to paste any unencrypted text, ready for encryption. There are buttons to copy text in editor, and one to delete the encrypted file to start fresh if desired. 'Text Encryption 'author = xcoder 'Date Feb 2023 'Purpose - To encrypt blocks of text, for example Just Basic Programs ' Just paste your code into the text editor and press encrypt /decrypt button 'to decrypt, press the same button nomainwin global key$, two64 two64 = 2^64 dim A$(10) WindowWidth = 600 : WindowHeight = 400 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2) texteditor #main.text, 200, 20, 360, 320 button #main.enc, "Encrypt", [encrypt], ul, 55, 115, 110, 25 button #main.dec, "Decrypt", [decrypt], ul, 55, 155, 110, 25 button #main.clearKey , "Clear Key", [clearKey], ul, 30, 75, 70, 20 button #main.dec , "Clear Text", [clearText], ul, 110, 75, 70, 20 button #main.refresh , "Delete Encrypted File", [refresh], ul, 40, 200, 135, 25 button #main.copytext , "Copy Text", [copytext], ul, 350, 340, 60, 25 textbox #main.key, 30, 45, 150, 20 textbox #main.subkey1, 30, 270, 150, 20 textbox #main.subkey2, 30, 320, 150, 20 statictext #main.editor, "Type or Paste Text Here", 335, 5, 190, 15 statictext #main.sk1T, "SubKey 1", 85, 255, 60, 15 statictext #main.sk2T, "SubKey 2", 85, 305, 60, 15 statictext #main.encKey, "Enter Encryption Key (HEX)", 55, 30, 110, 15 Open "Encryptor by xcoder" for dialog as #main #main "trapclose [quit]" key$ = "9e3779b9a1b2c3d4" 'hex 8 bytes (64 bits) '- original code key value = 16 chars #main.key key$ if fileExists(DefaultDir$, "key") then open "key" for binary as #1 #main.text input$(#1, lof(#1)); close #1 #main.text "!origin 0 0" end if wait
[encrypt] if cript = 1 then [decrypt] #main.key "!contents? key$" data 8, 64, 16, 128, 0, 0, 0, 0 for i = 1 to 8 read alpha Sbox(i) = alpha ''single bit flip in each byte next i call subKey #main.text "!contents? text$" text$ = trim$(text$) redim A$(len(text$)) alpha$ = txMain$(text$) alpha$ = trim$(alpha$) #main.text "!cls" cript = 1 #main.text alpha$ open "key" for binary as #1 #1 alpha$ close #1 restore #main.text "!origin 0 0" wait
[decrypt] #main.key "!contents? key$" if cript = 0 then if fileExists(DefaultDir$, "key") then open "key" for binary as #1 alpha$ = input$(#1, lof(#1)) close #1 else '############################################################################# tooBADsoSAD = 1 '#main.text "!contents? alpha$" 'this doesn't work when pasting encrypted text, as expected. '############################################################################# end if redim A$(len(alpha$)) open "array" for input as #1 while eof(#1) = 0 x = x + 1 line input #1, line$ A$(x) = line$ wend close #1 end if call subKey data 8, 64, 16, 128, 0, 0, 0, 0 for i = 1 to 8 read alpha Sbox(i) = alpha ''single bit flip in each byte next i alpha$ = trim$(alpha$) bravo$ = txMain$(alpha$) bravo$ = trim$(bravo$) #main.text "!cls" #main.text bravo$ cript = 0 restore #main.text "!origin 0 0" wait
[clearKey] #main.key "" wait
[clearText] #main.text "!cls" wait
[refresh] if fileExists(DefaultDir$, "key") then kill DefaultDir$;"\key" #main.text "!cls" wait
[copytext] #main.text "!selectall" #main.text "!copy" wait
[quit] close #main End
sub subKey num = hexDec(key$) alpha = leftRotate(num,1) hexKey1$ = decHex$(alpha) #main.subkey1 hexKey1$ bravo = leftRotate(num,4) hexKey2$ = decHex$(bravo) #main.subkey2 hexKey2$ x = 1 for i = 1 to len(hexKey1$) step 2 chunk$ = mid$(hexKey1$,i,2) byte = hexDec(chunk$) K1(x) = byte next i x = 1 for i = 1 to len(hexKey2$)step 2 chunk$ = mid$(hexKey2$,i,2) byte = hexDec(chunk$) K2(x) = byte x = x + 1 next i end sub
' rotates bits left n times function leftRotate(num,times) num = num mod two64 r = (num * 2^ times) mod two64 l = int(num /(2^ (64 - times))) leftRotate = r + l end function
function txMain$(text$) open "array" for output as #1 x = 1 for i = 1 to len(text$) step 8 chunk$ = mid$(text$,i,8) y = len(chunk$) if y < 8 then chunk$ = chunk$ + addPad$(y) A$(x) = chunk$ '********************* #1 A$(x) outStr$ = outStr$ + txRound1$(chunk$) 'envoke the rounds of cipher x = x + 1 'number of text blocks next i close #1 txMain$ = outStr$ end function
function addPad$(x) 'pads the final text block if x < 8 then dif = 8 - x for i = 1 to dif pad$ = pad$ + chr$(0) next i end if addPad$ = pad$ end function
function txRound1$(in$) sideL$ = right$(in$,4) 'swap left and right halfs of data block sideR$ = left$(in$,4) perm$ = sideL$ + sideR$ for i = 1 to 4 'process index 1 to 4 of D (new right half of data block) byte = asc(mid$(perm$,i,1)) 'Feistel technique D(i) = byte Sum(i) = K1(i) xor D(i) xor Sbox(i) 'key data then Sbox flips one bit char$ = char$ + chr$(Sum(i)) next i swap$ = char$ + sideR$ 'combine processed half and plain half txRound1$ = txRound2$(swap$) end function
function txRound2$(in$) sideL$ = right$(in$,4) sideR$ = left$(in$,4) perm$ = sideL$ + sideR$ for i = 1 to 4 byte = asc(mid$(perm$,i,1)) D(i) = byte Sum(i) = K2(i) xor D(i) xor Sbox(i) char$ = char$ + chr$(Sum(i)) next i swap$ = char$ + sideR$ txRound2$ = swap$ end function
'function for checking file existence function fileExists(path$, filename$) dim info$(0, 0) files path$, filename$, info$() fileExists = val(info$(0, 0)) 'non zero is true end function
|
|
|
Post by xxgeek on Feb 4, 2023 21:32:04 GMT
I forgot to mention that you can change the key if desired. Just remember to use the same key when decrypting.
I've been brainstorming how to go about structuring the code for a final product. It depends on what it will be used for I guess. The use I am personally striving for is to use it in code to encrypt bas files on the fly. A filedialog to choose the file, and an editor to display the results or paste some text for encryption (dual use), with a listbox with the names of the "saved encrypted" files - when clicked unites the original key and the original A$() array with the selected Listing. Also a textbox for the user to create a name for the file created when pasted text is used instead of a file.
One thought has me concerned though. If the A$() array is copied 'and it is needed to decipher the original encrypted text' the unencrypted text is kind of there in the open and could be deciphered manually by any savvy tech type.
|
|
|
Post by xxgeek on Feb 6, 2023 1:45:44 GMT
One thought has me concerned though. If the A$() array is copied 'and it is needed to decipher the original encrypted text' the unencrypted text is kind of there in the open and could be deciphered manually by any savvy tech type. Wrong again, man I shouldn't post when I'm sleep deprived. The A$() array is totally unreadable, as it should be. I had done so many tests and read so many of the resulting saved A$() array files I thought that was the case because many of them showed much of the original code/text in each block, but when I see array files that ARE working properly the encrypted blocks ARE totally unreadable. Sorry about my inability to think straight when overtired. I am still baffled as to why 'pasting' encrypted text to an editor doesn't work, while moving data from an encrypted file to the editor does work. It just doesn't make any sense to me, even considering Rod's information on control characters being in a texteditor, because those "control character" are in the same text coming from the file. Rod, I'm not saying you are wrong, and you may very well be right, but I need to prove it to myself. I still think it could be my code not getting all the variables and I am still trying to prove it somehow works if I can get it right.
|
|