473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Microsoft.Win32
  4. Imports System.IO
  5. Imports System.Text
  6. Imports Microsoft.VisualBasic
  7. Public Class employee
  8.     Private cons As String = ""
  9.     Public Function writeconnectionstring() As String
  10.         Try
  11.             Dim ds As String = ""
  12.             Dim db As String = ""
  13.             Dim uid As String = ""
  14.             Dim pass As String = ""
  15.  
  16.             Dim path As String = "C:\connection\tcon.txt"
  17.             Dim sw As StreamWriter = New StreamWriter(path)
  18.  
  19.             sw.WriteLine(ds)
  20.             sw.WriteLine(db)
  21.             sw.WriteLine(uid)
  22.             EncDecCls.EncryptClass.EncryptTripleDES(pass)
  23.             sw.WriteLine(pass)
  24.  
  25.             cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  26.         Catch ex As FileNotFoundException
  27.             MsgBox(ex.Message)
  28.         End Try
  29.         Return cons
  30.     End Function
  31.  
  32.     Public Function Readconnectionstring() As String
  33.         Try
  34.             Dim pass As String
  35.             Dim path As String = "C:\connection\tcon.txt"
  36.             Dim sr As StreamReader = New StreamReader(path)
  37.             Dim line As String = sr.ReadLine()
  38.  
  39.             Dim ds As String = sr.ReadLine()
  40.             Dim db As String = sr.ReadLine()
  41.             Dim uid As String = sr.ReadLine()
  42.             pass = EncDecCls.EncryptClass.DecryptTripleDES(sr.ReadLine())-> Error is here
  43.  
  44.             cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  45.         Catch ex As FileNotFoundException
  46.             MsgBox(ex.Message)
  47.         End Try
  48.         Return cons
  49.     End Function
Apr 23 '13 #1

✓ answered by vijay6

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_Name\Your_Project_Nam e\bin\Debug\" 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.

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Text
  4. Imports System.Windows.Forms
  5. Imports System.Security.Cryptography
  6.  
  7. Namespace [EnD]
  8.     Public Partial Class Form1
  9.         Inherits Form
  10.         Public Sub New()
  11.             InitializeComponent()
  12.         End Sub
  13.  
  14.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  15.             Dim secretKey As String = "Your_Secret_Key"
  16.             Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
  17.             Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
  18.             Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
  19.             Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
  20.  
  21.             Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
  22.             sw.WriteLine(DataSource)
  23.             sw.WriteLine(DataBase)
  24.             sw.WriteLine(UserID)
  25.             sw.WriteLine(Password)
  26.             sw.Close()
  27.         End Sub
  28.  
  29.         Private Function Encryption(PlainText As String, key As String) As Byte()
  30.             Dim des As TripleDES = CreateDES(key)
  31.             Dim ct As ICryptoTransform = des.CreateEncryptor()
  32.             Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
  33.             Return ct.TransformFinalBlock(input, 0, input.Length)
  34.         End Function
  35.  
  36.         Private Function CreateDES(key As String) As TripleDES
  37.             Dim md5 As MD5 = New MD5CryptoServiceProvider()
  38.             Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  39.             des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  40.             des.IV = New Byte(des.BlockSize / 8 - 1) {}
  41.             Return des
  42.         End Function
  43.     End Class
  44. 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.

http://bytes.com/topic/asp-net/answe...ng#post3748664

41 4051
Rabbit
12,516 Expert Mod 8TB
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.
Apr 23 '13 #2
This my problem " I don't know how encrypting it supplying a password." please give me the answer
Apr 24 '13 #3
vijay6
158 100+
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 "ProtectedConfigurationProvider". Refer the following link for more information.

