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.

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!

Still some issues. Found out that the small loop to find unique entries in the table is bogus, actually, one of the entries is missing and there is one duplicate created on the first table. The second table decides to create a zero entry (which is missing from the first table)... I'm still trying to find what is happening and finding a solution here. I've got another way of creating the table, but it's got an infinite loop in that and I haven't figured that one out either! Intense!

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
; ********* This is an infinite loop...
;For i% = 0 To 255
;   found = False
;   Repeat
;   value% = Rand(0,255)
;   For j% = 0 To 255
;      If value = PeekByte(table1, j) Then
;         Exit
;      ElseIf j >= i Then
;         found = True
;         Exit
;      EndIf
;   Next
;   Until found = True
;   PokeByte table1, i, value
;Next

Print "Encrypt Table:"
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 : Exit
  Next
Next
Print "Unique :" + found

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

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

found = 0
For i = 0 To 255
  For j = 0 To 255
     value = PeekByte (table2, j)
     If value = i Then found = found + 1 : Exit
  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$(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


EDIT: OK, I know what the problem is finally. Since a bank when initialized contains zero byte values across, it does an infinite loop as soon as it maches what the random value is versus a zero when the random value is zero. So I'll have to change my tactics a bit.

So here's the final solution, which seems to give 256 unique values:
Graphics 800,600,0,2
SeedRnd (MilliSecs()) 
Global table1 = CreateBank(256)
Global table2 = CreateBank(256)

zero% = Rand(0,255)
For i% = 0 To 255
   If i <> zero Then
      found = False
      Repeat
         value% = Rand(1,255)
         For j% = 0 To 255
            If value = PeekByte(table1, j) Then
               Exit
            ElseIf j >= i Then
               found = True
               Exit
            EndIf
         Next
      Until found = True
      PokeByte table1, i, value
   EndIf
Next

Print "Encrypt Table:"
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

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

;found = 0
;For i = 0 To 255
;  value1 = PeekByte (table1, i)
;  For j = 0 To 255
;     value2 = PeekByte (table1, j)
;     If value1 = value2 And i <> j Then found = value1 : Exit
;  Next
;Next
;Print "Duplicate entry at " + found

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

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

found = 0
For i = 0 To 255
  For j = 0 To 255
     value = PeekByte (table2, j)
     If value = i Then found = found + 1 : Exit
  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$(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 see you're altering a loop variable (j) inside the loop itself. I always thought that was a no-no? Maybe it is ok, though - if it works, it works.

Actually big10p, that's a false logic, as the counter will skip 0. Better avoid this to make sure you don't fall into some logic traps. The last example is the one that should work for 100% of cases.

Ah, yes - I knew it was dodgy. I guess you could have set j to -1 instead of 0 though? :P

Actually, I tried that, and got an infinite loop :o

thats funny. half an hour ago, i discovered the php function crypt() which one-way-crypts a string.
maybee the word DES tells you something.

http://www.google.de/search?hl=de&q=des&btnG=Google-Suche&meta=

anyway, looking up to a table is dangerous and can certainly be hacked. the worst thing is to save the table as a plain text file anywhere in your project...

Well, all I want to do is add a very simple live network protection scheme in the terminal system I'm building. I don't want to break barriers in inovation, just a simple live protection. This means that the key is generated at some point before the connexion occurs, and it is sent at some point. And from there, it can be used locally to decode incoming messages. So it means that both sides have a different key. Anyhow, this is all simplistic networking protection that I am thinking but I just want it to work and without any major flaw.

then you might just do a look-up table
you create an array with 256 entries and for every ascii-sign you define an other ascii-sign that will be converted into.

Any code example for that? I have 4 :P And they are all posted in this here thread!

i'd suppose that you're better of writing it yourself, because you have your understanding in that. if you just copy it, you dont understand it and you can hardly change it;)