Results 1 to 3 of 3

Thread: How do I encrypt a file?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Post

    Aaron,

    I'm curious to know what you mean by 'Very Simple'? Do you mean any gobdah at all, with little knowledge of VB, could decrypt the file? How? By trial and error? Or would it take a considerably experienced hacker to do it? Just curious.....

    Thanks!

  2. #2
    New Member
    Join Date
    Aug 1999
    Location
    Chicago,IL,USA
    Posts
    5

    Post

    I am building an application that will transfer files over the internet. I want to be able to secure these files by encrypting them or locking them so that no one can see or use them at least I want to try and do that. Does anyone have any suggestions?

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    All you really need to do is jumble the contents of the file around in such a way that you know how to unjumble it at the other end, eg. You could switch pairs of bytes before transfer, then switch them back after.

    How complex and secure the encryption is depends on the method you use.

    Here's an example of a Very Simple method of Encryption, which generates a Key to Encrypt and Decrypt a Text File.
    Code:
    Private Sub Command1_Click()
        'Open Encrypted File
        Dim sText As String
        Dim iFile As Integer
        
        On Error GoTo Cancelled
        With CommonDialog1
            .CancelError = True
            .Filter = "AY Encrypted|*.aye"
            .ShowOpen
            iFile = FreeFile
            Open .FileName For Input As iFile
            sText = Input(LOF(iFile), iFile)
            Close iFile
        End With
        If Left(sText, 12) <> "AY Encrypted" Then
            MsgBox "Not an AY Encrypted File Format"
        Else
            Text1 = DecryptText(sText)
        End If
        
    Cancelled:
    End Sub
    
    Private Sub Command2_Click()
        'Save Encrypted File
        On Error GoTo Cancelled
        With CommonDialog1
            .CancelError = True
            .Filter = "AY Encrypted|*.aye"
            .ShowSave
            iFile = FreeFile
            Open .FileName For Output As iFile
                Print #iFile, EncryptText(Text1);
            Close iFile
        End With
        MsgBox "Saved"
    Cancelled:
    End Sub
    
    Function EncryptText(ByVal sText As String) As String
        Dim sKey As String
        Dim iKey As Integer
        Dim lPos As Long
        Dim iChar As Integer
        Dim iEx As Integer
        
        Randomize Timer
        sKey = Right("000" & Hex(Int(Rnd(1) * 4095)), 3)
        For lPos = 1 To Len(sText)
            iChar = Asc(Mid$(sText, lPos, 1))
            iEx = iChar + Val("&H" & Mid(sKey, iKey + 1, 1))
            Mid$(sText, lPos, 1) = Chr(iEx)
            iKey = (iKey + 1) Mod 3
        Next
        EncryptText = "AY Encrypted" & sText & Right(Space(20) & (Val("&H" & sKey) + Len(sText)), 20)
        
    End Function
    
    Function DecryptText(ByVal sText As String) As String
        Dim sKey As String
        Dim iKey As Integer
        Dim lPos As Long
        Dim iChar As Integer
        Dim iEx As Integer
        
        sText = Mid(sText, 13)
        sKey = Right("000" & Hex(Val(Right(sText, 20)) - (Len(sText) - 20)), 3)
        For lPos = 1 To Len(sText) - 20
            iChar = Asc(Mid$(sText, lPos, 1))
            iEx = iChar - Val("&H" & Mid(sKey, iKey + 1, 1))
            Mid$(sText, lPos, 1) = Chr(iEx)
            iKey = (iKey + 1) Mod 3
        Next
        DecryptText = Left(sText, Len(sText) - 20)
    
    End Function
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

Posting Permissions

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



Click Here to Expand Forum to Full Width