Securing Connection Strings
Apr 24 '13 #4
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 .
Apr 24 '13 #5
vijay6
158 100+
Hey mohammed saleh, what you want? Secure connection string or secure text file content?
Apr 24 '13 #6
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 :
Expand|Select|Wrap|Line Numbers
  1. Private cons As String = ""
  2.  
  3.     Public Function Readconnectionstring() As String
  4.         Try
  5.  
  6.             Dim path As String = "C:\connection\tcon.txt"
  7.             Dim sr As StreamReader = New StreamReader(path)
  8.             Dim line As String = sr.ReadLine()
  9.  
  10.             Dim ds As String = sr.ReadLine()
  11.             Dim db As String = sr.ReadLine()
  12.             Dim uid As String = sr.ReadLine()
  13.             Dim pass As String = sr.ReadLine()
  14.  
  15.             cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  16.         Catch ex As FileNotFoundException
  17.             MsgBox(ex.Message)
  18.         End Try
  19.         Return cons
  20.     End Function
thanks for you
Apr 24 '13 #7
vijay6
158 100+
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!!!
Apr 24 '13 #8
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 "ProtectedConfigurationProvider".
.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
Apr 24 '13 #9
vijay6
158 100+
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?
Apr 24 '13 #10
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,EncDecCls.xml .
i use this code Encdeccls.Encryptclass.Encrept/decryptTrippleDES )
Expand|Select|Wrap|Line Numbers
  1. Private cons As String = ""
  2.  
  3.     Public Function Readconnectionstring() As String
  4.         Try
  5.  
  6.             Dim path As String = "C:\connection\tcon.txt"
  7.             Dim sr As StreamReader = New StreamReader(path)
  8.             Dim line As String = sr.ReadLine()
  9.  
  10.             Dim ds As String = sr.ReadLine()
  11.             Dim db As String = sr.ReadLine()
  12.             Dim uid As String = sr.ReadLine()
  13.             Dim pass As String = sr.ReadLine()
  14.  
  15.             cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  16.         Catch ex As FileNotFoundException
  17.             MsgBox(ex.Message)
  18.         End Try
  19.         Return cons
  20.     End Function
give me any algorithm
Thanks
Apr 24 '13 #11
vijay6
158 100+
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.
Apr 24 '13 #12
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
Apr 24 '13 #13
vijay6
158 100+
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.
Apr 24 '13 #14
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
Apr 24 '13 #15
vijay6
158 100+
I want to show my gridview
Hey mohammed saleh, what you want to show inside the gridview?
Apr 24 '13 #16
Mr vijay6 , I want to encrypt and decrypt password
in text file connection string

this example encrypt and decrypt text it doesn't encrypt and decrypt password
Apr 24 '13 #17
Mr vijay6 ,
I want show two column(id and name) for insert , update and delete
Apr 24 '13 #18
vijay6
158 100+
Hey mohammed saleh, first choose a secret key. In your first form inside load the event call the encryption function along with your connection string and secret key as arguments. After that store the encrypted connection string details in your text file. Then close your application. Now you've encrypted connection string details in your text file. (Probably hereafter you won't need encryption function. If you want you can remove it.) After this whenever you are trying to connect your database, open your text file and read the content and pass those values to decryption function along with your secret which you used to encryption which will give you the original connection string. Finally use this connection string to connect your database.

Note: Don't store your secret key in your text file. Store it in the application settings or inside the decryption function itself.
Apr 24 '13 #19
mr vijay6
how can I define secret key in my code vb.net

and Can you give me code example for your last post

Thanks
Apr 24 '13 #20
vijay6
158 100+
Hey mohammed saleh, you can use the following code for encryption. i.e., To store the encrypted connection string details in a text file. (You need this code only one time.)

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Text
  4. Imports System.Windows.Forms
  5. Imports System.Security.Cryptography
  6.  
  7. Namespace [EnD]
  8.     Public Partial Class Form1
  9.         Inherits Form
  10.         Public Sub New()
  11.             InitializeComponent()
  12.         End Sub
  13.  
  14.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  15.             Dim secretKey As String = "mohammed saleh"
  16.             Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
  17.             Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
  18.             Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
  19.             Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
  20.  
  21.             Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
  22.             sw.WriteLine(DataSource)
  23.             sw.WriteLine(DataBase)
  24.             sw.WriteLine(UserID)
  25.             sw.WriteLine(Password)
  26.             sw.Close()
  27.         End Sub
  28.  
  29.         Private Function Encryption(PlainText As String, key As String) As Byte()
  30.             Dim des As TripleDES = CreateDES(key)
  31.             Dim ct As ICryptoTransform = des.CreateEncryptor()
  32.             Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
  33.             Return ct.TransformFinalBlock(input, 0, input.Length)
  34.         End Function
  35.  
  36.         Private Function CreateDES(key As String) As TripleDES
  37.             Dim md5 As MD5 = New MD5CryptoServiceProvider()
  38.             Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  39.             des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  40.             des.IV = New Byte(des.BlockSize / 8 - 1) {}
  41.             Return des
  42.         End Function
  43.     End Class
  44. End Namespace

For decryption you can use the following code. i.e., To read the encrypted connection string from the text file and decrypt that details and to connect to the database.

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Data.SqlClient
  3. Imports System.IO
  4. Imports System.Text
  5. Imports System.Windows.Forms
  6. Imports System.Security.Cryptography
  7.  
  8. Namespace [EnD]
  9.     Public Partial Class Form1
  10.         Inherits Form
  11.         Public Sub New()
  12.             InitializeComponent()
  13.         End Sub
  14.  
  15.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  16.             Dim secretKey As String = "mohammed saleh"
  17.  
  18.             Dim sr As New StreamReader(Application.StartupPath + "\" + "TextFile1.txt")
  19.             Dim Server As String = sr.ReadLine()
  20.             Dim DataBase As String = sr.ReadLine()
  21.             Dim UserID As String = sr.ReadLine()
  22.             Dim Password As String = sr.ReadLine()
  23.             sr.Close()
  24.  
  25.             Dim dServer As String = Decryption(Server, secretKey)
  26.             Dim dDataBase As String = Decryption(DataBase, secretKey)
  27.             Dim dUserID As String = Decryption(UserID, secretKey)
  28.             Dim dPassword As String = Decryption(Password, secretKey)
  29.  
  30.             Dim con As New SqlConnection("Server=" + dServer + ";Database=" + dDataBase + ";User ID=" + dUserID + ";Password=" + dPassword + ";")
  31.  
  32.             Try
  33.                 con.Open()
  34.                 MessageBox.Show("Connection Successful")
  35.             Catch ex As Exception
  36.                 MessageBox.Show(ex.ToString())
  37.             Finally
  38.                 con.Close()
  39.             End Try
  40.         End Sub
  41.  
  42.         Private Function Decryption(CypherText As String, key As String) As String
  43.             Dim b As Byte() = Convert.FromBase64String(CypherText)
  44.             Dim des As TripleDES = CreateDES(key)
  45.             Dim ct As ICryptoTransform = des.CreateDecryptor()
  46.             Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)
  47.             Return Encoding.Unicode.GetString(output)
  48.         End Function
  49.  
  50.         Private Function CreateDES(key As String) As TripleDES
  51.             Dim md5 As MD5 = New MD5CryptoServiceProvider()
  52.             Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  53.             des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  54.             des.IV = New Byte(des.BlockSize / 8 - 1) {}
  55.             Return des
  56.         End Function
  57.     End Class
  58. End Namespace
Apr 24 '13 #21
mr vijay6
Can I use just decrypt (I want to connect database)

Can I use Encrypt and Decrypt both for my Text file
connection string ?

