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 - 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())-> Error is here
-
-
cons = "Data Source = " & ds & "; DataBase =" & db & "; User Id =" & uid & "; Password =" & pass & ";"
-
Catch ex As FileNotFoundException
-
MsgBox(ex.Message)
-
End Try
-
Return cons
-
End Function
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. - 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. http://bytes.com/topic/asp-net/answe...ng#post3748664 41 4061
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.
This my problem " I don't know how encrypting it supplying a password." please give me the answer
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
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 .
Hey mohammed saleh, what you want? Secure connection string or secure text file content?
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 : - 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
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!!!
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
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?
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 ) - 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
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.
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
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.
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
I want to show my gridview
Hey mohammed saleh, what you want to show inside the gridview?
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
Mr vijay6 ,
I want show two column(id and name) for insert , update and delete
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.
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
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.) - 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 = "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
-
-
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
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. - Imports System
-
Imports System.Data.SqlClient
-
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 = "mohammed saleh"
-
-
Dim sr As New StreamReader(Application.StartupPath + "\" + "TextFile1.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 dServer As String = Decryption(Server, secretKey)
-
Dim dDataBase As String = Decryption(DataBase, secretKey)
-
Dim dUserID As String = Decryption(UserID, secretKey)
-
Dim dPassword As String = Decryption(Password, secretKey)
-
-
Dim con As New SqlConnection("Server=" + dServer + ";Database=" + dDataBase + ";User ID=" + dUserID + ";Password=" + dPassword + ";")
-
-
Try
-
con.Open()
-
MessageBox.Show("Connection Successful")
-
Catch ex As Exception
-
MessageBox.Show(ex.ToString())
-
Finally
-
con.Close()
-
End Try
-
End Sub
-
-
Private Function Decryption(CypherText As String, 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
-
-
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
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
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.
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 : -
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())->this get my connection
-
Dim query As String = " select * from emp "
-
With cmd
-
.CommandType = CommandType.Text
-
.CommandText = query
-
.Connection = con
-
End With
-
da.SelectCommand = cmd
-
con.Open()
-
da.Fill(dt)
-
con.Close()
-
Return dt
-
End Function
-
-
Public Sub update(ByVal id As Integer, ByVal name As String)
-
-
Dim cmd As New SqlCommand
-
Dim da As New SqlDataAdapter
-
Dim con As New SqlConnection(Readconnectionstring())->this get my connection
-
Dim query As String = "update emp set empname='" & name & "'" & _
-
" where empid = " & id
-
-
-
With cmd
-
.CommandType = CommandType.Text
-
.CommandText = query
-
.Connection = con-> get object connection
-
-
End With
-
da.SelectCommand = cmd
-
con.Open()
-
cmd.ExecuteNonQuery()
-
con.Close()
-
End Sub
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... - Public Partial Class WebForm1
-
Inherits System.Web.UI.Page
-
-
Private con As SqlConnection
-
-
Protected Sub Page_Load(sender As Object, e As EventArgs)
-
-
con = New SqlConnection(Readconnectionstring())
-
-
End Sub
-
End Class
mr vijay6
show error : Bad data in this line
Dim output As Byte() = it.TransformFinalBlock(b, 0, b.Length)
How can I resolve this error : Bad data
Bad data in this line
Dim output As Byte() = it.TransformFinalBlock(b, 0, b.Length)
Hey mohammed saleh, where you've this line?
I put for you my 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 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(Readconnectionstring())
-
Dim query As String = " select * from emp "
-
With cmd
-
.CommandType = CommandType.Text
-
.CommandText = query
-
.Connection = con
-
End With
-
da.SelectCommand = cmd
-
con.Open()->Conncection property hasn't been initialized
-
da.Fill(dt)
-
con.Close()
-
Return dt
-
End Function
Hey mohammed saleh, your text file 'C:\connection\tcon.txt' containing your connection string information with encryption or without encryption?
no my text file without encryption
how can I containning mytext file connection string with encryption ?
I want this please
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 : - 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?
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...
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
How can I encrypt and decrypt file coonection string without all time
please help me ,give me any thing
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. - 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. http://bytes.com/topic/asp-net/answe...ng#post3748664
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 : - 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.
Hey mohammed saleh, you solved your problem or still having any error?
solved my problem .
Thank's Mr.vijay6
Sign in to post your reply or Sign up for a free account.
Similar topics
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' " & _...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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!
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |