Results 1 to 13 of 13

Thread: encryption

  1. #1

    Thread Starter
    Hyperactive Member Filter300's Avatar
    Join Date
    Aug 2001
    Posts
    413

    encryption

    can someone tell me an easy way to encrypt text??

  2. #2
    Member Dimins's Avatar
    Join Date
    Sep 2001
    Location
    NY
    Posts
    42
    try this

    Code:
    'keyTxt is the encryption key
    'if you set directionTxt to "1" it will decrypt ... if it's set to 
    'anything else, it will encrypt ... (I know it's odd using a 
    'String as the direction variable, but I used this in an ASP page
    'and was passing in the direction via QueryString 
    iSeed=0
    For i=1 To Len(keyTxt)
        iSeed=iSeed + Asc(Mid(keyTxt, i, 1))
    Next
    Randomize(iSeed)
    decrypt=""
    		
    For i=1 To Len(messageTxt)
        if (directionTxt ="1") then
            decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) + (Int(Rnd() * 10))))
        else
            decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) - (Int(Rnd() * 10))))
        End If
    Next
    hope this helps ...

  3. #3

    Thread Starter
    Hyperactive Member Filter300's Avatar
    Join Date
    Aug 2001
    Posts
    413
    thanks for the help

  4. #4
    Member Dimins's Avatar
    Join Date
    Sep 2001
    Location
    NY
    Posts
    42
    and for some reason that doesn't work for me. I ran into this problem earlier and I thought I had solved the problem, btu I guess not. When you encrypt the string and then decrypt it without closing the form, you get the incorrect decryption ... if you encrypt the string, copy it, close the app, open the app, paste the encrypted string and decrypt it , it works! ... I thought it was a problem with the Randomize (iSeed) part, but I can't mail the problem down ... if anyone can see my problem, please help

    Code:
    'add this into a form with 3 Textboxes and a Command Button
    'Text1 is the message to encypt/decrypt
    'Text2 is the "1" or anything else (tells whetehrt o encrypt or not)
    'Text3 is the result
    Private Sub Command1_Click()
        Dim iSeed As Integer
        Dim keyTxt, messageTxt, decrypt, directionTxt As String
        keyText = "dog"
        messageTxt = Text1.Text
        directionTxt = Text2.Text
        iSeed = 0
        Randomize (iSeed)
        For i = 1 To Len(keyTxt)
            iSeed = iSeed + Asc(Mid(keyTxt, i, 1))
        Next
        decrypt = ""
        For i = 1 To Len(messageTxt)
            If (directionTxt = "1") Then
                decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) + (Int(Rnd() * 10))))
            Else
                decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) - (Int(Rnd() * 10))))
            End If
        Next
        Text3.Text = decrypt
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member Filter300's Avatar
    Join Date
    Aug 2001
    Posts
    413
    i tried this thing out... and i cant get it to work... it seems to encrypt it ok.. but it wont decrypt it....

  6. #6

    Thread Starter
    Hyperactive Member Filter300's Avatar
    Join Date
    Aug 2001
    Posts
    413
    i read your post again.. and it does work if i kill the app and paste in the encrypted text...

    anyone have a guess on what i can do to get around this?

  7. #7
    Member Dimins's Avatar
    Join Date
    Sep 2001
    Location
    NY
    Posts
    42
    sorry about the first post ... I did use that code in a DLL call ... it works for that because I a sense, the DLL ends after each call to it. I'll take another look at it and see if I can fix it ...

  8. #8
    Member Dimins's Avatar
    Join Date
    Sep 2001
    Location
    NY
    Posts
    42
    fixed it ...

    Code:
    Dim iSeed As Integer
        Dim keyTxt, messageTxt, decrypt, directionTxt As String
        keyText = "dog"
        messageTxt = Text1.Text
        directionTxt = Text2.Text
        iSeed = 0
        Rnd (-1)
        Randomize (iSeed)
        For i = 1 To Len(keyTxt)
            iSeed = iSeed + Asc(Mid(keyTxt, i, 1))
        Next
        decrypt = ""
        For i = 1 To Len(messageTxt)
            If (directionTxt = "1") Then
                decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) + (Int(Rnd() * 10))))
            Else
                decrypt = decrypt & Chr((Asc(Mid(messageTxt, i, 1)) - (Int(Rnd() * 10))))
            End If
        Next
        Text3.Text = decrypt

    found the following in an article
    Note: To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.

    let me know if that works

  9. #9

    Thread Starter
    Hyperactive Member Filter300's Avatar
    Join Date
    Aug 2001
    Posts
    413
    thanks! it works great

  10. #10
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: encryption

    Try this one. Same function encypting and opening strings.
    VB Code:
    1. Public SecretKey      as string
    2. Public SecretKeyValue as long
    3.  
    4. Public Function fcnSecret(YourString As String) As String
    5.    
    6.     SecretKey = "YourPassWord"
    7.     SecretKeyValue = 1212 'YourValue
    8.  
    9.     Dim n1 As Long, n2 As Long, n3 As Long, i As Integer
    10.    
    11.     For i = 1 To Len(SecretKey)
    12.         n1 = n1 + Asc(Mid(SecretKey, i, 1))
    13.         n1 = (n1 * 367 + 331) Mod &HFFF
    14.         n2 = ((n2 + n1) * 743 + 599) Mod &HFFF
    15.         n3 = ((n3 + n2) * 563 + 787) Mod &HFFF
    16.     Next
    17.    
    18.     fcnSecret = fcnKrypting(YourString, SecretKeyValue, n1, n2, n3)
    19.  
    20. End Function
    21. Private Function fcnKrypting(YourString As String, SecretKeyValue As Long, Optional a1 As Long, Optional a As Long, Optional b As Long) As String
    22.    
    23.     Static r As Long
    24.     Static m As Long
    25.     Static n As Long
    26.     Dim i    As Long
    27.     Dim c    As Long
    28.     Dim d    As Long
    29.    
    30.     On Error Resume Next
    31.    
    32.     If SecretKeyValue < 2 Then SecretKeyValue = 2
    33.    
    34.     If IsMissing(a1) = False Then r = a1
    35.     If IsMissing(a) Then
    36.         If m = 0 Then m = 69
    37.     Else
    38.         m = (a * 4 + 1) Mod SecretKeyValue
    39.     End If
    40.     If IsMissing(b) Then
    41.         If n = 0 Then n = 47
    42.     Else
    43.         n = (b * 2 + 1) Mod SecretKeyValue
    44.     End If
    45.    
    46.     For i = 1 To Len(YourString)
    47.         c = Asc(Mid(YourString, i, 1))
    48.         Select Case c
    49.         Case 48 To 57
    50.             d = c - 48
    51.         Case 63 To 90
    52.             d = c - 53
    53.         Case 97 To 250
    54.             d = c - 59
    55.         Case Else
    56.             d = -1
    57.         End Select
    58.         If d >= 0 Then
    59.             r = (r * m + n) Mod SecretKeyValue
    60.             d = (r And 63) Xor d
    61.             Select Case d
    62.             Case 0 To 9
    63.                 c = d + 48
    64.             Case 10 To 37
    65.                 c = d + 53
    66.             Case 38 To 191
    67.                 c = d + 59
    68.             End Select
    69.             Mid(YourString, i, 1) = Chr(c)
    70.         End If
    71.     Next
    72.    
    73.      fcnKrypting = YourString
    74.  
    75. End Function

    oh1mie/Vic
    Last edited by oh1mie; May 21st, 2002 at 04:52 AM.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    Code:
    static r As Long
    could someone explain what static does and when you should use it as opposed to dim or pulic or const?


    thanks
    ______________

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Static is used a the procedure level. It will retain its value after the procedure has finished. It acts like dimming a variable at modular level.
    I prefer to keep the scope of a variable as narrow as possible, so I use static when I need to retain a value between calls to a procedure.

    Give this a try, it may clears somethings up.
    VB Code:
    1. Option Explicit
    2. Dim x As Integer
    3.  
    4. Private Sub Form_Load()
    5.    Timer1.Interval = 1000
    6.    Timer1.Enabled = True
    7. End Sub
    8.  
    9. Private Sub Timer1_Timer()
    10.    Dim y As Integer
    11.    Static z As Integer
    12.    
    13.    x = x + 1
    14.    y = y + 1
    15.    z = z + 1
    16.    
    17.    If x > 4 Or _
    18.       y > 4 Or _
    19.       z > 4 Then
    20.      
    21.       MsgBox "X = " & x & vbCrLf _
    22.             & "Y = " & y & vbCrLf _
    23.             & "Z + " & z
    24.       Timer1.Enabled = False
    25.    End If
    26. End Sub

  13. #13
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Heres some more...this is some pretty cheap symetrical Xor encryption. So call the same function with the same key on the cipher and you'll get the text back.
    VB Code:
    1. Function Enc(iText As String, Key As String) As String
    2. 'Very Simple Encryption
    3. 'Symetrical, Xor encryption. Simply text xor key. nothing fancy
    4.     Dim StrOut As String
    5.     Dim i As Long
    6.     Dim StrLen As Long
    7.     Dim KeyLen As Long
    8.  
    9.     StrLen = Len(iText)
    10.     KeyLen = Len(Key)
    11.  
    12.     For i = 1 To StrLen
    13.         StrOut = StrOut & Chr(Asc(Mid(iText, i, 1)) Xor Asc(Mid(Key, i Mod KeyLen + 1, 1)))
    14.     Next
    15.     Enc = StrOut
    16. End Function
    You just proved that sig advertisements work.

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