473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Encrypt My.Settings setting?

Tom
Is it possible to encrypt a value in the my.settings area in VB.NET
2005? I.E. Can I add a settings value (via My Project / Settings) and
have it encrypt that value so that if anyone looks at the resulting
app.config file the value is encrypted? If so, (1) How do you specify
the value to be encrypted? And (2) How do you access it now from VB?
Can you still go through My.Settings??

Tom

--

Jul 17 '06 #1
4 10825
well theres no built in function to say, for example

Dim str as string = "password"

my.Settings.Pas sword1 = str
my.settings.Pas sword1.Encrypt( )

theres nothing built in to do that. you have to encrypt the string yourself,
be it by your own algorithm or by the Crytography namaespace (ive never used
it, so if you use it let me know how it goes). the easiest way ive found,
unless you need things heavily encrypted, is to just create a simple letter
bump. anyone looking at it wont make sense of it unless theyre seriously
trying to hack it.
--
-iwdu15
Jul 17 '06 #2
Tom
iwdu15: Well, one of the things I thought that was 'trumpted' as new
and great with VS 2005 was the ability to encrypt values in your
settings file, especially connection strings. I see that in ASP.NET,
things can be encrypted in the web.config file, but I don't see
anything about this for Windows Forms applications.
--

iwdu15 wrote:
>well theres no built in function to say, for example

Dim str as string = "password"

my.Settings.Pa ssword1 = str
my.settings.Pa ssword1.Encrypt ()

theres nothing built in to do that. you have to encrypt the string
yourself, be it by your own algorithm or by the Crytography
namaespace (ive never used it, so if you use it let me know how it
goes). the easiest way ive found, unless you need things heavily
encrypted, is to just create a simple letter bump. anyone looking at
it wont make sense of it unless theyre seriously trying to hack it.
Jul 17 '06 #3
You can use this code to encrypt and decrypt a stored value.

Imports System.Security .Cryptography
Imports System.Text

Module mod_Globals

Public EncryptionKey As String =
"justsomewordst obeusedasacrypt ionkey"

Public Function EncryptString12 8Bit(ByVal vstrTextToBeEnc rypted As
String, ByVal vstrEncryptionK ey As String) As String

Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New MemoryStream
Dim objCryptoStream As CryptoStream
Dim objRijndaelMana ged As RijndaelManaged

vstrTextToBeEnc rypted =
StripNullCharac ters(vstrTextTo BeEncrypted)

bytValue =
Encoding.ASCII. GetBytes(vstrTe xtToBeEncrypted .ToCharArray)

intLength = Len(vstrEncrypt ionKey)

If intLength >= 32 Then
vstrEncryptionK ey = Strings.Left(vs trEncryptionKey , 32)
Else
intLength = Len(vstrEncrypt ionKey)
intRemaining = 32 - intLength
vstrEncryptionK ey = vstrEncryptionK ey &
Strings.StrDup( intRemaining, "X")
End If

bytKey = Encoding.ASCII. GetBytes(vstrEn cryptionKey.ToC harArray)

objRijndaelMana ged = New RijndaelManaged

Try
objCryptoStream = New CryptoStream(ob jMemoryStream,
objRijndaelMana ged.CreateEncry ptor(bytKey, bytIV),
CryptoStreamMod e.Write)
objCryptoStream .Write(bytValue , 0, bytValue.Length )
objCryptoStream .FlushFinalBloc k()
bytEncoded = objMemoryStream .ToArray
objMemoryStream .Close()
objCryptoStream .Close()
Catch

End Try

Return Convert.ToBase6 4String(bytEnco ded)

End Function

Public Function DecryptString12 8Bit(ByVal vstrStringToBeD ecrypted
As String, ByVal vstrDecryptionK ey As String) As String

Dim bytDataToBeDecr ypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255,
91, 45, 78, 14, 211, 22, 62}
Dim objRijndaelMana ged As New RijndaelManaged
Dim objMemoryStream As MemoryStream
Dim objCryptoStream As CryptoStream
Dim bytDecryptionKe y() As Byte
Dim intLength As Integer
Dim intRemaining As Integer
Dim intCtr As Integer
Dim strReturnString As String = String.Empty
Dim achrCharacterAr ray() As Char
Dim intIndex As Integer

bytDataToBeDecr ypted =
Convert.FromBas e64String(vstrS tringToBeDecryp ted)

intLength = Len(vstrDecrypt ionKey)

If intLength >= 32 Then
vstrDecryptionK ey = Strings.Left(vs trDecryptionKey , 32)
Else
intLength = Len(vstrDecrypt ionKey)
intRemaining = 32 - intLength
vstrDecryptionK ey = vstrDecryptionK ey &
Strings.StrDup( intRemaining, "X")
End If

