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

    #31
    no my text file without encryption

    Comment

    • mohammed saleh
      New Member
      • Apr 2013
      • 42

      #32
      how can I containning mytext file connection string with encryption ?
      I want this please

      Comment

      • vijay6
        New Member
        • Mar 2010
        • 158

        #33
        Hey mohammed saleh, i gave you the code already. Did you read?

        Comment

        • mohammed saleh
          New Member
          • Apr 2013
          • 42

          #34
          ok but i don't know how to use code encrypt and decrypt with both
          show The same Error : bad data
          I think this example :
          Code:
          Imports System.Data
          Imports System.Data.SqlClient
          Imports Microsoft.Win32
          Imports System.IO
          Imports System.Text
          Imports System.Security.Cryptography
          Imports Microsoft.VisualBasic
          Public Class employee
              Private cons As String = ""
           
          public sub writeconnection() 
             Dim secretKey As String = "mohammed saleh"
                      Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
                      Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
                      Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
                      Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
           
                      Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
                      sw.WriteLine(DataSource)
                      sw.WriteLine(DataBase)
                      sw.WriteLine(UserID)
                      sw.WriteLine(Password)
                      sw.Close()
          end sub
          Private Function Encryption(PlainText As String, key As String) As Byte()
                      Dim des As TripleDES = CreateDES(key)
                      Dim ct As ICryptoTransform = des.CreateEncryptor()
                      Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
                      Return ct.TransformFinalBlock(input, 0, input.Length)
                  End Function
            Public Function Readconnectionstring() As String
                  Try
                      Dim secretkey As String = "Employee"
                      Dim sr As New StreamReader("C:\connection\tcon.txt")
           
                      Dim Server As String = sr.ReadLine()
                      Dim database As String = sr.ReadLine()
                      Dim userid As String = sr.ReadLine()
                      Dim password As String = sr.ReadLine()
                      sr.Close()
           
                      Dim ds As String = Decryption(Server, secretkey)
                      Dim db As String = Decryption(database, secretkey)
                      Dim uid As String = Decryption(userid, secretkey)
                      Dim pass As String = Decryption(password, secretkey)
           
                      cons = "Data Source =" & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
                  Catch ex As Exception 
                      MsgBox(ex.Message)->bad data
                  End Try
                  Return cons
              End Function
              Private Function createdes(ByVal key As String) As TripleDES
                  Dim md5 As MD5 = New MD5CryptoServiceProvider()
                  Dim des As TripleDES = New TripleDESCryptoServiceProvider()
                  des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
                  des.IV = New Byte(des.BlockSize \ 8 - 1) {}
                  Return des
              End Function
              Private Function Decryption(ByVal cyphertext As String, ByVal key As String) As String
                  Dim b As Byte() = Convert.FromBase64String(cyphertext)
                  Dim des As TripleDES = createdes(key)
                  Dim ct As ICryptoTransform = des.CreateDecryptor()
                  Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)->bad data
                  Return Encoding.Unicode.GetString(output)
              End Function
          
              Public Function emp() As DataTable
           
                  Dim cmd As New SqlCommand
                  Dim da As New SqlDataAdapter
                  Dim dt As New DataTable
                  Dim con As New SqlConnection()
                  Dim query As String = " select * from emp "
                  With cmd
                      .CommandType = CommandType.Text
                      .CommandText = query
                      .Connection = con
                  End With
                  da.SelectCommand = cmd
                con.Open()->connection property has not been  initialized 
                  da.Fill(dt)
                  con.Close()
                  Return dt
              End Function
          This correct or not encrypt and decrypt in the same class?

          Comment

          • mohammed saleh
            New Member
            • Apr 2013
            • 42

            #35
            Mr.vijay6
            show the same error : bad data
            I put code encrypt and decypt in the class
            you can show it in the link

            Comment

            • vijay6
              New Member
              • Mar 2010
              • 158

              #36
              ok but i don't know how to use code encrypt and decrypt with both
              Hey mohammed saleh, there is no need to use encryption and decryption in your application all time...

              Comment

              • mohammed saleh
                New Member
                • Apr 2013
                • 42

                #37
                mr vijay6
                I delayed reply because I was on holiday.
                what is the code I need for encryption and decryption in my application ?
                please help me

                Comment

                • mohammed saleh
                  New Member
                  • Apr 2013
                  • 42

                  #38
                  How can I encrypt and decrypt file coonection string without all time
                  please help me ,give me any thing

                  Comment

                  • vijay6
                    New Member
                    • Mar 2010
                    • 158

                    #39
                    Hey mohammed saleh, create a new project copy and paste the following code in your new project's form1. Don't forget to replace your secret code in line number 15, your server name in line number 16, your database name in line number 17, your user id in line number 18 and your password in line number 19. And create a new text file called "TextFile1. txt" in your "C:\Users\Your_ Name\Documents\ Visual Studio\Projects \Your_Project_N ame\Your_Projec t_Name\bin\Debu g\" directory. After this run your project. Now you can find your connection string with encrypted format. Copy the content of this text file and paste it in your main project's text file where you like to store your connection string.

                    Code:
                    Imports System
                    Imports System.IO
                    Imports System.Text
                    Imports System.Windows.Forms
                    Imports System.Security.Cryptography
                     
                    Namespace [EnD]
                        Public Partial Class Form1
                            Inherits Form
                            Public Sub New()
                                InitializeComponent()
                            End Sub
                     
                            Private Sub Form1_Load(sender As Object, e As EventArgs)
                                Dim secretKey As String = "Your_Secret_Key"
                                Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
                                Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
                                Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
                                Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
                     
                                Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
                                sw.WriteLine(DataSource)
                                sw.WriteLine(DataBase)
                                sw.WriteLine(UserID)
                                sw.WriteLine(Password)
                                sw.Close()
                            End Sub
                     
                            Private Function Encryption(PlainText As String, key As String) As Byte()
                                Dim des As TripleDES = CreateDES(key)
                                Dim ct As ICryptoTransform = des.CreateEncryptor()
                                Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
                                Return ct.TransformFinalBlock(input, 0, input.Length)
                            End Function
                     
                            Private Function CreateDES(key As String) As TripleDES
                                Dim md5 As MD5 = New MD5CryptoServiceProvider()
                                Dim des As TripleDES = New TripleDESCryptoServiceProvider()
                                des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
                                des.IV = New Byte(des.BlockSize / 8 - 1) {}
                                Return des
                            End Function
                        End Class
                    End Namespace

                    Once you done the above steps successfully then run your main application again with the following code which you said you had an error bad data. Now you wont get this error.

                    Comment

                    • mohammed saleh
                      New Member
                      • Apr 2013
                      • 42

                      #40
                      I put Encryption code in webform2.aspx
                      the decryption code in webfrom1.aspx .
                      My File is Encrypted succefully.
                      but not Decrypt ,I write code decrypt in my class and call it from insert update,delete,s elect for connection
                      show this error :
                      A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).
                      How Can I decrypted my file after encrypted succefully
                      This example for my code :
                      Code:
                      Imports System.Data
                      Imports System.Data.SqlClient
                      Imports Microsoft.Win32
                      Imports System.IO
                      Imports System.Text
                      Imports System.Security.Cryptography
                      Imports Microsoft.VisualBasic
                      Public Class employee
                          Private cons As String = ""
                        Private Function createdes(ByVal key As String) As TripleDES
                              Dim md5 As MD5 = New MD5CryptoServiceProvider()
                              Dim des As TripleDES = New TripleDESCryptoServiceProvider()
                              des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
                              des.IV = New Byte(des.BlockSize \ 8 - 1) {}
                              Return des
                          End Function
                           Public Function Readconnectionstring() As String
                              Try
                                  Dim secretkey As String = "Employee"
                      
                                  Dim sr As New StreamReader("C:\connection\tcon.txt")
                      
                                  Dim Server As String = sr.ReadLine()
                                  Dim database As String = sr.ReadLine()
                                  Dim userid As String = sr.ReadLine()
                                  Dim password As String = sr.ReadLine()
                      
                                  Dim ds As String = Decryption(Server, secretkey)
                                  Dim db As String = Decryption(database, secretkey)
                                  Dim uid As String = Decryption(userid, secretkey)
                                  Dim pass As String = Decryption(password, secretkey)
                      
                                  cons = "Data Source =" & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
                              Catch ex As Exception
                                  MsgBox(ex.Message)
                              End Try
                              Return cons
                          End Function
                        
                          Private Function Decryption(ByVal cyphertext As String, ByVal key As String) As String
                              Dim b As Byte() = Convert.FromBase64String(cyphertext)
                              Dim des As TripleDES = createdes(key)
                              Dim ct As ICryptoTransform = des.CreateDecryptor()
                              Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)
                              Return Encoding.Unicode.GetString(output)
                          End Function
                          Public Function emp() As DataTable
                      
                              Dim cmd As New SqlCommand
                              Dim da As New SqlDataAdapter
                              Dim dt As New DataTable
                              Dim con As New SqlConnection(Readconnectionstring())
                              Dim query As String = " select * from emp "
                              With cmd
                                  .CommandType = CommandType.Text
                                  .CommandText = query
                                  .Connection = con
                              End With
                              da.SelectCommand = cmd
                              con.Open()-> show  error
                              da.Fill(dt)
                              con.Close()
                              Return dt
                          End Function
                      Thanks.

                      Comment

                      • vijay6
                        New Member
                        • Mar 2010
                        • 158

                        #41
                        Hey mohammed saleh, you solved your problem or still having any error?

                        Comment

                        • mohammed saleh
                          New Member
                          • Apr 2013
                          • 42

                          #42
                          solved my problem .
                          Thank's Mr.vijay6

                          Comment

                          Working...