Post by xcoder on Dec 17, 2020 12:53:22 GMT -5
'128 bit key/16 bytes
print "Four 32 bit/4 byte keys"
k0 = K(0)
k1 = K(1)
k2 = K(2)
k3 = K(3)
'bit shifting variables
v0sL4 = shiftL(v0,4)
v1sL4 = shiftL(v1,4)
v0sR5 = shiftR(v0,5)
v1sR5 = shiftR(v1,5)
'---------------------------------------------------------
call keys
text$ = "the quick brown fox jumps over the lazy dogs back"
print "the message length is ";len(text$)
print "input data blocks"
ctr = 1
for i = 1 to len(text$) step 8 '8 characters per block
block$ = mid$(text$,i,8)
call text2Dec block$,ctr
ctr = ctr + 1
next
print ""
print "Encrypted data blocks"
for i = 1 to ctr
print E1(i),E2(i)
next
'The contents of E1 and E2 Numeric arrays
'are written to a file.
print ""
input "Press Enter to Decrypt >>";entry$
j = 1
do while j < ctr
'loop output of E array into Decrypt procedure
'Simulates reading the Numeric contents of a file
'one line at a time into arrays D1 and D2
v0 = E1(j)
v1 = E2(j)
call Decrypt v0,v1,j
j = j + 1
loop
print ""
'---------------------------------------------------
for j = 1 to ctr 'number of data blocks
print D1(j),D2(j) 'display D array (Decrypt)
a = D1(j)
b = D2(j)
hex1$ = decHex$(a)
hex2$ = decHex$(b)
double$ = hex1$ + hex2$
hex3$ = hex3$ + double$
next j
for i = 1 to len(hex3$)step 2
chunk$ = mid$(hex3$,i,2)
ascii = hexdec(chunk$)
char$ = char$ + chr$(ascii)
next i
print char$
print:print
print "End of Code"
End
sub keys
randomize 0.22
for i = 0 to 3
x = int(rnd(1)* 2^32)
K(i) = x
print K(i)
next
print ""
end sub
sub text2Dec block$,ctr
x = len(block$)
if x < 8 then block$ = block$ + extra$(block$,x) 'pad the final block
sideL$ = left$(block$,4)
sideR$ = right$(block$,4)
x = 3 'initialize down counter for 4 byte array
for i = 1 to len(sideL$)
byte = asc(mid$(sideL$,i,1))
A(x) = byte * 256^x
word0 = word0 + A(x)
x = x - 1
next
x = 3 'initialize down counter for 4 byte array
for i = 1 to len(sideR$)
byte = asc(mid$(sideR$,i,1))
B(x) = byte * 256^x
word1 = word1 + B(x)
x = x - 1
next
v0 = word0
v1 = word1
print v0,v1
call Encrypt v0,v1,ctr
end sub
function extra$(a$,x)
if x < 8 then
dif = 8 - x
for i = 1 to dif
pad$ = pad$ + chr$(128)
next
end if
extra$ = pad$
end function
sub Encrypt v0,v1,ctr
sum = 0 'set up;sum is zero
delta = 2654435769 'a key schedule constant (golden ratio)
for i = 0 to 3 'basic cycle start
sum = sum + delta
v0 = (v0 + (v1sL4)+ k0) xor (v1 + sum) xor (v1sR5 + k1)
v1 = (v1 + (v0sL4) + k2) xor (v0 + sum) xor (v0sR5 + k3)
next
E1(ctr) = v0
E2(ctr) = v1
end sub
sub Decrypt v0,v1,ctr
sum = 10617743076 ' set up; sum is (rounds * delta) important ***********
delta = 2654435769 ' a key schedule constant
for i = 0 to 3 ' basic cycle start
v1 = (v1 - (v0sL4) + k2) xor (v0 + sum) xor (v0sR5 + k3)
v0 = (v0 - (v1sL4) + k0) xor (v1 + sum) xor (v1sR5 + k3)
sum = sum - delta
next
D1(ctr) = v0
D2(ctr) = v1
end sub
function shiftL(b,n)
shiftL = (b *2^n) and 2^32-1
end function
function shiftR(b,n)
shiftR = int(b /2^n)
end function
print "Four 32 bit/4 byte keys"
k0 = K(0)
k1 = K(1)
k2 = K(2)
k3 = K(3)
'bit shifting variables
v0sL4 = shiftL(v0,4)
v1sL4 = shiftL(v1,4)
v0sR5 = shiftR(v0,5)
v1sR5 = shiftR(v1,5)
'---------------------------------------------------------
call keys
text$ = "the quick brown fox jumps over the lazy dogs back"
print "the message length is ";len(text$)
print "input data blocks"
ctr = 1
for i = 1 to len(text$) step 8 '8 characters per block
block$ = mid$(text$,i,8)
call text2Dec block$,ctr
ctr = ctr + 1
next
print ""
print "Encrypted data blocks"
for i = 1 to ctr
print E1(i),E2(i)
next
'The contents of E1 and E2 Numeric arrays
'are written to a file.
print ""
input "Press Enter to Decrypt >>";entry$
j = 1
do while j < ctr
'loop output of E array into Decrypt procedure
'Simulates reading the Numeric contents of a file
'one line at a time into arrays D1 and D2
v0 = E1(j)
v1 = E2(j)
call Decrypt v0,v1,j
j = j + 1
loop
print ""
'---------------------------------------------------
for j = 1 to ctr 'number of data blocks
print D1(j),D2(j) 'display D array (Decrypt)
a = D1(j)
b = D2(j)
hex1$ = decHex$(a)
hex2$ = decHex$(b)
double$ = hex1$ + hex2$
hex3$ = hex3$ + double$
next j
for i = 1 to len(hex3$)step 2
chunk$ = mid$(hex3$,i,2)
ascii = hexdec(chunk$)
char$ = char$ + chr$(ascii)
next i
print char$
print:print
print "End of Code"
End
sub keys
randomize 0.22
for i = 0 to 3
x = int(rnd(1)* 2^32)
K(i) = x
print K(i)
next
print ""
end sub
sub text2Dec block$,ctr
x = len(block$)
if x < 8 then block$ = block$ + extra$(block$,x) 'pad the final block
sideL$ = left$(block$,4)
sideR$ = right$(block$,4)
x = 3 'initialize down counter for 4 byte array
for i = 1 to len(sideL$)
byte = asc(mid$(sideL$,i,1))
A(x) = byte * 256^x
word0 = word0 + A(x)
x = x - 1
next
x = 3 'initialize down counter for 4 byte array
for i = 1 to len(sideR$)
byte = asc(mid$(sideR$,i,1))
B(x) = byte * 256^x
word1 = word1 + B(x)
x = x - 1
next
v0 = word0
v1 = word1
print v0,v1
call Encrypt v0,v1,ctr
end sub
function extra$(a$,x)
if x < 8 then
dif = 8 - x
for i = 1 to dif
pad$ = pad$ + chr$(128)
next
end if
extra$ = pad$
end function
sub Encrypt v0,v1,ctr
sum = 0 'set up;sum is zero
delta = 2654435769 'a key schedule constant (golden ratio)
for i = 0 to 3 'basic cycle start
sum = sum + delta
v0 = (v0 + (v1sL4)+ k0) xor (v1 + sum) xor (v1sR5 + k1)
v1 = (v1 + (v0sL4) + k2) xor (v0 + sum) xor (v0sR5 + k3)
next
E1(ctr) = v0
E2(ctr) = v1
end sub
sub Decrypt v0,v1,ctr
sum = 10617743076 ' set up; sum is (rounds * delta) important ***********
delta = 2654435769 ' a key schedule constant
for i = 0 to 3 ' basic cycle start
v1 = (v1 - (v0sL4) + k2) xor (v0 + sum) xor (v0sR5 + k3)
v0 = (v0 - (v1sL4) + k0) xor (v1 + sum) xor (v1sR5 + k3)
sum = sum - delta
next
D1(ctr) = v0
D2(ctr) = v1
end sub
function shiftL(b,n)
shiftL = (b *2^n) and 2^32-1
end function
function shiftR(b,n)
shiftR = int(b /2^n)
end function