Post by xcoder on Dec 3, 2020 18:10:55 GMT -5
print "#########################################################"
global two32,key '************
two32 = 2^32
key = 0.26547358 'seeds PRNG
dim B(4)
call subkey
msg$ = "the quick brown fox jumps over the lazy dogs back"
print "Length of message is ";len(msg$)
print "Plain Block","Encrypted Block" '******
ctr = 0 'will count the data blocks
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
print sum, '********************
numCrypt = encrypt(sum,ctr)
ctr = ctr + 1 'increment data block counter
print numCrypt
result$ = result$ + hexAsc$(numCrypt)
next
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$ '***********
print msg$
print "Encrypted Block";space$(4);"Plain Block"
ctr = 0
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
print sum, '***************
numCrypt = decrypt(sum,ctr)
ctr = ctr + 1 'increment the data block counter
print numCrypt
plain$ = plain$ + hexAsc$(numCrypt)
next
print plain$
print:print
print "End of Code"
End
sub subkey
print "These are the sub keyS"
randomize key 'create sub keys
for i = 0 to 9
x = int(rnd(1)* 2^32) '32 bit integer
K(i) = x
print K(i)
next
end sub
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
end if
extra$ = pad$
end function
function encrypt(sum,ctr)
x = 9 'sets rotation
f$ = right$(str$(ctr),1)'ctr selects sub key 0 to 9 and repeats
i = val(f$)
a = sum xor K(i)
b = leftrotate(a,x)
encrypt = b
end function
function decrypt(sum,ctr)
x = 9 'sets rotation
f$ = right$(str$(ctr),1)
i = val(f$)
a = leftrotate(sum,32-x)
b = a xor K(i)
decrypt = b
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 hexAsc$(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
hexAsc$ = char$
end function
global two32,key '************
two32 = 2^32
key = 0.26547358 'seeds PRNG
dim B(4)
call subkey
msg$ = "the quick brown fox jumps over the lazy dogs back"
print "Length of message is ";len(msg$)
print "Plain Block","Encrypted Block" '******
ctr = 0 'will count the data blocks
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
print sum, '********************
numCrypt = encrypt(sum,ctr)
ctr = ctr + 1 'increment data block counter
print numCrypt
result$ = result$ + hexAsc$(numCrypt)
next
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$ '***********
print msg$
print "Encrypted Block";space$(4);"Plain Block"
ctr = 0
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
print sum, '***************
numCrypt = decrypt(sum,ctr)
ctr = ctr + 1 'increment the data block counter
print numCrypt
plain$ = plain$ + hexAsc$(numCrypt)
next
print plain$
print:print
print "End of Code"
End
sub subkey
print "These are the sub keyS"
randomize key 'create sub keys
for i = 0 to 9
x = int(rnd(1)* 2^32) '32 bit integer
K(i) = x
print K(i)
next
end sub
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
end if
extra$ = pad$
end function
function encrypt(sum,ctr)
x = 9 'sets rotation
f$ = right$(str$(ctr),1)'ctr selects sub key 0 to 9 and repeats
i = val(f$)
a = sum xor K(i)
b = leftrotate(a,x)
encrypt = b
end function
function decrypt(sum,ctr)
x = 9 'sets rotation
f$ = right$(str$(ctr),1)
i = val(f$)
a = leftrotate(sum,32-x)
b = a xor K(i)
decrypt = b
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 hexAsc$(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
hexAsc$ = char$
end function