Post by xcoder on Feb 22, 2023 3:54:41 GMT
print "###############################"
dim A$(100), K1(8), K2(8), D(8)
dim Fbox1(8), Fbox2(8)
global key$, two64
two64 = 2^ 64
key$ = "9e3779b9a1b2c3d4" 'hex 8 bytes (64 bits)
call subKey
call flipBox
print ""
text$ = "ABCDabcd12345678okayokay"
alpha$ = txMain$(text$)
'print alpha$ 'will display a continuous hex string (actual string)
for j = 1 to len(alpha$) step 16
chunk$ = mid$(alpha$,j,16) + chr$(13) + chr$(10)
print chunk$ 'will display block formatted hex string
next j
print "------------------------------------------"
input "Press Enter to continue: ";entry$
bravo$ = hex2Text$(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,4)
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 two64
r = (num * 2^ times) mod two64
l = int(num /(2^ (64 - times)))
leftRotate = r + l
end function
sub flipBox
data 128, 64, 32, 16, 8, 4, 2, 1 'odd block bit flips
for i = 1 to 8
read alpha
Fbox1(i) = alpha
next i
print:print
data 1, 2, 4, 8, 16, 32, 64, 128 'even block bit flips
for i = 1 to 8
read bravo
Fbox2(i) = bravo
next i
end sub
function txMain$(text$)
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$ '*********************
print "BLOCK "; x, A$(x)
outStr$ = outStr$ + txRound1$(chunk$) 'envoke the rounds of cipher
x = x + 1 'number of text blocks
next i
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$)
in$ = right$(in$,1) + left$(in$,7) '***********
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K1(i) xor Fbox1(i)
char$ = char$ + chr$(sum)
next i
txRound1$ = txRound2$(char$)
end function
function txRound2$(in$)
in$ = right$(in$,1) + left$(in$,7) '***********
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K2(i) xor Fbox2(i)
char$ = char$ + chr$(sum)
next i
print ''skip line
txRound2$ = text2Hex$(char$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$) 'encrypted text (for binary file)
byte = asc(mid$(cipher$,i,1)) 'encrypted ascii
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "############ Decryption Functions ############"
function hex2Text$(hex$)
for i = 1 to len(hex$)step 2 'encrypted hex
chunk$ = mid$(hex$,i,2)
byte = hexDec(chunk$) 'encrypted ascii
text$ = text$ + chr$(byte) 'encrypted text
next i
hex2Text$= rxMain$(text$)
end function
function rxMain$(text$)
x = 1
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
A$(x) = chunk$ '*********************
print "BLOCK "; x, A$(x)
outStr$ = outStr$ + rxRound2$(chunk$) 'envoke the rounds of cipher
x = x + 1 'reverse order
next i
rxMain$ = outStr$
end function
function rxRound2$(in$)
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K2(i) xor Fbox2(i)
char$ = char$ + chr$(sum)
next i
rxRound2$ = rxRound1$(right$(char$,7) + left$(char$,1)) '***************
end function
function rxRound1$(in$)
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K1(i) xor Fbox1(i)
char$ = char$ + chr$(sum)
next i
print ''skip line
rxRound1$ = right$(char$,7) + left$(char$,1) '***************
end function
dim A$(100), K1(8), K2(8), D(8)
dim Fbox1(8), Fbox2(8)
global key$, two64
two64 = 2^ 64
key$ = "9e3779b9a1b2c3d4" 'hex 8 bytes (64 bits)
call subKey
call flipBox
print ""
text$ = "ABCDabcd12345678okayokay"
alpha$ = txMain$(text$)
'print alpha$ 'will display a continuous hex string (actual string)
for j = 1 to len(alpha$) step 16
chunk$ = mid$(alpha$,j,16) + chr$(13) + chr$(10)
print chunk$ 'will display block formatted hex string
next j
print "------------------------------------------"
input "Press Enter to continue: ";entry$
bravo$ = hex2Text$(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,4)
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 two64
r = (num * 2^ times) mod two64
l = int(num /(2^ (64 - times)))
leftRotate = r + l
end function
sub flipBox
data 128, 64, 32, 16, 8, 4, 2, 1 'odd block bit flips
for i = 1 to 8
read alpha
Fbox1(i) = alpha
next i
print:print
data 1, 2, 4, 8, 16, 32, 64, 128 'even block bit flips
for i = 1 to 8
read bravo
Fbox2(i) = bravo
next i
end sub
function txMain$(text$)
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$ '*********************
print "BLOCK "; x, A$(x)
outStr$ = outStr$ + txRound1$(chunk$) 'envoke the rounds of cipher
x = x + 1 'number of text blocks
next i
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$)
in$ = right$(in$,1) + left$(in$,7) '***********
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K1(i) xor Fbox1(i)
char$ = char$ + chr$(sum)
next i
txRound1$ = txRound2$(char$)
end function
function txRound2$(in$)
in$ = right$(in$,1) + left$(in$,7) '***********
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K2(i) xor Fbox2(i)
char$ = char$ + chr$(sum)
next i
print ''skip line
txRound2$ = text2Hex$(char$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$) 'encrypted text (for binary file)
byte = asc(mid$(cipher$,i,1)) 'encrypted ascii
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "############ Decryption Functions ############"
function hex2Text$(hex$)
for i = 1 to len(hex$)step 2 'encrypted hex
chunk$ = mid$(hex$,i,2)
byte = hexDec(chunk$) 'encrypted ascii
text$ = text$ + chr$(byte) 'encrypted text
next i
hex2Text$= rxMain$(text$)
end function
function rxMain$(text$)
x = 1
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
A$(x) = chunk$ '*********************
print "BLOCK "; x, A$(x)
outStr$ = outStr$ + rxRound2$(chunk$) 'envoke the rounds of cipher
x = x + 1 'reverse order
next i
rxMain$ = outStr$
end function
function rxRound2$(in$)
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K2(i) xor Fbox2(i)
char$ = char$ + chr$(sum)
next i
rxRound2$ = rxRound1$(right$(char$,7) + left$(char$,1)) '***************
end function
function rxRound1$(in$)
for i = 1 to len(in$)
byte = asc(mid$(in$,i,1))
D(i) = byte
sum = D(i) xor K1(i) xor Fbox1(i)
char$ = char$ + chr$(sum)
next i
print ''skip line
rxRound1$ = right$(char$,7) + left$(char$,1) '***************
end function