xcoder
Member in Training
Posts: 56
|
Post by xcoder on Dec 24, 2022 20:36:14 GMT
print "###############################" dim A$(40) dim K1(4) dim K2(4) dim D(4) dim Sum(4) global key$, two32 two32 = 2^ 32 key$ = "9e3779b9" 'hex call subKey print "" text$ = "the quick brown fox jumps over the laxy dogs back testing testing testing" alpha$ = main$(text$) print alpha$ print "------------------------------------------" input "Press Enter to continue: ";entry$ bravo$ = rxMain$(alpha$) print bravo$
print:print print "Finished" End
sub subKey num = hexDec(key$) alpha = leftRotate(num,1) hexKey1$ = decHex$(alpha) print hexKey1$, "subKey 1" bravo = leftRotate(num,7) hexKey2$ = decHex$(bravo) print hexKey2$, "subKey2" print "" x = 1 for i = 1 to len(hexKey1$)step 2 chunk$ = mid$(hexKey1$,i,2) byte = hexDec(chunk$) K1(x) = byte print K1(x) x = x + 1 next i print "" x = 1 for i = 1 to len(hexKey2$)step 2 chunk$ = mid$(hexKey2$,i,2) byte = hexDec(chunk$) K2(x) = byte print K2(x) x = x + 1 next i end sub
' rotates bits left n times function leftRotate(num,times) num = num mod two32 r = (num * 2^ times) mod two32 l = int(num /(2^ (32 - times))) leftRotate = r + l end function
function main$(text$) x = 1 for i = 1 to len(text$) step 4 chunk$ = mid$(text$,i,4) y = len(chunk$) if y < 4 then chunk$ = chunk$ + addPad$(y) A$(x) = chunk$ '********************* print A$(x), x outStr$ = outStr$ + send11$(chunk$) 'envoke the rounds of cipher x = x + 1 'number of text blocks next i main$ = outStr$ end function
function addPad$(x) 'pads the final text block if x < 4 then dif = 4 - x for i = 1 to dif pad$ = pad$ + chr$(128) next i end if addPad$ = pad$ end function
function send11$(in$) perm$ = right$(in$,2) + left$(in$,2) 'permutation 1st round send11$ = txMath11$(perm$) end function
function txMath11$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K1(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i txMath11$ = send22$(char$) end function
function send22$(in$) perm$ = right$(in$,1) + left$(in$,3) 'permutation 2nd round send22$ = txMath22$(perm$) end function
function txMath22$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K2(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i txMath22$ = char$ end function print "-----------------------------------------------------------------"
function rxMain$(text$) x = 1 for i = 1 to len(text$) step 4 chunk$ = mid$(text$,i,4) y = len(chunk$) A$(x) = chunk$ '********************* print A$(x), x outStr$ = outStr$ + rxMath11$(chunk$) x = x + 1 'number of text blocks next i rxMain$ = outStr$ end function
function rxMath11$(in$) 'reverse function sequence for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K2(i) xor D(i) 'reverse key sequence char$ = char$ + chr$(Sum(i)) next i rxMath11$ = receive11$(char$) end function
function receive11$(in$) perm$ = right$(in$,3) + left$(in$,1) 'reverse permutation sequence receive11$ = rxMath22$(perm$) end function
function rxMath22$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K1(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i rxMath22$ = receive22$(char$) end function
function receive22$(in$) perm$ = right$(in$,2) + left$(in$,2) receive22$ = perm$ end function
|
|
|
Post by Rod on Dec 24, 2022 20:44:24 GMT
Nice, but it needs an explanation else folks will just shy away.
|
|
xcoder
Member in Training
Posts: 56
|
Post by xcoder on Dec 24, 2022 23:19:50 GMT
I divide a string of characters into blocks of 4 characters. The character blocks have been numbered. Each block of characters will be go throught 2 rounds of cipering.
The ciphering consist of rearranging the characters (bytes) to create a permutation. The permutated text is converted to ascii then Xored with a sub key using parallel array technique. Sub keys are obtained by rotating a 32 bit integer N amount of bits, converting to hex string, then converting to 4 bytes of asciil.
More complex algorithms will be explored in the future.
|
|
|
Post by xxgeek on Dec 26, 2022 16:10:35 GMT
I like these encryption programs you post xcoder.
There seems to be a problem with this one. It leaves extra characters at the end of the decrypted text.
I've created a GUI for your code.
' Program to encrypt text in blocks ' written by xcoder Dec 2022 ' GUI added by xxgeek
dim A$(100000) : dim K1(4) : dim K2(4) : dim D(4) : dim Sum(4) WindowWidth = 640 : WindowHeight = 480 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2) texteditor #main.text, 200, 20, 400, 350 button #main.enc, "Encrypt", [encrypt], ul, 20, 95, 100, 20 button #main.dec , "Decrypt", [decrypt], ul, 20, 125, 100, 20 textbox #main.key, 10, 45, 150, 20 textbox #main.hide, 0, 0, 0, 0 statictext #main.editor, "Type or Paste Your Message Here", 315, 5, 190, 15 statictext #main.encKey, "Enter Encryption Key (HEX)", 25, 30, 160, 15 Open "untiltled" for window as #main #main "trapclose [quit]" wait
[encrypt] redim A$(100000) : dim K1(4) : dim K2(4) : dim D(4) : dim Sum(4) global key$, two32, text$ two32 = 2^ 32 #main.key "!contents? key$" #main.text "!contents? text$" call subKey alpha$ = text$ #main.text "!cls" #main.text main$(alpha$) wait
[decrypt] #main.text "!contents? text$" : print "text$ = ";text$ #main.text "!cls" alpha$ = text$ bravo$ = rxMain$(alpha$) #main.text bravo$ wait
[quit] close #main end
sub subKey num = hexDec(key$) alpha = leftRotate(num,1) hexKey1$ = decHex$(alpha) bravo = leftRotate(num,7) hexKey2$ = decHex$(bravo) x = 1 for i = 1 to len(hexKey1$)step 2 chunk$ = mid$(hexKey1$,i,2) byte = hexDec(chunk$) K1(x) = byte x = x + 1 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 two32 r = (num * 2^ times) mod two32 l = int(num /(2^ (32 - times))) leftRotate = r + l end function
function main$(text$) x = 1 for i = 1 to len(text$) step 4 chunk$ = mid$(text$,i,4) y = len(chunk$) if y < 4 then chunk$ = chunk$ + addPad$(y) A$(x) = chunk$ '********************* outStr$ = outStr$ + send11$(chunk$) 'envoke the rounds of cipher x = x + 1 'number of text blocks next i main$ = outStr$ end function
function addPad$(x) 'pads the final text block if x < 4 then dif = 4 - x for i = 1 to dif pad$ = pad$ + chr$(128) next i end if addPad$ = pad$ end function
function send11$(in$) perm$ = right$(in$,2) + left$(in$,2) 'permutation 1st round send11$ = txMath11$(perm$) end function
function txMath11$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K1(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i txMath11$ = send22$(char$) end function
function send22$(in$) perm$ = right$(in$,1) + left$(in$,3) 'permutation 2nd round send22$ = txMath22$(perm$) end function
function txMath22$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K2(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i txMath22$ = char$ end function
function rxMain$(text$) x = 1 for i = 1 to len(text$) step 4 chunk$ = mid$(text$,i,4) y = len(chunk$) A$(x) = chunk$ '********************* outStr$ = outStr$ + rxMath11$(chunk$) x = x + 1 'number of text blocks next i rxMain$ = outStr$ end function
function rxMath11$(in$) 'reverse function sequence for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K2(i) xor D(i) 'reverse key sequence char$ = char$ + chr$(Sum(i)) next i rxMath11$ = receive11$(char$) end function
function receive11$(in$) perm$ = right$(in$,3) + left$(in$,1) 'reverse permutation sequence receive11$ = rxMath22$(perm$) end function
function rxMath22$(in$) for i = 1 to len(in$) byte = asc(mid$(in$,i,1)) D(i) = byte Sum(i) = K1(i) xor D(i) char$ = char$ + chr$(Sum(i)) next i rxMath22$ = receive22$(char$) end function
function receive22$(in$) perm$ = right$(in$,2) + left$(in$,2) receive22$ = perm$ end function
|
|