Text Scrambling

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
xbgtc
Posts: 249
Joined: Oct 14, 2007 5:40
Location: Australia

Text Scrambling

Post by xbgtc »

Was playing around trying to convert a sentence into a number (prolly impossible) but then just scrambling/unscrambling and stumbled upon this which doesn't make sense but works nonetheless.

Two Weird Things

1. How can you get a CHR of numbers > 255?? lol
2. How can you unscramble with a different code??

Code: Select all

dim as string a,b,c,d="This can be any text"
for i as integer=1 to len(d)
    b=mid(d,i,1)
    b=chr(asc(b)+1000)
    c+=b
next
d=""
?"  scrambled= ";c
for i as integer=1 to len(c)
    b=mid(c,i,1)
    d+=chr(asc(b)-232)
next
?"unscrambled= ";d
sleep
xbgtc
Posts: 249
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Text Scrambling

Post by xbgtc »

ahh what's happening is that chr must wrap around after 255 ie. 256=0, 257=1 and so 512=0,768=0 and 1000-768=232 and that's why it works - not so magical as first thought :)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Text Scrambling

Post by stylin »

Hi, good observation about the wrap-around effect. :) If you want to play around with more ways to encrypt text, this is a good place to start:
http://en.wikipedia.org/wiki/Cryptography
http://en.wikipedia.org/wiki/Stream_cipher
http://en.wikipedia.org/wiki/RC4

For example, here's a simple synchronous stream cipher, like yours, although this one scrambles each character with a new random number. Have fun!

Code: Select all

function Encrypt ( key as integer, chars as const string ) as string
  Randomize( key )
  
  var result = ""
  for i as integer = 0 to Len( chars ) - 1
    var x = Int( Rnd() * 255 )
    result += Chr( chars[i] xor x ) ' chars[i] is the same as Asc(Mid(chars,i,1))
  next i
  return result
end function

var key = 5318008
var plaintext = "some plaintext stream"
var ciphertext = Encrypt( key, plaintext )
Print "encrypted: "; ciphertext
Print "decrypted: "; Encrypt( key, ciphertext )
xbgtc
Posts: 249
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Text Scrambling

Post by xbgtc »

Hi and yeh have fun is about right :)

Nice little program as it encrypts/decrypts although i have no idea how lol. I mean, the use of the key with randomize, and how it decrypts when it appears to be all random haha.

Will have to study it, and check out the links too which might give me an idea :)

I initially wanted to create an integer of the string ie. a short number like 37684 that would represent the string, but that may be impossible or at the very least above my programming capabilities. Anyhow encrypting is interesting for the fact it hides stuff :)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Text Scrambling

Post by stylin »

Hi. Randomize is used to seed the random number generator; a different seed will tell Rnd to produce a different sequence of random numbers. But if you use the same seed (our key), Rnd will produce the same sequence.

When you have time, look into hash functions; they take a stream of characters and turn them into a number. They're useful for creating data structures that are very fast and efficient to add and search for elements, like associative arrays or hash tables; the number generated is used as an index into an array.

Some kinds of hash functions are used to verify the integrity of some data (like when you download a file) since they give relatively strong guarantees that if the data is corrupted or tampered with, the number generated from it will be different than the one generated from the original.

http://en.wikipedia.org/wiki/Hash_function
http://en.wikipedia.org/wiki/List_of_hash_functions
http://en.wikipedia.org/wiki/MD5

But hash functions in general won't allow you to "decode" the number back into the original string, so you might have to research other topics (like maybe compression).
xbgtc
Posts: 249
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Text Scrambling

Post by xbgtc »

Ahh i get it now with randomize - never knew you could do that with it :) and that chars was cool too.

So i decided to try crack the key and yeh takes about 6 secs haha but you did say these are simple methods.

Yeh hash tags - i've used checksums in my program to verify the cheaply encrypted data on disk hasn't been tampered with haha.

All intresting stuff - thanks for the links :)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: Text Scrambling

Post by stylin »

Cool, glad you got some new things to think about. :)

One of my favorite NES games uses a cipher like the ones we've posted to encrypt the passwords it gives you when you save the game. It was lots of fun to mess around with the debugger and figure out how the game was doing that, I even wrote a FB program so I could create my own passwords and start the game with the gold/items I wanted! :)
Post Reply