Post by xcoder on Mar 31, 2021 0:25:42 GMT
print "###################################"
global two32,kky1,kky2,kky3 '************
two32 = 2^32
key = 2654435769
kky1 = leftrotate(key,1)
kky2 = leftrotate(key,4)
kky3 = leftrotate(key,10)
dim B(4)
msg$ = "The quick brown fox jumps over the lazy dogs back";chr$(13)+_
"Testing the GetSmart block cipher in JustBasic 2.0"
print msg$
print "Length of message is ";len(msg$)
for i = 1 to len(msg$) step 4
chunk$ = mid$(msg$,i,4)
x = len(chunk$)
if x < 4 then chunk$ = chunk$ + extra$(chunk$,x)'pad the final block
k = 3 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(k) = ascii ' fill array B
k = k - 1
next j
a = B(3) * 256^3 '4 byte block
b = B(2) * 256^2
c = B(1) * 256^1
d = B(0) * 256^0
sum = a + b + c + d
numCrypt = Encrypt(sum)
result$ = result$ + dec2Text$(numCrypt)
next i
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$
print ""
for i = 1 to len(msg$) step 4
chunk$ = mid$(msg$,i,4)
k = 3 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(k) = ascii ' fill array B
k = k - 1
next j
a = B(3) * 256^3 '4 byte block
b = B(2) * 256^2
c = B(1) * 256^1
d = B(0) * 256^0
sum = a + b + c + d
numCrypt = Decrypt(sum)
plain$ = plain$ + dec2Text$(numCrypt)
next i
print plain$
print:print
print "End of Code"
End
function extra$(a$,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
extra$ = pad$
end function
function Encrypt(a)
print
f = a xor 2^32-1 'invert all bits
ff = f xor kky1 'use 1st sub key
print ff,decHex$(ff),dec2Bin$(ff)
Encrypt = Round2(ff)
end function
function Round2(a)
f = a xor kky2 'use 2nd sub key
print f,decHex$(f),dec2Bin$(f)
Round2 = Round3(f)
end function
function Round3(a)
f = a xor kky3 'use 3rd sub key
print f,decHex$(f),dec2Bin$(f)
Round3 = f:print
end function
print "===================================================="
function Decrypt(a)
print
f = a xor kky3 'reverse order of sub keys
print f,decHex$(f),dec2Bin$(f)
Decrypt = revRound2(f)
end function
function revRound2(a)
f = a xor kky2
print f,decHex$(f),dec2Bin$(f)
revRound2 = revRound3(f)
end function
function revRound3(a)
f = a xor kky1
ff = f xor 2^32-1 'invert all bits
print ff,decHex$(ff),dec2Bin$(ff)
revRound3 = ff
end function
' leftrotate: spins 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 dec2Text$(dec) 'outputs cipher text and plain text
hex$ = decHex$(dec)
for i = 1 to len(hex$)step 2
chunk$ = mid$(hex$,i,2)
ascii = hexDec(chunk$)
char$ = char$ + chr$(ascii)
next i
dec2Text$ = char$
end function
function dec2Bin$(num)
dec2Bin$ =""
while (num >0)
dec2Bin$ =str$( num mod 2) +dec2Bin$
num =int(num /2)
wend
dec2Bin$ =right$( "00000000" +dec2Bin$, 32)
end function
global two32,kky1,kky2,kky3 '************
two32 = 2^32
key = 2654435769
kky1 = leftrotate(key,1)
kky2 = leftrotate(key,4)
kky3 = leftrotate(key,10)
dim B(4)
msg$ = "The quick brown fox jumps over the lazy dogs back";chr$(13)+_
"Testing the GetSmart block cipher in JustBasic 2.0"
print msg$
print "Length of message is ";len(msg$)
for i = 1 to len(msg$) step 4
chunk$ = mid$(msg$,i,4)
x = len(chunk$)
if x < 4 then chunk$ = chunk$ + extra$(chunk$,x)'pad the final block
k = 3 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(k) = ascii ' fill array B
k = k - 1
next j
a = B(3) * 256^3 '4 byte block
b = B(2) * 256^2
c = B(1) * 256^1
d = B(0) * 256^0
sum = a + b + c + d
numCrypt = Encrypt(sum)
result$ = result$ + dec2Text$(numCrypt)
next i
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$
print ""
for i = 1 to len(msg$) step 4
chunk$ = mid$(msg$,i,4)
k = 3 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(k) = ascii ' fill array B
k = k - 1
next j
a = B(3) * 256^3 '4 byte block
b = B(2) * 256^2
c = B(1) * 256^1
d = B(0) * 256^0
sum = a + b + c + d
numCrypt = Decrypt(sum)
plain$ = plain$ + dec2Text$(numCrypt)
next i
print plain$
print:print
print "End of Code"
End
function extra$(a$,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
extra$ = pad$
end function
function Encrypt(a)
f = a xor 2^32-1 'invert all bits
ff = f xor kky1 'use 1st sub key
print ff,decHex$(ff),dec2Bin$(ff)
Encrypt = Round2(ff)
end function
function Round2(a)
f = a xor kky2 'use 2nd sub key
print f,decHex$(f),dec2Bin$(f)
Round2 = Round3(f)
end function
function Round3(a)
f = a xor kky3 'use 3rd sub key
print f,decHex$(f),dec2Bin$(f)
Round3 = f:print
end function
print "===================================================="
function Decrypt(a)
f = a xor kky3 'reverse order of sub keys
print f,decHex$(f),dec2Bin$(f)
Decrypt = revRound2(f)
end function
function revRound2(a)
f = a xor kky2
print f,decHex$(f),dec2Bin$(f)
revRound2 = revRound3(f)
end function
function revRound3(a)
f = a xor kky1
ff = f xor 2^32-1 'invert all bits
print ff,decHex$(ff),dec2Bin$(ff)
revRound3 = ff
end function
' leftrotate: spins 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 dec2Text$(dec) 'outputs cipher text and plain text
hex$ = decHex$(dec)
for i = 1 to len(hex$)step 2
chunk$ = mid$(hex$,i,2)
ascii = hexDec(chunk$)
char$ = char$ + chr$(ascii)
next i
dec2Text$ = char$
end function
function dec2Bin$(num)
dec2Bin$ =""
while (num >0)
dec2Bin$ =str$( num mod 2) +dec2Bin$
num =int(num /2)
wend
dec2Bin$ =right$( "00000000" +dec2Bin$, 32)
end function