bytDecryptionKe y =
Encoding.ASCII. GetBytes(vstrDe cryptionKey.ToC harArray)

ReDim bytTemp(bytData ToBeDecrypted.L ength)

objMemoryStream = New MemoryStream(by tDataToBeDecryp ted)

Try

objCryptoStream = New CryptoStream(ob jMemoryStream,
objRijndaelMana ged.CreateDecry ptor(bytDecrypt ionKey, bytIV),
CryptoStreamMod e.Read)
objCryptoStream .Read(bytTemp, 0, bytTemp.Length)
objCryptoStream .FlushFinalBloc k()
objMemoryStream .Close()
objCryptoStream .Close()

Catch

End Try

Return StripNullCharac ters(Encoding.A SCII.GetString( bytTemp))

End Function
Public Function StripNullCharac ters(ByVal vstrStringWithN ulls As
String) As String

Dim intPosition As Integer
Dim strStringWithOu tNulls As String

intPosition = 1
strStringWithOu tNulls = vstrStringWithN ulls

Do While intPosition 0
intPosition = InStr(intPositi on, vstrStringWithN ulls,
vbNullChar)

If intPosition 0 Then
strStringWithOu tNulls = Left$(strString WithOutNulls,
intPosition - 1) & _
Right$(strStrin gWithOutNulls,
Len(strStringWi thOutNulls) - intPosition)
End If

If intPosition strStringWithOu tNulls.Length Then
Exit Do
End If
Loop

Return strStringWithOu tNulls

End Function
End Module
Then to call this code do the following:

'Get Password
Dim strPassword as string = DecryptString12 8Bit(My.Setting s.Password,
EncryptionKey)

'Save Password
My.Settings.Pas sword = EncryptString12 8Bit(txt_Passwo rd1.Text.Trim,
EncryptionKey)

Hope this helps!

I didn't write this and I can't remember who did, otherwise I would
reference them.

Israel

Tom wrote:
Is it possible to encrypt a value in the my.settings area in VB.NET
2005? I.E. Can I add a settings value (via My Project / Settings) and
have it encrypt that value so that if anyone looks at the resulting
app.config file the value is encrypted? If so, (1) How do you specify
the value to be encrypted? And (2) How do you access it now from VB?
Can you still go through My.Settings??

Tom

--
Jul 17 '06 #4
Sy
I found this Class somehwere... can't remember where now... just include it
in your project.

Then somewhere in your main code just do something like the following...

dim EncClass as new Encryption
dim txtPlainTextPas sword as string = "ThisIsMyNewPas swordSoThere!"
dim txtEncryptedPas sword as string =
EncClass.Encryp tData(txtPlainT extPassword)
dim txtDecryptedPas sword as string =
EncClass.Decryp tData(txtEncryp tedPassword)

debug.print(txt PlainTextPasswo rd)
debug.print(txt EncryptedPasswo rd)
debug.print(txt DecryptedPasswo rd)

Cheers, Sy

PS. Here's the Encryption Class... As I said I liked to give credit where I
found this...

Imports System.Security .Cryptography

Public NotInheritable Class Encryption

Private TripleDes As New TripleDESCrypto ServiceProvider

Private svKey As String = "justsomewordst obeusedasacrypt ionkey"

Sub New(ByVal key As String)

' Initialize the crypto provider.

TripleDes.Key = TruncateHash(ke y, TripleDes.KeySi ze \ 8)

TripleDes.IV = TruncateHash("" , TripleDes.Block Size \ 8)

End Sub

Sub New()

TripleDes.Key = TruncateHash(sv Key, TripleDes.KeySi ze \ 8)

TripleDes.IV = TruncateHash("" , TripleDes.Block Size \ 8)

End Sub

Private Function TruncateHash( _

ByVal key As String, _

ByVal length As Integer) _

As Byte()

Dim sha1 As New SHA1CryptoServi ceProvider

' Hash the key.

Dim keyBytes() As Byte = _

System.Text.Enc oding.Unicode.G etBytes(key)

Dim hash() As Byte = sha1.ComputeHas h(keyBytes)

' Truncate or pad the hash.

ReDim Preserve hash(length - 1)

Return hash

End Function

Public Function EncryptData( _

ByVal plaintext As String) _

As String

' Convert the plaintext string to a byte array.

Dim plaintextBytes( ) As Byte = _

System.Text.Enc oding.Unicode.G etBytes(plainte xt)

' Create the stream.

Dim ms As New System.IO.Memor yStream

' Create the encoder to write to the stream.

