Encrypt / bDecrypt issues

Blitz3D Forums/Blitz3D Programming/Encrypt / bDecrypt issues

LOOKUP TABLE CODE
===============

The issue is that something is miscalculated when encrypting/decrypting, and I can't seem to see that.

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

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

For i = 0 To 239 Step 16
   a$ = ""
   For j = 0 To 15
      b$ = Str$(PeekByte (table, 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 (table, j)
     If value = i Then found = found + 1
  Next
Next
Print "Unique :" + found

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_decrypt$(temp_bank1, temp_bank2, length)

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

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


Any help appreciated.