PDA

View Full Version : Uses "File", "Crypto" ... ???



marcuslee
01-12-2009, 02:56
I've been trying to get FILE and CRYPTO to work together. I can get encrypted data to be inserted into a RANDOM file, but when I retrieve it, the message is both encrypted and decrypted. It is kinda strange.

Here's the code:



Uses "File", "Crypto"

Type Record
Message As String * 50
Alt As String *50
End Type

Dim Save As Record
Dim GetContents As Record
Dim Decrypt As Record
Dim FileName As String = APP_SourcePath & "Records.DAT"
Dim f As DWord
Dim password As String = "password"

FILE_Kill(FileName)
f = FILE_Open(FileName, "random", SIZEOF(Record))

Save.Message = iCrypto_EncryptRC4("Here is the first message.", password)
Save.Alt = iCrypto_EncryptRC4("Here is the first alternate.", password)

FILE_PutR(f, 1, Save)

Save.Message = iCrypto_EncryptRC4("Here is the second message.", password)
Save.Alt = iCrypto_EncryptRC4("Here is the second alternate.", password)

FILE_PutR(f, 2, Save)

FILE_GetR(f, 1, GetContents)
Decrypt.Message = iCrypto_DecryptRC4(GetContents.Message, password)
Decrypt.Alt = iCrypto_DecryptRC4(GetContents.Alt, password)

MsgBox 0, Decrypt.Message & Chr$(13) & Decrypt.Alt


Anyone have any ideas of what I am doing wrong?



Mark :violent:

Petr Schreiber
01-12-2009, 08:08
Hi Mark,

changing the TYPE to:


Type Record
Message As ASCIIZ * 50
Alt As ASCIIZ * 50
End Type

seems to fix the problem.

I am thinking why - the only difference is that STRING * n is padded with spaces (ASCII 32), while ASCIIZ * n is padded with ASCII 0.

I think both should work for this case, but better wait for Eros.


Petr

marcuslee
01-12-2009, 16:45
changing the TYPE to:


Type Record
Message As ASCIIZ * 50
Alt As ASCIIZ * 50
End Type

seems to fix the problem.


Most excellent! Now, I understand a fundamental difference between asciiz and string. I was wondering when it would make a difference. Now I know!

Since I couldn't get iCrypto_EncryptRC4 to work, I started looking at the iCrypto_Bin2ASCII or iCrypto_String2ASCII set of commands. They were working for me, at least so far, but to me it looks kinda funny to have an encrypted file with a bunch of numbers. Plus, that means that it really isn't encrypted at all. But, I don't need high levels of encryption, so I wasn't worried about it. Now, maybe, I have two ways to do it.

Thank you!


Mark :eusadance:

Edit: In fact, I applaud you!

marcuslee
01-12-2009, 19:38
The following version works with type STRING:



Uses "File", "Crypto"

Type Record
Message As String * 50
Alt As String * 50
End Type

Dim Save As Record
Dim Encrypted As Record
Dim GetContents As Record
Dim Decrypted As Record

Dim FileName As String = APP_SourcePath & "Save.DAT"
Dim f As DWord
Dim password As String = "password"

FILE_Kill (FileName)
f= FILE_Open (FileName, "random" , SIZEOF (Record))

Save.Message = "Here is the first message."
Save.Alt = "Here's the first alternate."
Encrypted.Message = iCrypto_EncryptRC4(Save.Message, password)
Encrypted.Alt = iCrypto_EncryptRC4(Save.Alt, password)
FILE_PutR (f, 1, Encrypted)

Save.Message = "Here is the second message."
Save.Alt = "Here's the second alternate."
Encrypted.Message = iCrypto_EncryptRC4(Save.Message, password)
Encrypted.Alt = iCrypto_EncryptRC4(Save.Alt, password)
FILE_PutR (f, 2, Encrypted)

FILE_GetR (f,1,GetContents)
Decrypted.Message = iCrypto_DecryptRC4(GetContents.Message, password)
Decrypted.Alt = iCrypto_DecryptRC4(GetContents.Alt, password)

MsgBox 0, Trim$(Decrypted.Message) & Chr$(13) & Trim$(Decrypted.Alt)


The difference I believe is that I assigned a value to the Message and Alt before trying to encrypt it. If that isn't it, then I am once again lost. It also works if you change the type to ASCIIZ.

Gotta Love thinBasic! :party:


Mark