Thanks for you
Apr 25 '13 #22
vijay6
158 100+
Hey mohammed saleh, your text file containing the encrypted database connection string details. And you program having decryption algorithm with your secret key. If you want to connect your database read your text file pass the encrypted details to the decryption function and connect your database with decrypted details. So only i said you need encryption algorithm only one time.
Apr 25 '13 #23
OK mr vijay6
But I have class for property and class for methods:
Function select
subs insert ,update and delete
when I put decrypt in my pageload , how the insert ,update and delete connect database for do.
i used con as new sqlconnction . This my Example For Function and sub :
Expand|Select|Wrap|Line Numbers
  1. Public Function emp() As DataTable
  2.         Dim cmd As New SqlCommand
  3.         Dim da As New SqlDataAdapter
  4.         Dim dt As New DataTable
  5.         Dim con As New SqlConnection(Readconnectionstring())->this get my connection
  6.         Dim query As String = " select * from emp "
  7.         With cmd
  8.             .CommandType = CommandType.Text
  9.             .CommandText = query
  10.             .Connection = con
  11.         End With
  12.         da.SelectCommand = cmd
  13.         con.Open()
  14.         da.Fill(dt)
  15.         con.Close()
  16.         Return dt
  17.     End Function
  18.  
  19.     Public Sub update(ByVal id As Integer, ByVal name As String)
  20.  
  21.         Dim cmd As New SqlCommand
  22.         Dim da As New SqlDataAdapter
  23.         Dim con As New SqlConnection(Readconnectionstring())->this get my connection
  24.         Dim query As String = "update emp set empname='" & name & "'" & _
  25.        "  where empid = " & id
  26.  
  27.  
  28.         With cmd
  29.             .CommandType = CommandType.Text
  30.             .CommandText = query
  31.             .Connection = con-> get object connection
  32.  
  33.         End With
  34.         da.SelectCommand = cmd
  35.         con.Open()
  36.         cmd.ExecuteNonQuery()
  37.         con.Close()
  38.   End Sub
Apr 25 '13 #24
vijay6
158 100+
Hey mohammed saleh, you can declare SqlConnection 'con' inside your class and define the value for 'con' inside your page load event (There is no need to decrypt the connection string for all different kind of operations like select, insert, update, delete and etc.,). You can use like this...

Expand|Select|Wrap|Line Numbers
  1. Public Partial Class WebForm1
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Private con As SqlConnection
  5.  
  6.     Protected Sub Page_Load(sender As Object, e As EventArgs)
  7.  
  8.         con = New SqlConnection(Readconnectionstring())
  9.  
  10.     End Sub
  11. End Class
Apr 25 '13 #25
mr vijay6
show error : Bad data in this line
Dim output As Byte() = it.TransformFinalBlock(b, 0, b.Length)
Apr 25 '13 #26
How can I resolve this error : Bad data
Apr 25 '13 #27
vijay6
158 100+
Bad data in this line
Dim output As Byte() = it.TransformFinalBlock(b, 0, b.Length)
Hey mohammed saleh, where you've this line?
Apr 25 '13 #28
I put for you my code :
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Microsoft.Win32
  4. Imports System.IO
  5. Imports System.Text
  6. Imports System.Security.Cryptography
  7. Imports Microsoft.VisualBasic
  8. Public Class employee
  9.     Private cons As String = ""
  10.  
  11.     Public Function Readconnectionstring() As String
  12.         Try
  13.             Dim secretkey As String = "Employee"
  14.             Dim sr As New StreamReader("C:\connection\tcon.txt")
  15.  
  16.             Dim Server As String = sr.ReadLine()
  17.             Dim database As String = sr.ReadLine()
  18.             Dim userid As String = sr.ReadLine()
  19.             Dim password As String = sr.ReadLine()
  20.             sr.Close()
  21.  
  22.             Dim ds As String = Decryption(Server, secretkey)
  23.             Dim db As String = Decryption(database, secretkey)
  24.             Dim uid As String = Decryption(userid, secretkey)
  25.             Dim pass As String = Decryption(password, secretkey)
  26.  
  27.             cons = "Data Source =" & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  28.         Catch ex As Exception 
  29.             MsgBox(ex.Message)->bad data
  30.         End Try
  31.         Return cons
  32.     End Function
  33.     Private Function createdes(ByVal key As String) As TripleDES
  34.         Dim md5 As MD5 = New MD5CryptoServiceProvider()
  35.         Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  36.         des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  37.         des.IV = New Byte(des.BlockSize \ 8 - 1) {}
  38.         Return des
  39.     End Function
  40.     Private Function Decryption(ByVal cyphertext As String, ByVal key As String) As String
  41.         Dim b As Byte() = Convert.FromBase64String(cyphertext)
  42.         Dim des As TripleDES = createdes(key)
  43.         Dim ct As ICryptoTransform = des.CreateDecryptor()
  44.         Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)->Bad Data
  45.         Return Encoding.Unicode.GetString(output)
  46.     End Function
  47.     Public Function emp() As DataTable
  48.  
  49.         Dim cmd As New SqlCommand
  50.         Dim da As New SqlDataAdapter
  51.         Dim dt As New DataTable
  52.         Dim con As New SqlConnection(Readconnectionstring())
  53.         Dim query As String = " select * from emp "
  54.         With cmd
  55.             .CommandType = CommandType.Text
  56.             .CommandText = query
  57.             .Connection = con
  58.         End With
  59.         da.SelectCommand = cmd
  60.       con.Open()->Conncection property hasn't been initialized
  61.         da.Fill(dt)
  62.         con.Close()
  63.         Return dt
  64.     End Function
Apr 25 '13 #29
vijay6
158 100+
Hey mohammed saleh, your text file 'C:\connection\tcon.txt' containing your connection string information with encryption or without encryption?
Apr 25 '13 #30
no my text file without encryption
Apr 25 '13 #31
how can I containning mytext file connection string with encryption ?
I want this please
Apr 25 '13 #32
vijay6
158 100+
Hey mohammed saleh, i gave you the code already. Did you read?

http://bytes.com/topic/asp-net/answe...ng#post3748574
Apr 25 '13 #33
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 :
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Microsoft.Win32
  4. Imports System.IO
  5. Imports System.Text
  6. Imports System.Security.Cryptography
  7. Imports Microsoft.VisualBasic
  8. Public Class employee
  9.     Private cons As String = ""
  10.  
  11. public sub writeconnection() 
  12.    Dim secretKey As String = "mohammed saleh"
  13.             Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
  14.             Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
  15.             Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
  16.             Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
  17.  
  18.             Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
  19.             sw.WriteLine(DataSource)
  20.             sw.WriteLine(DataBase)
  21.             sw.WriteLine(UserID)
  22.             sw.WriteLine(Password)
  23.             sw.Close()
  24. end sub
  25. Private Function Encryption(PlainText As String, key As String) As Byte()
  26.             Dim des As TripleDES = CreateDES(key)
  27.             Dim ct As ICryptoTransform = des.CreateEncryptor()
  28.             Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
  29.             Return ct.TransformFinalBlock(input, 0, input.Length)
  30.         End Function
  31.   Public Function Readconnectionstring() As String
  32.         Try
  33.             Dim secretkey As String = "Employee"
  34.             Dim sr As New StreamReader("C:\connection\tcon.txt")
  35.  
  36.             Dim Server As String = sr.ReadLine()
  37.             Dim database As String = sr.ReadLine()
  38.             Dim userid As String = sr.ReadLine()
  39.             Dim password As String = sr.ReadLine()
  40.             sr.Close()
  41.  
  42.             Dim ds As String = Decryption(Server, secretkey)
  43.             Dim db As String = Decryption(database, secretkey)
  44.             Dim uid As String = Decryption(userid, secretkey)
  45.             Dim pass As String = Decryption(password, secretkey)
  46.  
  47.             cons = "Data Source =" & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  48.         Catch ex As Exception 
  49.             MsgBox(ex.Message)->bad data
  50.         End Try
  51.         Return cons
  52.     End Function
  53.     Private Function createdes(ByVal key As String) As TripleDES
  54.         Dim md5 As MD5 = New MD5CryptoServiceProvider()
  55.         Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  56.         des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  57.         des.IV = New Byte(des.BlockSize \ 8 - 1) {}
  58.         Return des
  59.     End Function
  60.     Private Function Decryption(ByVal cyphertext As String, ByVal key As String) As String
  61.         Dim b As Byte() = Convert.FromBase64String(cyphertext)
  62.         Dim des As TripleDES = createdes(key)
  63.         Dim ct As ICryptoTransform = des.CreateDecryptor()
  64.         Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)->bad data
  65.         Return Encoding.Unicode.GetString(output)
  66.     End Function
  67.  
  68.     Public Function emp() As DataTable
  69.  
  70.         Dim cmd As New SqlCommand
  71.         Dim da As New SqlDataAdapter
  72.         Dim dt As New DataTable
  73.         Dim con As New SqlConnection()
  74.         Dim query As String = " select * from emp "
  75.         With cmd
  76.             .CommandType = CommandType.Text
  77.             .CommandText = query
  78.             .Connection = con
  79.         End With
  80.         da.SelectCommand = cmd
  81.       con.Open()->connection property has not been  initialized 
  82.         da.Fill(dt)
  83.         con.Close()
  84.         Return dt
  85.     End Function
