saving and reading encrypted crypto files

Discussion in 'Code Samples & Tips' started by hlo, Apr 7, 2009.

  1. hlo

    hlo Member Licensed User

    working on a medical database and needing to encrypt some data in it. used crypto but found I had trouble figuring out how to save a retrieve encrypted data. here is what i came up with. may not be the best way, but it works.

    'Crypto is a Crypto object, Bit is a Bitwise object, Bin is a Binaryfile object
    Sub Globals
    Dim string(0) As Byte, secret(0) As Byte, s3(0) As Byte,string2(0) As Byte
    s4=""
    x=0
    length=0
    'used lots of variables so no chance of cross over of data for demo
    PassPhrase = "password" 'not recomended - best to save password as an encrypted file to compare with what is typed in
    txtString="begin- a text file to encrypt -end" 'just a file to encrypt than decrypt
    End Sub

    Sub App_Start
    FileClose(c2) 'i always close my files 1st to be sure
    FileClose(c1)
    Bit.New1
    Crypto.New1
    encryptx 'encrypt and save routine
    decryptx 're-read then decrypt routine
    AppClose
    End Sub

    Sub Encryptx
    string() = Bit.StringToBytes(txtString,0,StrLength(txtString)) 'convert the string to bytes
    secret() = Crypto.Encrypt(PassPhrase, string()) 'crypto routine
    x=ArrayLen(secret()) 'get length of array to save
    FileOpen(c1,"test.txt",cRandom)
    bin.New1(c1,True)
    bin.WriteInt16(x) 'save length of array
    bin.WriteBytes(secret()) 'save encrypted data as bytes
    FileClose(c1)
    Msgbox("done encrypt")
    End Sub

    Sub decryptx
    FileOpen(c2,"test.txt",cRandom)
    bin.New1(c2,True)
    x2=bin.ReadInt16 'read in array size
    Dim s3(x2) As byte 'reset dim to proper size - believe it or not, this makes a difference
    length = bin.ReadBytes(s3(), x2) 'read in string for x2 bytes - length variable not used
    FileClose(c2)
    string2()=crypto.Decrypt(PassPhrase,s3()) 'crypto routine
    txtString=bit.BytesToString(string(),0,ArrayLen(string2())) 'turn back to string
    Msgbox("decrypted file "&txtstring)
    End Sub

    hope it helps some one.
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice