bb to bmx? could use some help with this

BlitzMax Forums/BlitzMax Programming/bb to bmx? could use some help with this

Caton(Posted 2016) [#1]
Could you guys help get the code correct?
Local key$="eje3ii44994594540"
EncryptFile("original.txt","encrypted.txt",key$)
DecryptFile("encrypted.txt","decrypted.txt",key$)

Function EncryptFile(in$,out$,k$)
	Local filein:TStream = ReadFile(in$)
	Local fileout:TStream = WriteFile(out$)
	Local s$=""
	While Not Eof(filein)
		s$=s$+Chr(ReadByte( filein ))
	Wend
	
	Local backwards:Int=False
	Local i
	Local c$
	Local a
	Local j
	Local kc$
	Local ka
	Local enc
	For i%=1 To Len (s$)
		c$=Mid(s$,i%,1)
		a%=Asc(c$)
		j%=i%
		While j%>Len(k$)
			j%=j%-Len(k$)
		Wend
		If backwards Then j%=Len(k$)-j%+1
		If j=1 Then backwards=False
		If j=Len(k) Then backwards=True
		kc$=Mid(k$,j%,1)
		ka%=Asc(kc$)
		enc%=ka%+a%
		
		WriteShort(fileout,enc)
		
	Next
	
	CloseFile filein
	CloseFile fileout
	
End Function


Function DecryptFile(in$,out$,k$)
	Local filein:TStream = ReadFile(in$)
	Local fileout:TStream = WriteFile(out$)
	Local backwards:Int=False
	While Not Eof(filein)
		Local i
		Local s$
		Local enc
		Local j
		Local kc$
		Local ka
		Local k
		Local a
		i=i+1
		s$=s$+Chr(ReadByte( filein ))
		enc%=ReadShort(filein)
		
		j%=i%
		While j%>Len(k$)
			j%=j%-Len(k$)
		Wend
		If backwards Then j%=Len(k$)-j%+1
		If j=1 Then backwards=False
		If j=Len(k) Then backwards=True
		kc$=Mid(k$,j%,1)
		ka%=Asc(kc$)
		a%=enc%-ka%
		WriteByte fileout,a%
	Wend
	
	CloseFile filein
	CloseFile fileout

End Function



Cocopino(Posted 2016) [#2]
Found some usable code for you on the forums:


Local key:String = "12345678"
Local filepath:String = "original.txt"

'create original.txt file if it does not exist
If (FileType(filepath) <> 1)
	Local fh:TStream = WriteStream(filepath)
	fh.WriteLine("abcdefghijklmnopqrstuvwxyz")
	fh.Close()
EndIf

'encrypt
Local encrypted:TBank = Cipher.Encrypt (LoadBank(filepath), key)
encrypted.Save("encrypted.txt")

'decrypt
Local decrypted:TBank = Cipher.Decrypt (LoadBank("encrypted.txt"), key)
decrypted.Save("decrypted.txt")

Type Cipher

	Function Encrypt:TBank(Bank:TBank, Key:String)

		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:String)
	
		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



BlitzSupport(Posted 2016) [#3]
The code itself basically works (I just had to comment out the duplicate identifier 'k', which is passed as a parameter), but for whatever reason, it crashes while decrypting, on the ReadShort line -- presumably passing the end of the file, so it seems more of a logic error than any problem with your conversion.

Can't really tell what it's meant to be doing, though, so can't help with that! I tried adding an Eof () check to the ReadByte and ReadString lines, which runs, but produces garbage in the output file.


Floyd(Posted 2016) [#4]
Reads in DecryptFile must match up with writes in EncryptFile.

You write a Short but try to read back a Byte and a Short.