This correct or not encrypt and decrypt in the same class?
Apr 25 '13 #34
Mr.vijay6
show the same error : bad data
I put code encrypt and decypt in the class
you can show it in the link
http://bytes.com/topic/asp-net/answe...ng#post3748664
Apr 25 '13 #35
vijay6
158 100+
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...
Apr 25 '13 #36
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
Apr 28 '13 #37
How can I encrypt and decrypt file coonection string without all time
please help me ,give me any thing
Apr 28 '13 #38
vijay6
158 100+
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_Name\Your_Project_Nam e\bin\Debug\" 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.

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.Text
  4. Imports System.Windows.Forms
  5. Imports System.Security.Cryptography
  6.  
  7. Namespace [EnD]
  8.     Public Partial Class Form1
  9.         Inherits Form
  10.         Public Sub New()
  11.             InitializeComponent()
  12.         End Sub
  13.  
  14.         Private Sub Form1_Load(sender As Object, e As EventArgs)
  15.             Dim secretKey As String = "Your_Secret_Key"
  16.             Dim Server As String = Convert.ToBase64String(Encryption("Your_Server_Name", secretKey))
  17.             Dim DataBase As String = Convert.ToBase64String(Encryption("Your_Database_Name", secretKey))
  18.             Dim UserID As String = Convert.ToBase64String(Encryption("Your_User_ID", secretKey))
  19.             Dim Password As String = Convert.ToBase64String(Encryption("Your_Password", secretKey))
  20.  
  21.             Dim sw As New StreamWriter(Application.StartupPath + "\" + "TextFile1.txt")
  22.             sw.WriteLine(DataSource)
  23.             sw.WriteLine(DataBase)
  24.             sw.WriteLine(UserID)
  25.             sw.WriteLine(Password)
  26.             sw.Close()
  27.         End Sub
  28.  
  29.         Private Function Encryption(PlainText As String, key As String) As Byte()
  30.             Dim des As TripleDES = CreateDES(key)
  31.             Dim ct As ICryptoTransform = des.CreateEncryptor()
  32.             Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
  33.             Return ct.TransformFinalBlock(input, 0, input.Length)
  34.         End Function
  35.  
  36.         Private Function CreateDES(key As String) As TripleDES
  37.             Dim md5 As MD5 = New MD5CryptoServiceProvider()
  38.             Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  39.             des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  40.             des.IV = New Byte(des.BlockSize / 8 - 1) {}
  41.             Return des
  42.         End Function
  43.     End Class
  44. 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.

http://bytes.com/topic/asp-net/answe...ng#post3748664
Apr 28 '13 #39
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,select 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 :
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports Microsoft.Win32
  4. Imports System.IO
  5. Imports System.Text
  6. Imports System.Security.Cryptography
  7. Imports Microsoft.VisualBasic
  8. Public Class employee
  9.     Private cons As String = ""
  10.   Private Function createdes(ByVal key As String) As TripleDES
  11.         Dim md5 As MD5 = New MD5CryptoServiceProvider()
  12.         Dim des As TripleDES = New TripleDESCryptoServiceProvider()
  13.         des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
  14.         des.IV = New Byte(des.BlockSize \ 8 - 1) {}
  15.         Return des
  16.     End Function
  17.      Public Function Readconnectionstring() As String
  18.         Try
  19.             Dim secretkey As String = "Employee"
  20.  
  21.             Dim sr As New StreamReader("C:\connection\tcon.txt")
  22.  
  23.             Dim Server As String = sr.ReadLine()
  24.             Dim database As String = sr.ReadLine()
  25.             Dim userid As String = sr.ReadLine()
  26.             Dim password As String = sr.ReadLine()
  27.  
  28.             Dim ds As String = Decryption(Server, secretkey)
  29.             Dim db As String = Decryption(database, secretkey)
  30.             Dim uid As String = Decryption(userid, secretkey)
  31.             Dim pass As String = Decryption(password, secretkey)
  32.  
  33.             cons = "Data Source =" & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
  34.         Catch ex As Exception
  35.             MsgBox(ex.Message)
  36.         End Try
  37.         Return cons
  38.     End Function
  39.  
  40.     Private Function Decryption(ByVal cyphertext As String, ByVal key As String) As String
  41.         Dim b As Byte() = Convert.FromBase64String(cyphertext)
  42.         Dim des As TripleDES = createdes(key)
  43.         Dim ct As ICryptoTransform = des.CreateDecryptor()
  44.         Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)
  45.         Return Encoding.Unicode.GetString(output)
  46.     End Function
  47.     Public Function emp() As DataTable
  48.  
  49.         Dim cmd As New SqlCommand
  50.         Dim da As New SqlDataAdapter
  51.         Dim dt As New DataTable
  52.         Dim con As New SqlConnection(Readconnectionstring())
  53.         Dim query As String = " select * from emp "
  54.         With cmd
  55.             .CommandType = CommandType.Text
  56.             .CommandText = query
  57.             .Connection = con
  58.         End With
  59.         da.SelectCommand = cmd
  60.         con.Open()-> show  error
  61.         da.Fill(dt)
  62.         con.Close()
  63.         Return dt
  64.     End Function
