Post by xcoder on Feb 11, 2023 22:25:27 GMT
print "###############################"
dim A$(100), K1(8), K2(8), D(8), Sum(8), Sbox1(8), Sbox2(8)
print "Sbox1"
data 8, 64, 16, 128, 0, 0, 0, 0
for i = 1 to 8
read alpha
Sbox1(i) = alpha ''single bit flip in each byte
print Sbox1(i);" ";
next i
print:print
data 128, 16, 64, 8, 0, 0, 0, 0
print "Sbox2"
for i = 1 to 8
read alpha
Sbox2(i) = alpha ''single bit flip in each byte
print Sbox2(i);" ";
next i
print:print
global key$, two64
two64 = 2^ 64
key$ = "9e3779b9a1b2c3d" 'hex 8 bytes (64 bits)
call subKey
print ""
text$ = "the quick brown fox jumps over the lazy dogs back"
alpha$ = txMain$(text$)
'print alpha$ 'Actual string: it is a continuous hex string
for j = 1 to len(alpha$) step 16
chunk$ = mid$(alpha$,j,16) + chr$(13) + chr$(10)
print chunk$ ' Display string: 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
function txMain$(text$)
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
y = len(chunk$)
if y < 8 then chunk$ = chunk$ + addPad$(y)
outStr$ = outStr$ + txRound1$(chunk$) 'envoke the rounds of cipher
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$)
sideL$ = right$(in$,4)
sideR$ = left$(in$,4)
perm$ = sideL$ + sideR$
for i = 1 to 4
byte = asc(mid$(perm$,i,1)) 'Feistel technique
D(i) = byte
Sum(i) = K1(i) xor D(i) xor Sbox1(i) 'key xor 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 Sbox2(i)
char$ = char$ + chr$(Sum(i))
next i
swap$ = char$ + sideR$
txRound2$ = text2Hex$(swap$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$)
byte = asc(mid$(cipher$,i,1))
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "-----------------------------------------------------------------"
''' Decryption Process
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$)
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
outStr$ = outStr$ + rxRound1$(chunk$)
next i
rxMain$ = outStr$
end function
function rxRound1$(in$) 'reverse function sequence
sideR$ = right$(in$,4) 'unprocessed original left half
for i = 1 to 4
byte = asc(mid$(in$,i,1))
D(i) = byte
Sum(i) = K2(i) xor D(i) xor Sbox2(i) 'reverse key sequence
char$ = char$ + chr$(Sum(i))
next i
swap$ = sideR$ + char$
rxRound1$ = rxRound2$(swap$) 'combine unprocessed original left half
end function 'and processed original right half
function rxRound2$(in$)
sideR$ = right$(in$,4)
for i = 1 to 4
byte = asc(mid$(in$,i,1))
D(i) = byte
Sum(i) = K1(i) xor D(i) xor Sbox1(i) 'reverse key sequence
char$ = char$ + chr$(Sum(i))
next i
swap$ = sideR$ + char$
rxRound2$ = swap$
end function
dim A$(100), K1(8), K2(8), D(8), Sum(8), Sbox1(8), Sbox2(8)
print "Sbox1"
data 8, 64, 16, 128, 0, 0, 0, 0
for i = 1 to 8
read alpha
Sbox1(i) = alpha ''single bit flip in each byte
print Sbox1(i);" ";
next i
print:print
data 128, 16, 64, 8, 0, 0, 0, 0
print "Sbox2"
for i = 1 to 8
read alpha
Sbox2(i) = alpha ''single bit flip in each byte
print Sbox2(i);" ";
next i
print:print
global key$, two64
two64 = 2^ 64
key$ = "9e3779b9a1b2c3d" 'hex 8 bytes (64 bits)
call subKey
print ""
text$ = "the quick brown fox jumps over the lazy dogs back"
alpha$ = txMain$(text$)
'print alpha$ 'Actual string: it is a continuous hex string
for j = 1 to len(alpha$) step 16
chunk$ = mid$(alpha$,j,16) + chr$(13) + chr$(10)
print chunk$ ' Display string: 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
function txMain$(text$)
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
y = len(chunk$)
if y < 8 then chunk$ = chunk$ + addPad$(y)
outStr$ = outStr$ + txRound1$(chunk$) 'envoke the rounds of cipher
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$)
sideL$ = right$(in$,4)
sideR$ = left$(in$,4)
perm$ = sideL$ + sideR$
for i = 1 to 4
byte = asc(mid$(perm$,i,1)) 'Feistel technique
D(i) = byte
Sum(i) = K1(i) xor D(i) xor Sbox1(i) 'key xor 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 Sbox2(i)
char$ = char$ + chr$(Sum(i))
next i
swap$ = char$ + sideR$
txRound2$ = text2Hex$(swap$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$)
byte = asc(mid$(cipher$,i,1))
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "-----------------------------------------------------------------"
''' Decryption Process
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$)
for i = 1 to len(text$) step 8
chunk$ = mid$(text$,i,8)
outStr$ = outStr$ + rxRound1$(chunk$)
next i
rxMain$ = outStr$
end function
function rxRound1$(in$) 'reverse function sequence
sideR$ = right$(in$,4) 'unprocessed original left half
for i = 1 to 4
byte = asc(mid$(in$,i,1))
D(i) = byte
Sum(i) = K2(i) xor D(i) xor Sbox2(i) 'reverse key sequence
char$ = char$ + chr$(Sum(i))
next i
swap$ = sideR$ + char$
rxRound1$ = rxRound2$(swap$) 'combine unprocessed original left half
end function 'and processed original right half
function rxRound2$(in$)
sideR$ = right$(in$,4)
for i = 1 to 4
byte = asc(mid$(in$,i,1))
D(i) = byte
Sum(i) = K1(i) xor D(i) xor Sbox1(i) 'reverse key sequence
char$ = char$ + chr$(Sum(i))
next i
swap$ = sideR$ + char$
rxRound2$ = swap$
end function