CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2004
    Posts
    184

    Cool Help with encryption

    I found this encryption app on the internet, its very good but it can only Encrypt/Decrypt files, ive been trying to change it to encrypt/decrypt strings.
    To do this i need to figure out how to decode binary data without openning the file.

    Hidden in this string: "áYq" should be the number: 1901715839
    which is used with the password to decrypt the text.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Help with encryption

    Unfortunately without knowing what encryption technology is been used on it, it's very difficult to say what to do...

    when you say 'decode binary data without openning the file', what do you mean???? ...

    without opening a file you could simply pass it a string or byte array to en/decrypt ...

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Aug 2004
    Posts
    184

    Cool Re: Help with encryption

    All i'm trying to do is get the number: 1901715839 from this binary code: "áYq".
    When decrypting a file it loads the binary data into a UDT

    Code:
     
    Private Type encFileHeader
        FLType1 As Byte ' = e
        FLType2 As Byte ' = n
        FLType3 As Byte ' = c
        Alg As Byte     ' algorithm = 1 or 2 or 3
        RndVal As Long  ' a random value (to enforce the password)
    End Type
     
    Private Sub Decrypt
    Dim FLHeader As encFileHeader
       Open "C:\Test.txt" For Binary Access Read As #1
       Get #1,, FLHeader
       Close #1
    End Sub
    So FLHeader.RndVal has the number: 1901715839
    if you copy this string "encáYq" in a txt file and read it you get
    FLType1 = 101
    FLType2 = 110
    FLType3 = 99
    Alg = 1
    RndVal = 1901715839

    I need to know how i can read this data directly from a textbox or string, i can get the first 4 data easily using Asc.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Help with encryption

    Let me invite you to the following considerations.
    The string, coming from a textfield or residing in a variable is actually unicode.
    If you assign a string to a byte array this is revealed
    Code:
    Private Sub Command1_Click()
      Dim i%, a$, ba() As Byte
      a$ = "encáYq"
      ba = a$
      Debug.Print len(a$), UBound(ba)
      For i = 0 To UBound(ba): Debug.Print ba(i); " ";: Next
    End Sub
    The byte array ba is twice the length of the string. Printing it out reveals every second byte to be zero. This is how VB handles unicode and defines Len() to be the length if it was pure ASCII.

    But this also reveals our information and allows us to write a string-to-header conversion:

    Code:
    Private Sub FillHeaderFromString(Header As encFileHeader, HStr As String)
      Dim ba() As Byte
      ba = HStr
      With Header
        .FLType1 = ba(0)
        .FLType2 = ba(2)
        .FLType3 = ba(4)
        .Alg = ba(6)
        .RndVal = ((ba(14) * 256& + ba(12)) * 256& + ba(10)) * 256& + ba(8)
      End With
    End Sub
    
    Dim Hdr as encFileHeader
    FillHeaderFromString Hdr, "encáYq"
    The above sub fills a given header structure with the relevant information taken from a string.

    The footfall is, when writing to a file, every character only represents one byte only. The zeroes are NOT printed to the file when using Print #f,...
    So reading it back with Get #f,,header, the bytes are stuffed to the header structure.

    Our sub takes the info from a variable which is a string.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Help with encryption

    Since the random value is a Long, there are four bytes of data stored in it. Thus the three character string you gave isn't all of it. I suspect that whatever you used to look at the file didn't show you all the characters because they don't all have visual representations. In other words, one or more characters apparently has an ASCII value below 32. Try looking at it with a Hex viewer/editor.

    Additionally, there should be a total of eight characters for your string. Three bytes for the FLType, one for the Alg, and four for the RndVal.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Help with encryption

    Hi Wiz.
    Look, I decoded all 4 bytes of the long properly.
    Also I double checked with the values cdrcools gave in the first place.
    If you debug.print .RndVal it gives 1901715839, although I simply copied the string "encáYq" with cut&paste. So the result is right.

    Length of the byte array is 16 (0 to 15) and string Len() is 8.

    I double checked the other values too. As you can see in the dump I do
    For i = 0 To UBound(ba): Debug.Print ba(i); " ";: Next
    you can see all the bytes turning out right.

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Help with encryption

    Well, we're both correct. Although I only see 6 characters in my browser, pasting the string into VB reveals the two missing characters. They are indeed non-printable, displaying as squares, with ASCII values of 1 and 127. As to why my browser isn't showing them I'm not sure, since it has shown squares on other occasions.

    However, to avoid the zeros when converting to the Byte array, try it this way:
    Code:
    Private Sub Command1_Click()
      Dim i%, a$, ba() As Byte
      a$ = "encáYq"
      ba = StrConv(a, vbFromUnicode)
      Debug.Print Len(a$), UBound(ba)
      For i = 0 To UBound(ba): Debug.Print ba(i); " ";: Next
    End Sub
    <EDIT>Interestingly, I cannot paste the original string into the post. It seems Chr$(1) just won't paste. Therefore, the string in the code above is missing one character.
    Last edited by WizBang; February 17th, 2009 at 02:13 PM.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Help with encryption

    Well yes, that's my code.
    Pasting the string might show a square, but the Chr$(1) is still there, only the font has no representing character for this. Icopied and pasted it from the original post.

  9. #9
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Help with encryption

    I changed the line where it converts the string to the Byte array, to use StrConv().

    Sure, I copy/pasted also, but when I recopy my own pasted example, it is then 7 characters. The Chr$(1) is missing. Are you saying it is still eight characters in the one I posted?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  10. #10
    Join Date
    Aug 2004
    Posts
    184

    Cool Re: Help with encryption

    Ok good job both of you, because Rndval is random everytime you encrypt, i might have to ajust the code when reading/decrypting, hopefully it will be the same length everytime.

    WizBang i sugest you check your settings, my browser can see all 8 chrs "enc" the 2 blocks and "áYq"

  11. #11
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Help with encryption

    Quote Originally Posted by cdrcools View Post
    Ok good job both of you, because Rndval is random everytime you encrypt, i might have to ajust the code when reading/decrypting, hopefully it will be the same length everytime.

    WizBang i sugest you check your settings, my browser can see all 8 chrs "enc" the 2 blocks and "áYq"
    If it's a Long, it will always be 4 bytes.

    I did check my browser settings, but of course we may not all be using the same browser. I use Firefox.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  12. #12
    Join Date
    Aug 2004
    Posts
    184

    Cool Re: Help with encryption

    OK, ive always used Internet Explorer.
    Well all i have to do now is configure the encrypt/decrypt process and put the data into a string and not a file.
    If you want a better understanding about what i'm trying to do and how it works check the attachment.
    Attached Files Attached Files

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

Featured