Thanks.
Apr 29 '13 #40
vijay6
158 100+
Hey mohammed saleh, you solved your problem or still having any error?
Apr 29 '13 #41
solved my problem .
Thank's Mr.vijay6
Apr 30 '13 #42

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kim | last post by:
Hello, I am selecting data from a text file with the following statement: "INSERT INTO SELECT * FROM " & sSource & " IN '' " & _ "'text;Database=" & sPath & ";FMT=Delimited;HDR=No' " & _...
5
by: Mike P | last post by:
How do I read the contents of a text file into a string in my program? Thanks, Mike *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get...
1
by: GrantS | last post by:
I am unable to get the connection to work with using the app.config file. the connection works when I use 'in line' connection as below:...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
1
by: Kelly | last post by:
I have a db connection that uses a userid/passwd that will remain static and the application user does not need to know/change it. However, by hardcoding it, the plain text is available both in...
0
by: christophe.leroquais | last post by:
Hello, i can't find the connection string (and the def of the options) to access a flat file from SQL Server 2005. I guess it is something like: Provider=Microsoft.Jet.OLEDB.4.0; Data...
4
by: Max Vit | last post by:
Here is my problem: I have an application built in Access that outputs sensitive data to a text file. I would like to encrypt this data *whilst* the file is being outputted. The encryption I was...
2
by: mfuribondo | last post by:
I have a solution with three projects in it. Two are windows forms applications and the third is a class library. I am trying to deploy the projects which use a single database located in one of...
2
kim6987
by: kim6987 | last post by:
Can any one show me how to write the code in C when I want to read the content of a text file and write it into a string? Thank you!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.