Results 1 to 4 of 4

Thread: Uses "File", "Crypto" ... ???

  1. #1
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    Uses "File", "Crypto" ... ???

    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:

    [code=thinbasic]
    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
    [/code]

    Anyone have any ideas of what I am doing wrong?



    Mark

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Uses "File", "Crypto" ... ???

    Hi Mark,

    changing the TYPE to:
    [code=thinbasic]
    Type Record
    Message As ASCIIZ * 50
    Alt As ASCIIZ * 50
    End Type
    [/code]
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    Re: Uses "File", "Crypto" ... ???

    Quote Originally Posted by Petr Schreiber
    changing the TYPE to:
    [code=thinbasic]
    Type Record
    Message As ASCIIZ * 50
    Alt As ASCIIZ * 50
    End Type
    [/code]
    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

    Edit: In fact, I applaud you!

  4. #4
    Member marcuslee's Avatar
    Join Date
    Sep 2008
    Location
    Kansas, USA
    Age
    42
    Posts
    222
    Rep Power
    38

    Re: Uses "File", "Crypto" ... ???

    The following version works with type STRING:

    [code=thinbasic]
    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)
    [/code]

    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!


    Mark

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •