Encryption that doesn't repeat?

BlitzMax Forums/BlitzMax Programming/Encryption that doesn't repeat?

I've tried a couple of the encryption routines from the archives to no avail. What I need is an encryption routine that doesn't print the same character for all of one character. Ie...every A would be a 9. I thought RC4 would do the trick but if you encrypt a string of "0000000000"...you get 10 of the same character back. Any ideas?

I thought RC4 would do the trick but if you encrypt a string of "0000000000"...you get 10 of the same character back. Any ideas?
Yes, there's something very wrong with your RC4 routine.

...every A would be a 9...

Maybe it's so simple no-one would think to try cracking it in that way? ;-)

Maybe do a simple addition of some kind of counter, perhaps adding the key to itself, and then add this to the current value, so with each character the value changes?

Here is my simple encryption method. It probably wouldn't stand up to a decent hacker, but I didn't need it too when I wrote it.

To encrypt or decrypt a string you simply need a key which is passed to encrypt() or decrypt().

The encrypted string from toString() is quite large so you might want to use Hex or something else instead of Ascii values to represent it.

Local cleartext$ = "So you want encryption then?"
Local encrypted$ = "074332112044137241368121252220129355329160238191294150111158188154327136198261156374257"

Local text:TEncrypt

text = TEncrypt.Encrypt( 567, cleartext )

Print "Cleartext = " + cleartext
Print "Encrypted = " + text.toString() 
Print "Decrypted Correctly = " + text.decrypt( 567 )
Print "Decrypted Incorrectly = " + text.decrypt( 890 ) 

text = TEncrypt.set( encrypted )
Print "Decripted string = " + text.decrypt( 234 )


Type TEncrypt
	Field enc%[]

	Method toString$() 
	Local val$
		For Local char% = 1 To enc.length - 1
			val:+Right("000"+enc[char],3)
		Next
	Return val
	End Method
		
	Function encrypt:TEncrypt( seed% , text$ ) 
	Local me:TEncrypt = New TEncrypt
		SeedRnd( seed )
		me.enc = New Int[text.length + 1]
		For Local char% = 1 To text.length
			me.enc[char] = Asc(Mid(text , char , 1) ) + Rand(255)
		Next
	Return me
	End Function
	
	Function set:TEncrypt( data$ = "" )
	Local me:TEncrypt = New TEncrypt
		'# Minimal Error checking
		If Len(data) / 3 <> Int(Len(data) / 3) Then Return Null
		'#
		me.enc = New Int[data.length/3+1]
		For Local char% = 1 To me.enc.length -1
			me.enc[char] = Int(Mid( data , char *3-2, 3))
		Next
	Return me
	End Function

	Method decrypt$( seed% )
	Local val%, text$
		SeedRnd( seed) 
		For Local char% = 1 To enc.length -1
			val = enc[char] - Rand(255) 
			If val > 31 And val < 255 And val <> 127 Then
				text:+ Chr(val) 
			Else
				text:+ "?"
			End If
		Next
	Return text
	End Method
End Type



Yes, there's something very wrong with your RC4 routine.


so, this:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1711
isn't *truly* rc4 encryption?

If you want secury encryption use blockciphers.

Even though already mentioned: AES / Rijndael

I was actually looking at the IDEA algorithm since it's never been cracked. Has anyone already coded IDEA? A .dll of the algorithm in C++ would be cool too.

I was actually looking at the IDEA algorithm since it's never been cracked.
The strength of an encryption system is not measured by how often it's been "cracked", but how much it's been under scrutiny. IDEA is fairly new (compared to say DES) and thus has not been under as much scrutiny. Just because someone can't come up with a better than brute force attack today, doesn't mean they can't do it tomorrow.

FD, you keep forgetting to put IMO at the end of your posts. :)

If I'm not mistaken, IDEA has been around for almost 17 years. I think not being cracked in that amount of time deserves some merit. And just because no one can sprout wings today doesn't mean they can't tomorrow...

Now if you can port the IDEA algorithm to BMax...that would be useful.

Just because you never heard of IDEA being cracked doesn't mean it hasn't been cracked...

Thought I might have a go at a cipher for fun.
Local file$ = RequestFile("Choose a file to test cipher on")

' create an encrypted version of the file in the same path
Local Bank:TBank = Cipher.Encrypt(LoadBank(file),AppTitle)
SaveBank(Bank, file+".encrypted")
Print "Saved ~q"+(file+".encrypted")+"~q"

' decrypt the encrypted version and save
Bank = Cipher.Decrypt(LoadBank(file+".encrypted"),AppTitle)
SaveBank(Bank, file+".decrypted")
Print "Saved ~q"+(file+".decrypted")+"~q"

Type Cipher

	Function Encrypt:TBank(Bank:TBank, Key$)
		Local t:Int=10, n:Int, ki:Int
		Local Out:TBank = CreateBank(Bank.Size())
		Local inptr:Byte Ptr = BankBuf(Bank)
		Local outptr:Byte Ptr = BankBuf(Out)
		For n=0 Until key.length
			Select n Mod 2
		 		Case 0 ; t:*key[n]
				Case 1 ; t:+key[n]
			EndSelect
			t:Mod 360
		Next
		For n=0 Until Bank.Size()
			outptr[n]=inptr[n]+Floor(Cos(t)*255)
			t:+(inptr[n]*key[ki])+n
			t:Mod 360+inptr[n]
			ki:+1
			If ki=>key.length ki=0
		Next
		Return Out
	EndFunction

	Function Decrypt:TBank(Bank:TBank, Key$)
		Local t:Int=10, n:Int, ki:Int
		Local Out:TBank = CreateBank(Bank.Size())
		Local inptr:Byte Ptr = BankBuf(Bank)
		Local outptr:Byte Ptr = BankBuf(Out)
		For n=0 Until key.length
			Select n Mod 2
		 		Case 0 ; t:*key[n]
				Case 1 ; t:+key[n]
			EndSelect
			t:Mod 360
		Next
		For n=0 Until Bank.Size()
			outptr[n]=inptr[n]-Floor(Cos(t)*255)
			t:+(outptr[n]*key[ki])+n
			t:Mod 360+outptr[n]
			ki:+1
			If ki=>key.length ki=0
		Next
		Return Out
	EndFunction

EndType

Seems solid, even with a single character key, and handles your repeating problem quite well (try it with a text file full of the same character)

IMHO, nobodies going to spend much time trying to decrypt your media, as other people have already said, there is alot easier ways of nabbing it.

*EDIT* Updated code!

Thanks REDi i'll mess around with that.

Plash, what grade are you in?