Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[157] Encrypt/Decrypt

Last post 08-11-2008 8:32 PM by sledghammer64. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [157] Encrypt/Decrypt

    This thread is for discussions of Encrypt/Decrypt.

    • Post Points: 25
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 09-30-2003 2:45 PM In reply to

    • miglou
    • Not Ranked
    • Joined on 09-30-2003
    • New Member
    • Points 5

    error reading encrypted word

    Hello,
    i'm doing a small app to create encrypt keys, and i'm used your example, except in Chr$ i got an error and just used Chr, and it works fine.the problem is, after create encrypt word, and saved to a file, when reading the encrypted word i can't decrypt.......any idea???
    thanks very much for your time
    • Post Points: 0
  • 02-11-2007 9:57 AM In reply to

    Re: [157] Encrypt/Decrypt

    The problem with the chr(0) can be solved by this process.

    The pseudocode is like this. In the final encrypted text, the chr(0) cuts the rest of the string. We can
    mark those positions and decrypt accordingly.

    In our encrypted text we can include these characters in the front.

    flag & noOfFaults & pos(1), .....pos(n),

    flag indicated that the problem occured. noofFaults indicated the total number of faults and pos(1),..pos(n) indicates the positions of faults. In the decryption process we will first replace those positions with chr(0) and decrypt accordingly.


    Option Explicit

    'Set to True to make the password case-sensitive
    #Const CASE_SENSITIVE_PASSWORD = False

    Private Sub cmdEncrypt_Click()
        Dim buffText As String
        Dim lngFileEnd As Integer
        
        ' You can encrypt twice for extra security
        buffText = EncryptText((txtText), txtPassword)
        buffText = EncryptTextFinal((buffText), txtPassword)
        
        Dim path As String
        path = App.path & "\a.txt"
        'writing the contents to a file
            'opening the file and writing contents
            Open path For Binary Access Write As #1
         
            lngFileEnd = LOF(1) + 1
         'putting data into file
            Put #1, lngFileEnd, buffText
        Close #1
        
        txtText = buffText
        
        cmdEncrypt.Enabled = False
        cmdDecrypt.Enabled = True
        
    End Sub

    Private Sub cmdDecrypt_Click()
        txtText = DecryptTextFault((txtText), txtPassword)
        txtText = DecryptText((txtText), txtPassword)
        
        cmdEncrypt.Enabled = True
        cmdDecrypt.Enabled = False
    End Sub

    'Encrypt text finally
    Private Function EncryptTextFinal(strText As String, ByVal strPwd As String)
        Dim i As Integer, c As Integer
        Dim strBuff As String
        
        Dim jhamela As Integer
        Dim noFault As Integer

          jhamela = 0
          noFault = 0
        
    #If Not CASE_SENSITIVE_PASSWORD Then

        'Convert password to upper case
        'if not case-sensitive
        strPwd = UCase(strPwd)

    #End If

        'Encrypt string
        If Len(strPwd) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c + Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
                
                Dim addBuff As String
                
                'checking for faulty position
                
                If Chr(c And &HFF) = Chr(0) Then
                  'marking of a faulty portion
                  jhamela = 1
                  noFault = noFault + 1
                
                  addBuff = Trim(addBuff) & Trim(Str(i)) & ","
                  
                  
                  strBuff = strBuff & "0"
                Else
                  strBuff = strBuff & Chr(c And &HFF)
                End If
                
            Next i
        End If
            
            Dim jhBuff As String
            
        If jhamela = 1 Then
          jhBuff = "1"
          strBuff = Trim(jhBuff) & Trim(Str(noFault)) & "," & Trim(addBuff) & Trim(strBuff)
        Else
          jhBuff = "0"
          strBuff = Trim(jhBuff) & Trim(strBuff)
        End If

        EncryptTextFinal = strBuff
        
    End Function


    'Encrypt text
    Private Function EncryptText(strText As String, ByVal strPwd As String)
        Dim i As Integer, c As Integer
        Dim strBuff As String
        
        Dim jhamela As Integer
        Dim noFault As Integer

          jhamela = 0
          noFault = 0
        
    #If Not CASE_SENSITIVE_PASSWORD Then

        'Convert password to upper case
        'if not case-sensitive
        strPwd = UCase(strPwd)

    #End If

        'Encrypt string
        If Len(strPwd) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c + Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
               
            Next i
        Else
            strBuff = strText
        End If
        EncryptText = strBuff
    End Function

    'Decrypt text encrypted with EncryptText
    Private Function DecryptText(strText As String, ByVal strPwd As String)
        Dim i As Integer, c As Integer
        Dim strBuff As String

    #If Not CASE_SENSITIVE_PASSWORD Then

        'Convert password to upper case
        'if not case-sensitive
        strPwd = UCase(strPwd)

    #End If

        

        'Decrypt string
        If Len(strPwd) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c - Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next i
        Else
            strBuff = strText
        End If
        DecryptText = strBuff
    End Function

    'Decrypt text encrypted with EncryptText
    Private Function DecryptTextFault(strText As String, ByVal strPwd As String)
        Dim i As Integer, c As Integer
        Dim strBuff As String

    #If Not CASE_SENSITIVE_PASSWORD Then

        'Convert password to upper case
        'if not case-sensitive
        strPwd = UCase(strPwd)

    #End If

        'getting the first portion and finding the faulty positions
        Dim jhText As String
        Dim mainText As String
        Dim noFault As Integer
        Dim pos(100) As Integer
        
        mainText = strText
        
        jhText = Mid(strText, 1, 1)
        'MsgBox jhText
        
        If jhText = "1" Then
          'getting the jhamelas
          
          Dim jhNo As String
          jhNo = ""
          
          Dim j As Integer
          j = 2
          Do
            
            jhNo = jhNo & Trim(Mid(mainText, j, 1))
            j = j + 1
            
          Loop While Mid(mainText, j, 1) <> ","
          
          noFault = CInt(jhNo)
          
          
          'looping through the maintext for finding the positions
            'MsgBox j
          For i = 1 To noFault
            Do
              
              pos(i) = pos(i) & Trim(Mid(mainText, j, 1))
              j = j + 1
              
            Loop While Mid(mainText, j, 1) <> ","
            
            'MsgBox pos(i) & "and current j " & j
            
          Next i
     
          'now correction of the main text and getting our desired text
          
          mainText = Mid(mainText, j + 1, Len(mainText))
          
          j = 1 'updating the current position
          
          'going to faulty positions and correcting those
          Dim correctedMainText As String
          
          correctedMainText = ""
                     
            For i = 1 To noFault
              j = pos(i)
              'previous text
              correctedMainText = Trim(Mid(mainText, 1, j - 1))
              'faulty text
              correctedMainText = correctedMainText & Chr(0)
              'trailing text
              correctedMainText = correctedMainText & Trim(Mid(mainText, j + 1, Len(mainText)))
              
              mainText = correctedMainText
            Next i
            
            mainText = correctedMainText

          
          
        Else
          mainText = Mid(strText, 2, Len(strText))
        End If

        'Decrypt string
        If Len(strPwd) Then
            For i = 1 To Len(mainText)
                c = Asc(Mid(mainText, i, 1))
                c = c - Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next i
        Else
            strBuff = strText
        End If
        DecryptTextFault = strBuff
    End Function



















































































































































































































































































    • Post Points: 5
  • 04-20-2007 12:19 PM In reply to

    • ajanu81
    • Not Ranked
    • Joined on 04-20-2007
    • India
    • New Member
    • Points 10

    Re: [157] Encrypt/Decrypt

    hi,

    I have been trying to use your code for my application in C#,but i am less successful.

    Could you please post the code in C#?

    Thanks in advance.

    Regards

    Anuradha











    • Post Points: 10
  • 07-11-2007 3:47 PM In reply to

    • ashnir
    • Not Ranked
    • Joined on 07-11-2007
    • United States
    • New Member
    • Points 5

    Re: [157] Encrypt/Decrypt

    hi,

    i implemented the code and it encrypts correctly,but when i decrypt it it still gives me weird characters and not the string that i had passed to encrypt function.

    Can you please guide me in the same.

     

     

    • Post Points: 5
  • 08-22-2007 9:04 AM In reply to

    • amilath
    • Not Ranked
    • Joined on 08-22-2007
    • Sri Lanka
    • New Member
    • Points 5

    Re: [157] Encrypt/Decrypt

    Dear Anuradha

    This  is the  C#  Code

    class Class1
        {
            // Encryption Method
            public object EncryptText(string strText, string strPwd)
            {
                int i;
                int c;
                string strBuff = "";


                // Convert User Password to upper case
                strPwd = strPwd.ToUpper();

                for (i = 0; i < (strText.Length); i++)
                {
                    c = Convert.ToInt32(char.Parse(strText.Substring(i, 1)));
                    c = c + Convert.ToInt32(char.Parse(strPwd.Substring((i % (strPwd.Length)), 1)));

                    strBuff = strBuff + Chr(c & 255);
                }
                return strBuff;
            }

            // Decryption Method
            public object DecryptText(string strText, string strPwd)
            {
                int i;
                int c;
                string strBuff = "";


                // Convert User Password to upper case
                strPwd = strPwd.ToUpper();

                for (i = 0; i < (strText.Length); i++)
                {
                    c = Convert.ToInt32(char.Parse(strText.Substring(i, 1)));
                    c = c - Convert.ToInt32(char.Parse(strPwd.Substring((i % (strPwd.Length)), 1)));

                    strBuff = strBuff + Chr(c & 255);
                }
                return strBuff;
            }


            char Chr(int n)
            {
                return (char)n;
            }
        }

    regards


    Amila



























































    • Post Points: 5
  • 08-11-2008 8:32 PM In reply to

    Re: [157] Encrypt/Decrypt

     looks good lemme try it

    • Post Points: 0
Page 1 of 1 (7 items)