Dim encStream As New CryptoStream(ms , _

TripleDes.Creat eEncryptor(), _

System.Security .Cryptography.C ryptoStreamMode .Write)

' Use the crypto stream to write the byte array to the stream.

encStream.Write (plaintextBytes , 0, plaintextBytes. Length)

encStream.Flush FinalBlock()

' Convert the encrypted stream to a printable string.

Return Convert.ToBase6 4String(ms.ToAr ray)

End Function

Public Function DecryptData( _

ByVal encryptedtext As String) _

As String

' Convert the encrypted text string to a byte array.

Dim encryptedBytes( ) As Byte = Convert.FromBas e64String(encry ptedtext)

' Create the stream.

Dim ms As New System.IO.Memor yStream

' Create the decoder to write to the stream.

Dim decStream As New CryptoStream(ms , _

TripleDes.Creat eDecryptor(), _

System.Security .Cryptography.C ryptoStreamMode .Write)

' Use the crypto stream to write the byte array to the stream.

decStream.Write (encryptedBytes , 0, encryptedBytes. Length)

decStream.Flush FinalBlock()

' Convert the plaintext stream to a string.

Return System.Text.Enc oding.Unicode.G etString(ms.ToA rray)

End Function

End Class

"Tom" <to*@nospam.com wrote in message
news:eF******** ******@TK2MSFTN GP04.phx.gbl...
Is it possible to encrypt a value in the my.settings area in VB.NET
2005? I.E. Can I add a settings value (via My Project / Settings) and
have it encrypt that value so that if anyone looks at the resulting
app.config file the value is encrypted? If so, (1) How do you specify
the value to be encrypted? And (2) How do you access it now from VB?
Can you still go through My.Settings??

Tom

--

Jul 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2037
by: Kevin Hodgson | last post by:
I store a users SMTP UserName and Password in a settings class, which is serialized to an XML Settings File. I need to encrypt the password string in some manner. Does anyone have any ideas how to do that and still store it in the XML File? I was thinking of putting the Encryption/Decryption code in the Get/Set methods of the SMTPPassword Property. Sample Code:
4
6358
by: tomtown.net | last post by:
Hello I'm using the File.Encrypt method (.net 2.0) to encrypt files and folders (mark for encryption) on a local disk (XP SP2, 3DES, .net 2.0). Unfortunatelly I get an exception when trying to mark the %desktop% folder for encryption: "The process cannot access the file because it is being used by another process). Is there any possibility I can close handles that use the folder? Thanks a lot in advance
5
8373
by: Andy | last post by:
I need to write a VB.NET WinForms app. For this purpose I need to keep some settings of the application, like many other Windows applications do. The most obvious solution is to store settings in the XML file or registry. If it's an XML file, then I need the following: 1. Create XSD schema, and be able to validate XML file with the settings against that schema (How would I store the schema, if it's a class library, for example? Would...
0
2834
by: =?Utf-8?B?UmljayBHbG9z?= | last post by:
For some unknown reason (user error?), I cannot get a NameValueCollection to persist in the app.config file. Unlike other settings, I cannot get the String Collection Editor GUI to allow my to add/edit any values for a setting with type NameValueCollection. Nor can I get a NameValueCollection to persist to the User Settings via code using a simple C# Console App... Is this a user error or ?
6
2797
by: WT | last post by:
Hello, Using VS2005. I have an assembly library that can be called from a Web site asp.net application or from a winform application. From this library I need to retrieve a path using simply a key like 'libPath'. As far as winform and asp.net share the same common base class for settings, SettingsBase, how to manage this ? For winform the value should be set in app.config and for web site in
5
8606
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The plan is to delivere different report.dlls with the main app. But it is essentially importent, that the reports and the app use the same settings.
2
1946
by: Robert Dufour | last post by:
Its easyb to change user leve settings but how do you change application level settings in code. Apparently it seems it can't be done. Duhhh. Anybody manage a workaround? Thanks for any help. Bob
6
11955
by: Aneesh P | last post by:
Hi All, I need to encrypt some fields esp password key values in configuration file while installting the application using .Net installer project and decrypt those values from my solution(windows service). Is there any built in method in.Net that I can use. The flow would be like this: Accept username/password from .Net installer dialog V
2
10054
by: =?Utf-8?B?QWFyb24=?= | last post by:
I am trying to create dynamic settings in a .NET 2.0 C# application. I need to be able to store settings on the user, but I do not know how many settings are necessary at design time because the settings are determined by a business object that is loaded into the app of which there could be one or many. I would prefer to use the same LocalFileSettingsProvider that the settings default to so that I do not have to manage it myself. Here...
0
8428
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.