Encrypt / bDecrypt issues

Blitz3D Forums/Blitz3D Programming/Encrypt / bDecrypt issues

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

My solution, 2 tables... And 2 seperate functions (Encrypt / Decrypt)... :(

Graphics 800,600,0,2
SeedRnd (MilliSecs()) 
Global table1 = CreateBank(256)
Global table2 = CreateBank(256)

For i% = 0 To 255
   value% = Rand(0,255)
   For j% = 0 To i
      table_value% = PeekByte(table1, j)
      If value = table_value Then
         j = 0
         value% = Rand(0,255)
      EndIf
   Next
   PokeByte table1, i, value
Next

For i = 0 To 239 Step 16
   a$ = ""
   For j = 0 To 15
      b$ = Str$(PeekByte (table1, i + j))
      a$ = a$ + b$ + String$(" ", 4 - Len(b$))
   Next
   Print a$
Next

For i = 0 To 255
  For j = 0 To 255
     value = PeekByte (table1, j)
     If value = i Then found = found + 1
  Next
Next
Print "Unique :" + found

For i = 0 To 255
  value = PeekByte (table1, i)
  PokeByte table2, value, i
Next

phrase$ = Input$("Type the phrase to encrypt :")
length% = Len(phrase$)

temp_bank1 = CreateBank(length) ; original string
For i = 1 To length
   ascii% = Asc(Mid$(phrase$,i,1))
   PokeByte temp_bank1, i - 1, ascii
Next

temp_bank2 = CreateBank(length) ; result bank

Print "Encrypted :" + encrypt$(temp_bank1, temp_bank2, length)

CopyBank temp_bank2, 0, temp_bank1, 0, length
Print "Decrypted :" + decrypt$(temp_bank1, temp_bank2, length)
WaitKey()
End

Function encrypt$(bank1, bank2, length)
result$ = ""
For i = 1 To length
   ascii% = PeekByte(bank1, i - 1)
   value% = PeekByte(table1, ascii)
   PokeByte bank2, i - 1, value
   result$ = result$ + Chr$(value) ; for display purpose
Next
Return result$
End Function

Function decrypt$(bank1, bank2, length)
result$ = ""
For i = 1 To length
   ascii% = PeekByte(bank1, i - 1)
   value% = PeekByte(table2, ascii)
   PokeByte bank2, i - 1, value
   result$ = result$ + Chr$(value) ; for display purpose
Next
Return result$
End Function


I tought I could make it simpler. In fact, to make this simpler, I would have to swap 2 values, making it a 128 byte encryption key, which would give the answer in the next 128 bytes. That would be really bad! Actually, making a reverse reference from the table seems the logical way of doing this. But for the solution proposed here, I'll try to see if it is possible to not have a cross reference table and just try to compute that out... I'm just a little hazed out on this sort of thing!