Post by xcoder on Nov 25, 2020 7:38:56 GMT -5
print "#########################################################"
global two32,key '************
two32 = 2^32
key = 0.26547358 'seeds PRNG
dim A$(100) 'text blocks
dim B(4)
call subkey
msg$ = "the quick brown fox jumps over the lazy dogs back"
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 blocks
A$(ctr) = chunk$
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)
print numCrypt
result$ = result$ + hexAscii$(numCrypt)
next
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$ '***********
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 blocks
A$(ctr) = chunk$
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)
print numCrypt
plain$ = plain$ + hexAscii$(numCrypt)
next
print plain$
print:print
print "End of Code"
End
sub subkey
randomize key 'create sub keys
for i = 0 to 9
x = int(rnd(1)* 2^32) '32 bit integer
K(i) = x
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)
i = 9
a = sum xor K(i)
b = leftrotate(a,i)
encrypt = b
end function
function decrypt(sum)
i = 9
a = leftrotate(sum,32-i)
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 hexAscii$(dec)
hex$ = decHex$(dec)
for i = 1 to len(hex$)step 2
chunk$ = mid$(hex$,i,2)
ascii = hexDec(chunk$)
char$ = char$ + chr$(ascii)
next
hexAscii$ = char$
end function
global two32,key '************
two32 = 2^32
key = 0.26547358 'seeds PRNG
dim A$(100) 'text blocks
dim B(4)
call subkey
msg$ = "the quick brown fox jumps over the lazy dogs back"
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 blocks
A$(ctr) = chunk$
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)
print numCrypt
result$ = result$ + hexAscii$(numCrypt)
next
print result$
'==================================================================
print ""
input "Press Enter to Decrypt Message >> ";entry$
msg$ = result$ '***********
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 blocks
A$(ctr) = chunk$
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)
print numCrypt
plain$ = plain$ + hexAscii$(numCrypt)
next
print plain$
print:print
print "End of Code"
End
sub subkey
randomize key 'create sub keys
for i = 0 to 9
x = int(rnd(1)* 2^32) '32 bit integer
K(i) = x
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)
i = 9
a = sum xor K(i)
b = leftrotate(a,i)
encrypt = b
end function
function decrypt(sum)
i = 9
a = leftrotate(sum,32-i)
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 hexAscii$(dec)
hex$ = decHex$(dec)
for i = 1 to len(hex$)step 2
chunk$ = mid$(hex$,i,2)
ascii = hexDec(chunk$)
char$ = char$ + chr$(ascii)
next
hexAscii$ = char$
end function