Qbasicnews.com

Full Version: 105.. bit encryption.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
DECLARE SUB decrypt (array1() AS INTEGER, key1() AS INTEGER)
DECLARE SUB encrypt (array1() AS INTEGER, key1() AS INTEGER)
CLS

RANDOMIZE TIMER

string1$ = "Don't you just love 'Married with Children'?"
len1% = LEN(string1$)
DIM array1(len1% - 1) AS INTEGER

FOR i% = 0 TO UBOUND(array1)
array1(i%) = ASC(MID$(string1$, i% + 1, 1))
PRINT array1(i%);
NEXT i%

'MUST BE ODD! (or, even, if you count the zeroth element)
'MUST NOT EXCEED: max.bits.data.can.hold - bits.required(max.plaintext.number)


'In this case, a max of 255 must not exceed a key length of 7
'Each time the bits required can possibly double by almost 2.
'Program will not automatically error if length of 7 is exceeded,
'but can error with the right key combination.

'For signed integers, each sub key is made up of 15 bits (MOD max length of string). Theoretically, then, each key holds 7 subkeys * 15 bits.
'That's 105 bits, folks. Assuming there aren't any repeat keys, that's
'2^105, or 40,564,819,207,303,340,847,894,502,572,032, combinations.

keyLength% = 7
DIM testkey(keyLength% - 1) AS INTEGER

'DO
FOR i% = 0 TO keyLength% - 1
testkey(i%) = INT(RND * len1%)
NEXT i%

encrypt array1(), testkey()

PRINT : PRINT : FOR i% = 0 TO UBOUND(array1): PRINT array1(i%); : NEXT i%

decrypt array1(), testkey()

'IF INKEY$ <> "" THEN EXIT DO
'LOOP
PRINT : PRINT : FOR i% = 0 TO UBOUND(array1): PRINT array1(i%); : NEXT i%

SUB decrypt (array1() AS INTEGER, key1() AS INTEGER)

keyLength% = UBOUND(key1)
arrayLength% = UBOUND(array1) + 1
n% = keyLength%

DO
i% = key1%(n%) MOD arrayLength%
k% = (i% + arrayLength% - 1) MOD arrayLength%
temp1% = array1(i%)
DO
i% = i% + 1
IF i% = arrayLength% THEN i% = 0
IF i% = k% THEN EXIT DO
j% = i% - 1: IF j% < 0 THEN j% = j% + arrayLength%
array1(i%) = array1(i%) + array1(j%)
LOOP
n% = n% - 1
IF n% = -1 THEN EXIT DO


i% = key1%(n%) MOD arrayLength%
temp1% = array1(i%)
k% = (i% + arrayLength% - 1) MOD arrayLength%
DO
i% = i% + 1
IF i% = arrayLength% THEN i% = 0
IF i% = k% THEN EXIT DO
temp2% = array1(i%)
array1(i%) = array1(i%) - temp1%
temp1% = temp2%
LOOP
n% = n% - 1
LOOP
END SUB

SUB encrypt (array1() AS INTEGER, key1() AS INTEGER)

keyLength% = UBOUND(key1)
arrayLength% = UBOUND(array1) + 1

DO
i% = key1%(n%) MOD arrayLength%
temp1% = array1(i%)
k% = (i% + arrayLength% - 1) MOD arrayLength%
DO
i% = i% + 1
IF i% = arrayLength% THEN i% = 0
IF i% = k% THEN EXIT DO
temp2% = array1(i%)
array1(i%) = array1(i%) - temp1%
temp1% = temp2%
LOOP
n% = n% + 1
IF n% > keyLength% THEN EXIT DO


i% = key1%(n%) MOD arrayLength%
k% = (i% + arrayLength% - 1) MOD arrayLength%
temp1% = array1(i%)
DO
i% = i% + 1
IF i% = arrayLength% THEN i% = 0
IF i% = k% THEN EXIT DO
j% = i% - 1: IF j% < 0 THEN j% = j% + arrayLength%
array1(i%) = array1(i%) + array1(j%)
LOOP
n% = n% + 1
LOOP
END SUB

The only drawback so far is that encryption can double size of the text for every +2 in the key length. (to be extra safe you can say quadruple. Haven't analyzed it thoroughly. But with 99% certainty I can say that the maximum is double)

This is why you can only have 7 15-bit keys....
Using longs to store the data, it is possible to have a key length of 24... that is 360 bit encryption.

Figuring out a way to keep the resulting bit usage stable would make it possible to have any bit length encryption.
Im no expert, but Im sure that 128bit encryption is nigh on impossible to crack, at least for non expert cyrptographers. It looks meaty!

I will test it out when I get home
correction. 105 bits.
lol. Big Grin
Hey, Aga....do you mind if I put this on the "Encryption" section of The Geekery?
Very cool, btw. Smile
Ok, but I don't know how strong it actually is..
Thanks...
It's strong.