How To Encrypt and Decrypt password in Text File connection string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohammed saleh
    New Member
    • Apr 2013
    • 42

    #1

    How To Encrypt and Decrypt password in Text File connection string

    Show Error :
    Invalid character in a Base-64 string.
    How Can I get pass,ds,db,uid the value when writeline Textfile.Please help me
    this my code
    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports Microsoft.Win32
    Imports System.IO
    Imports System.Text
    Imports Microsoft.VisualBasic
    Public Class employee
        Private cons As String = ""
        Public Function writeconnectionstring() As String
            Try
                Dim ds As String = ""
                Dim db As String = ""
                Dim uid As String = ""
                Dim pass As String = ""
    
                Dim path As String = "C:\connection\tcon.txt"
                Dim sw As StreamWriter = New StreamWriter(path)
    
                sw.WriteLine(ds)
                sw.WriteLine(db)
                sw.WriteLine(uid)
                EncDecCls.EncryptClass.EncryptTripleDES(pass)
                sw.WriteLine(pass)
    
                cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
            Catch ex As FileNotFoundException
                MsgBox(ex.Message)
            End Try
            Return cons
        End Function
           
        Public Function Readconnectionstring() As String
            Try
                Dim pass As String
                Dim path As String = "C:\connection\tcon.txt"
                Dim sr As StreamReader = New StreamReader(path)
                Dim line As String = sr.ReadLine()
              
                Dim ds As String = sr.ReadLine()
                Dim db As String = sr.ReadLine()
                Dim uid As String = sr.ReadLine()
                pass = EncDecCls.EncryptClass.DecryptTripleDES(sr.ReadLine())-> [B][U]Error is here[/U][/B]
    
                cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
            Catch ex As FileNotFoundException
                MsgBox(ex.Message)
            End Try
            Return cons
        End Function
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Line 22 needs to be inside line 23.

    You also need to make sure that the encryption method returns a base64 formatted string because it looks like the decryption method expects a base64 input. If not, you will need to take the encryption output and format it to base64 first.

    But I don't understand how you are encrypting it without supplying a password.

    Comment

    • mohammed saleh
      New Member
      • Apr 2013
      • 42

      #3
      This my problem " I don't know how encrypting it supplying a password." please give me the answer

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #4
        Hey mohammed saleh, to protect your connection string, store your connection string in app.config/web.config file (depending on your project) then encrypt/decrypt it using "ProtectedConfig urationProvider". Refer the following link for more information.

        Securing Connection Strings

        Comment

        • mohammed saleh
          New Member
          • Apr 2013
          • 42

          #5
          Please vijay6 and Guids , I want Decrypt and Encrypt Text file
          pleas I am Learning just Decrypt and Encrypt Text file connection string
          please help for this ,I want it .

          Comment

          • vijay6
            New Member
            • Mar 2010
            • 158

            #6
            Hey mohammed saleh, what you want? Secure connection string or secure text file content?

            Comment

            • mohammed saleh
              New Member
              • Apr 2013
              • 42

              #7
              my text file is contain
              the connection string
              I want (Decrypt and encrypt) secure my text file contain connection string

              mr vijay6 this code is connect my database with text file :
              Code:
              Private cons As String = ""
              
                  Public Function Readconnectionstring() As String
                      Try
              
                          Dim path As String = "C:\connection\tcon.txt"
                          Dim sr As StreamReader = New StreamReader(path)
                          Dim line As String = sr.ReadLine()
              
                          Dim ds As String = sr.ReadLine()
                          Dim db As String = sr.ReadLine()
                          Dim uid As String = sr.ReadLine()
                          Dim pass As String = sr.ReadLine()
              
                          cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
                      Catch ex As FileNotFoundException
                          MsgBox(ex.Message)
                      End Try
                      Return cons
                  End Function
              thanks for you

              Comment

              • vijay6
                New Member
                • Mar 2010
                • 158

                #8
                Hey mohammed saleh, did you tried the way which is mentioned in the link which i posted before? You can do the same thing (encrypt and decrypt your connection string) with few steps very easily!!!

                Comment

                • mohammed saleh
                  New Member
                  • Apr 2013
                  • 42

                  #9
                  mr vijay6 I want to learning how to encrypt and decrypt

                  Text file(for connection string)
                  please tell me for this ,because I don't want using "ProtectedConfi gurationProvide r".
                  .I tried it but not succefully , even if it had succeeded, I do not want it I want to use the Text File. I want learn text file only

                  please give me a way for text file ,Tell me.

                  Thank's for you

                  Comment

                  • vijay6
                    New Member
                    • Mar 2010
                    • 158

                    #10
                    I tried it but not succefully , even if it had succeeded, I do not want it I want to use the Text File. I want learn text file only
                    Hey mohammed saleh, okay cool...

                    Which algorithm you choosed for encryption/decryption?

                    Comment

                    • mohammed saleh
                      New Member
                      • Apr 2013
                      • 42

                      #11
                      I don't know any algorithm but
                      I get you my code in my code I want to encrypt when write then Decrypt when read(Exampl: in my bin folder adde these files :
                      BLx.aspx.vb ,EncDecCls.dll, EncDecCls.pdb,E ncDecCls.xml .
                      i use this code Encdeccls.Encry ptclass.Encrept/decryptTrippleD ES )
                      Code:
                      Private cons As String = ""
                      
                          Public Function Readconnectionstring() As String
                              Try
                      
                                  Dim path As String = "C:\connection\tcon.txt"
                                  Dim sr As StreamReader = New StreamReader(path)
                                  Dim line As String = sr.ReadLine()
                      
                                  Dim ds As String = sr.ReadLine()
                                  Dim db As String = sr.ReadLine()
                                  Dim uid As String = sr.ReadLine()
                                  Dim pass As String = sr.ReadLine()
                      
                                  cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
                              Catch ex As FileNotFoundException
                                  MsgBox(ex.Message)
                              End Try
                              Return cons
                          End Function
                      give me any algorithm
                      Thanks

                      Comment

                      • vijay6
                        New Member
                        • Mar 2010
                        • 158

                        #12
                        I don't know any algorithm
                        You can use the algorithm from the following link...

                        Triple DES Encryption and Decryption using User provided key

                        Steps:

                        1) Choose a strong key and store it in your code.
                        2) Store your encrypted connection strings using the key in your text file.
                        3) Before connecting your database get all the details related to your database connection string from your text file and store it in the related variables after the decryption using the same key which you used for encryption.
                        4) Connect your database with the decrypted values.

                        Comment

                        • mohammed saleh
                          New Member
                          • Apr 2013
                          • 42

                          #13
                          mr vijay6 I don't want use text box and click button for encrypt or decrypt ,I want encrypt and decrypt connection database
                          for show my gridview (two columns ) for update and delete and insert column only
                          Thanks

                          Comment

                          • vijay6
                            New Member
                            • Mar 2010
                            • 158

                            #14
                            Hey mohammed saleh, i never asked you to do the same thing as what he does. Take that code as your reference and use it as per your wish.

                            Comment

                            • mohammed saleh
                              New Member
                              • Apr 2013
                              • 42

                              #15
                              ok Mr vijay6 ,
                              I don't want build textbox or button for encrypt and decrypt
                              .I want to show my gridview
                              how Can I use the functions creatkey , Encrypt and decrypt
                              for connection with select ,insert,update and delete

                              pleas Tell me
                              Thanks

                              Comment

                              Working...