Community discussion forum

Encrypt/Decrypt

This is a comment thread discussing Encrypt/Decrypt
  • 9 years ago

    This thread is for discussions of Encrypt/Decrypt.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 5 years ago

    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

  • 1 year ago

    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 CASESENSITIVEPASSWORD = False

    Private Sub cmdEncryptClick()
        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 CASESENSITIVEPASSWORD 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 CASESENSITIVEPASSWORD 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 CASESENSITIVEPASSWORD 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 CASESENSITIVEPASSWORD 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



















































































































































































































































































  • 1 year ago

    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











  • 1 year ago

    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.

     

     

  • 1 year ago

    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



























































  • 3 months ago

     looks good lemme try it

Post a reply

Enter your message below

Sign in or Join us (it's free).