Post by xcoder on Jun 12, 2021 20:59:06 GMT
print "###################################"
'four byte text block
'Three rounds of cipher per text block (three sub keys)
global two32,K1,K2,K3 '************ 'sub keys
two32 = 2^32
key = 2654435769
K1 = leftrotate(key,1)
K2 = leftrotate(key,4)
K3 = leftrotate(key,10)
dim B(4)
msg$ = "The quick brown fox jumps over the lazy dogs back"
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
t = 4 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(t) = ascii ' fill array B
t = t - 1
next j
a = B(4) * 256^3 '4 byte block
b = B(3) * 256^2
c = B(2) * 256^1
d = B(1) * 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)
t = 4 'max array number
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(t) = ascii ' fill array B
t = t - 1
next j
a = B(4) * 256^3 '4 byte block
b = B(3) * 256^2
c = B(2) * 256^1
d = B(1) * 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 K1 'xor 1st sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
Encrypt = Eround2(fff)
end function
function Eround2(a)
f = a xor 2^32-1 'invert all bits
ff = f xor K2 'xor second sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
Eround2 = Eround3(fff)
end function
function Eround3(a)
f = a xor 2^32-1 'invert all bits
ff = f xor K3 'xor third sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
print fff,decHex$(fff),dec2Bin$(fff)
Eround3 = fff
end function
print "===================================================="
'reverse order for decryption
function Decrypt(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
f = hexDec(swap$)
ff = f xor K3 'xor third sub key
fff = ff xor 2^32-1 'invert all bits
Decrypt = Dround2(fff)
end function
function Dround2(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
f = hexDec(swap$)
ff = f xor K2 'xor second sub key
fff = ff xor 2^32-1 'invert all bits
Dround2 = Dround3(fff)
end function
function Dround3(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4)
f = hexDec(swap$)
ff = f xor K1 'xor 1st sub key
fff = ff xor 2^32-1 'invert all bits
print fff,decHex$(fff),dec2Bin$(fff)
Dround3 = fff
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
'four byte text block
'Three rounds of cipher per text block (three sub keys)
global two32,K1,K2,K3 '************ 'sub keys
two32 = 2^32
key = 2654435769
K1 = leftrotate(key,1)
K2 = leftrotate(key,4)
K3 = leftrotate(key,10)
dim B(4)
msg$ = "The quick brown fox jumps over the lazy dogs back"
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
t = 4 'array index
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(t) = ascii ' fill array B
t = t - 1
next j
a = B(4) * 256^3 '4 byte block
b = B(3) * 256^2
c = B(2) * 256^1
d = B(1) * 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)
t = 4 'max array number
for j = 1 to len(chunk$)
ascii = asc(mid$(chunk$,j,1))
B(t) = ascii ' fill array B
t = t - 1
next j
a = B(4) * 256^3 '4 byte block
b = B(3) * 256^2
c = B(2) * 256^1
d = B(1) * 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 K1 'xor 1st sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
Encrypt = Eround2(fff)
end function
function Eround2(a)
f = a xor 2^32-1 'invert all bits
ff = f xor K2 'xor second sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
Eround2 = Eround3(fff)
end function
function Eround3(a)
f = a xor 2^32-1 'invert all bits
ff = f xor K3 'xor third sub key
hex$ = decHex$(ff)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
fff = hexDec(swap$)
print fff,decHex$(fff),dec2Bin$(fff)
Eround3 = fff
end function
print "===================================================="
'reverse order for decryption
function Decrypt(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
f = hexDec(swap$)
ff = f xor K3 'xor third sub key
fff = ff xor 2^32-1 'invert all bits
Decrypt = Dround2(fff)
end function
function Dround2(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4) 'transposition
f = hexDec(swap$)
ff = f xor K2 'xor second sub key
fff = ff xor 2^32-1 'invert all bits
Dround2 = Dround3(fff)
end function
function Dround3(a)
hex$ = decHex$(a)
swap$ = right$(hex$,4) + left$(hex$,4)
f = hexDec(swap$)
ff = f xor K1 'xor 1st sub key
fff = ff xor 2^32-1 'invert all bits
print fff,decHex$(fff),dec2Bin$(fff)
Dround3 = fff
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