Post by xcoder on Dec 12, 2020 19:30:50 GMT -5
print "##########################################"
dim K(4)
dim E1(20)
dim E2(20)
dim D1(20)
dim D2(20)
'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 "input data blocks"
ctr = 1
for i = 1 to len(text$) step 8
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
print ""
input "Press Enter to Decrypt >>";entry$
j = 1
do while j < ctr
'loop output of E array into Decrypt procedure
v0 = E1(j)
v1 = E2(j)
call Decrypt v0,v1,j
j = j + 1
loop
print ""
for j = 1 to ctr -1
print D1(j),D2(j) 'display D array (Decrypt)
next
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
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
sub text2Dec block$,ctr
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
sub Encrypt v0,v1,ctr
sum = 0 'set up;sum is zero
delta = 2654735769 'a key schedule constant
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 = 10618943076 ' set up; sum is (rounds * delta) important ***********
delta = 2654735769 ' 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
dim K(4)
dim E1(20)
dim E2(20)
dim D1(20)
dim D2(20)
'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 "input data blocks"
ctr = 1
for i = 1 to len(text$) step 8
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
print ""
input "Press Enter to Decrypt >>";entry$
j = 1
do while j < ctr
'loop output of E array into Decrypt procedure
v0 = E1(j)
v1 = E2(j)
call Decrypt v0,v1,j
j = j + 1
loop
print ""
for j = 1 to ctr -1
print D1(j),D2(j) 'display D array (Decrypt)
next
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
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
sub text2Dec block$,ctr
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
sub Encrypt v0,v1,ctr
sum = 0 'set up;sum is zero
delta = 2654735769 'a key schedule constant
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 = 10618943076 ' set up; sum is (rounds * delta) important ***********
delta = 2654735769 ' 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