|
JRHibner wrote: I want to be able to check and make sure the Name isn't already used in the
datatable
In that case it sounds like you want to check whether a row is returned from the Select method?
If tRow.Count > 0 Then
Else
End If
I don't speak Idiot - please talk slowly and clearly
"I have sexdaily. I mean dyslexia. Fcuk!"
Driven to the arms of Heineken by the wife
|
|
|
|
|
So simple, so elegant. I'm completely overthinking that one. Thanks again!
|
|
|
|
|
Is it possible to change an external assembly's property using reflection; specifically the GUID? The reason I'm trying to do this is for when a customer logs into my website and purchases a plug-in for the application. The server would then change the GUID within the plug-in's DLL and then allow the custom to download it. When they then go to install the plug-in, the application check the assembly's GUID to see if it matches what it's supposed to. This is part of the security system I have designed (within my head at the moment) to ensure that the assembly is allowed to be used, and can only be used by that specific person. So is there anyway to change the GUID of an assembly through reflection?
Thanks
|
|
|
|
|
You can't make modifications using reflection; it's a read-only game. There are multiple ways to generate a new assembly; using the CompilerService[^] would be an option, but "modifying" an existing assembly would be easier using Mono's Cecil.
|
|
|
|
|
So if I understand correctly, based on my quick read of the link you provided, with the Mono's Cecil I could open up an assembly that I created, modify the GUID assembly attribute, and then save it to a new file and server it to the customer for download?
|
|
|
|
|
Yes
|
|
|
|
|
I am looking for an example of how to call MessageBeep in Visual Basic 8/net?
|
|
|
|
|
You can start with this[^].
And then move on to these[^].
Honestly, this was not very hard to find. You just Google for "MessageBeep VB.NET" and you come up with all kinds of examples.
|
|
|
|
|
Hi,
I am working on program to be used to process confidential data. That data must be well-encrypted. I have built method, that exports data from memory to XML, encrypts XMl document and saves it to file. But, what seems strange to me is, that encryption method doesn’t work. After its execution, file, which may contain encrypted data is created, but remains empty. I don’ t see any bug in the code. Could you help me to get it working, please?
Here is code used to encrypt and save XML.
Public Sub Save(ByVal carrier As DataCarrier, ByVal password As String, ByVal filename As String)
Try
Using sha As New SHA512Managed
Dim salt As Byte() = sha.ComputeHash(Encoding.Default.GetBytes(password))
Using pdb As New Rfc2898DeriveBytes(password, salt), aes As New RijndaelManaged, filestr As New FileStream(filename, FileMode.Create, FileAccess.Write)
Using cryptostr As New CryptoStream(filestr, aes.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write)
ExportData(carrier).Save(cryptostr)
End Using
End Using
End Using
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Also, is that encryption algorithm OK, or there is some way to break it? I must be sure that there is no way to break encryption, that is used.
Stanislav Husár
|
|
|
|
|
Have you tried putting a breakpoint to see if an Exception is thrown?
After a brief glance, the code itself looks fine AFAICT, though the problem could be in the ExportData method, is there a reason you aren't calling the cryptostream's Write directly?
My knowledge about this is four years out of date, but Rijndael was considered secure. I vaguely recall reading they'd found a way to crack it in theory, but applying the crack it in practise hasn't happened yet and isn't likely to be feasible soon.
|
|
|
|
|
It does not throw any exception.
ExportData method only creates XML document with data to be encrypted. I have checked, that result of ExportData contains, what it should cotain.
I have also tried following, again, empty file is generated.
Public Sub Save(ByVal carrier As DataCarrier, ByVal password As String, ByVal filename As String)
Try
Using sha As New SHA512Managed
Dim salt As Byte() = sha.ComputeHash(Encoding.Default.GetBytes(password))
Using pdb As New Rfc2898DeriveBytes(password, salt), aes As New RijndaelManaged, filestr As New FileStream(filename, FileMode.Create, FileAccess.Write)
Using cryptostr As New CryptoStream(filestr, aes.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write)
Dim str = ExportData(carrier).ToString()
Using w As New StreamWriter(cryptostr)
w.Write(str)
End Using
End Using
End Using
End Using
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Moreover, I have tried simply writing string literal to CryptoStream , this approach generates empty file too.
Strange is, that when I run this version:
Public Sub Save(ByVal carrier As DataCarrier, ByVal password As String, ByVal filename As String)
Try
Using sha As New SHA512Managed
Dim salt As Byte() = sha.ComputeHash(Encoding.Default.GetBytes(password))
Using pdb As New Rfc2898DeriveBytes(password, salt), aes As New RijndaelManaged, filestr As New MemoryStream()
Using cryptostr As New CryptoStream(filestr, aes.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write)
ExportData(carrier).Save(cryptostr)
Dim str = New String(Encoding.Default.GetChars(filestr.ToArray()))
Debugger.Break()
End Using
End Using
End Using
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Then at Debugger.Break statement str variable cotains data, which should be in the file.
While this and simmilar solutions generate empty file.
Public Sub Save(ByVal carrier As DataCarrier, ByVal password As String, ByVal filename As String)
Try
Using sha As New SHA512Managed
Dim salt As Byte() = sha.ComputeHash(Encoding.Default.GetBytes(password))
Using pdb As New Rfc2898DeriveBytes(password, salt), aes As New RijndaelManaged, filestr As New MemoryStream()
Using cryptostr As New CryptoStream(filestr, aes.CreateEncryptor(pdb.GetBytes(32), pdb.GetBytes(16)), CryptoStreamMode.Write)
ExportData(carrier).Save(cryptostr)
File.WriteAllBytes(filename, filestr.ToArray())
Debugger.Break()
End Using
End Using
End Using
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
I am really confused on what it does.
|
|
|
|
|
I'm not surprised at your confusion. The only thing I can suggest is to simplify as far as possible and re-factor back to what you want.
- Comment out the export line and perform the crypto-stream write directly.
- Create two byte arrays for the key and IV and make sure
pdb is creating a sensible key & IV, I would have thought that the if these were incorrect the encryption algorithm would throw. I've not used the derived bytes thing, so it is new to me - If the above isn't working, hard-code the key and IV byte arrays
If all else fails, you could go back to square one, and write a console app that uses hard-coded byte arrays.
|
|
|
|
|
I think I may have figured it out!
The cryptostream writes to a filestream. The XML can't write to the same stream, and if you use a different stream you'll either overwrite (this is probably your situation) or it'll append the XML after the encrypted information. The other option is the XML is written first, then appended or overwitten with the encrypted information, neith seems to apply in your case.
In your position I'd replace the file stream with a memory stream for the cryptography stuff. I'd perform the write immediately, reset the position to 0 and pass the memory stream into the Export method. Then all the XML writer has to do is to read the Memory stream into the file stream when you've reached the correct point in the file (or set a property to its contents).
The other problem you might face is that you need to encode the encrypted bytes (e.g. into Hex characters), otherwise you'll get non-printing characters in the XML document, fouling it up.
|
|
|
|
|
Hmm, now it works, even with using the same code as the one that didn´t work.I di d nothing more, than copying binaries from bin/Debug/ and running them on another computer. I can´t understand this behaviour.
|
|
|
|
|
I think that is properly called a heisenbug[^].
That is odd behaviour. Something to do with a 64/32 bit compilation on a 32/64 bit machine (I mean really unsure)! The only other things that spring to mind immediately is the framework version or that restarting your machine might be a good idea, in case VS has screwed up. Hope you get it stable.
|
|
|
|
|
Maybe it is stable now. But there is new problem. I have tried decrypting encrypted file, but it fails to decrypt, decrypted data absolutely do not match data which was encrypted. I am using this code:
Public Sub Save(ByVal carrier As DataCarrier, ByVal password As String, ByVal filename As String)
Try
Using sha As New SHA512Managed
Dim hash = sha.ComputeHash(Encoding.Default.GetBytes(password))
Dim key As Byte() = New Byte(31) {}
Dim iv As Byte() = New Byte(15) {}
Array.Copy(hash, 0, key, 0, 32)
Array.Copy(hash, 32, iv, 0, 16)
Using aes As New RijndaelManaged, filestr As New FileStream(filename, FileMode.Create, FileAccess.Write)
Using cryptostr As New CryptoStream(filestr, aes.CreateEncryptor(key, iv), CryptoStreamMode.Write)
ExportData(carrier).Save(cryptostr)
End Using
End Using
End Using
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function Load(ByVal password As String, ByVal filename As String) As XDocument
Dim sha As SHA512Managed
Dim aes As RijndaelManaged
Dim trans As ICryptoTransform
Dim cryptostr As CryptoStream
Dim filestr As FileStream
Try
sha = New SHA512Managed()
Dim hash = sha.ComputeHash(Encoding.Default.GetBytes(password))
aes = New RijndaelManaged()
Dim key As Byte() = New Byte(31) {}
Dim iv As Byte() = New Byte(15) {}
Array.Copy(hash, 0, key, 0, 32)
Array.Copy(hash, 32, iv, 0, 16)
trans = aes.CreateEncryptor(key, iv)
filestr = New FileStream(filename, FileMode.Open)
cryptostr = New CryptoStream(filestr, trans, CryptoStreamMode.Read)
Return XDocument.Load(cryptostr)
Catch ex As Exception
Logging.Instance.WriteException(ex, TraceEventType.Error)
MessageBox.Show(String.Format(My.Resources.UnhandledExceptionExString, ex.Message, ex.ToString), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If cryptostr IsNot Nothing Then cryptostr.Dispose()
If filestr IsNot Nothing Then filestr.Dispose()
If trans IsNot Nothing Then trans.Dispose()
If aes IsNot Nothing Then aes.Dispose()
If sha IsNot Nothing Then sha.Dispose()
End Try
End Function
modified 17-Nov-12 11:23am.
|
|
|
|
|
Can you post the code for the Export.Save method so we can see how you are using the cryptostream?
|
|
|
|
|
Do you mean ExportData(carrier).Save(cryptostr) ? That is System.Xml.Linq.Save method.
|
|
|
|
|
I've never tried encrypting something exactly as you are trying, but I believe the problem is that you're not calling the write method of the crypto stream. Check out the information on the write method of the crypto stream for for information. Below is a function I use to encrypt a string. As you can see I call the write method of the crypto stream and pass in a byte array of the data I want to encrypt. Hopefully the code below helps you.
Public Shared Function Encrypt(value As String, key() As Byte, iv() As Byte) As string
Dim bytValue() As Byte
Dim bytEncoded() As Byte
Dim rijndael As New Rijndael Managed
Dim ms As New MemoryStream
bytValue = Encoding.ASCII.GetBytes(value.ToCharArray)
Using CryptoStream As New CryptoStream(ms, rijnadel.CreateEncryptor(key, iv), CryptoStreamMode.Write)
CryptoStream.Write(bytValue, 0, bytValue.Length)
CryptoStream.FlushFinalBlock()
bytEncoded = ms.ToArray
ms.Close()
End Using
Return Convert.ToBase64String(bytEncoded)
End Function
|
|
|
|
|
Why do you think, I do not call Write ? I call System.Xml.Linq.XDocument.Save.
It looks, that it is really a Heisenbug.
|
|
|
|
|
Hi,
My VB6 application has been developed long back using Access as back end. Now I am planning to migrate data from Access to sQL server 2008 .
1)Previously vb6 application was retrieving date from Access file in dd/MM/yyyy format which is the system format without any format function.(Uses RecordSet object) . After migrating to sQL server 2008 date retrieved in yyyy-MM-dd format which I have to format each time to my required format.
So it's creating issues at many places like inserting records to server, date comparison with date controls of vb6 etc. So I may have to apply format() function at many places. Can we use any once only setup to retrieve date in required format , so the application changes will be minimal. (at SQL Server side or at my application). ie the RecordSet.Open will always provide date data in dd/MM/yyyy format without any explicit formatting.
2)I am not able to use seek function with RecordSet object
Thanks in Advance
Johnson
|
|
|
|
|
If you're worried about "formats" of DateTime values in the Access database, you've been storing datetime data as strings, not datetime values.
You shouldn't be worried about those "formats" at all since a true datetime value is has no format in the database.
The only time you should be worried about the format of a datetime is when such values are presented in the UI or the user has to otherwise interact with them.
|
|
|
|
|
Hi all,
We have a financial system that writes a text file out that could be several 100 or even 1000 lines long. It writes the file into a txt file and can't be modified to be another file extension and then be renamed after the data has been loaded.
I have a service already written that notices that a txt file has shown up in this directory and fires off the application that needs to read this data.
I don't want to have the application which reads the data begin the process until after the financial system has written all data to the file.
I've searched all kinds of VB help sites and here is the code most refer to in various ways:
<br />
Public Function IsFileLocked(filename As String) As Boolean<br />
Dim Locked As Boolean = False<br />
Try<br />
Dim fs As FileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)<br />
fs.Close()<br />
Catch ex As IOException<br />
Locked = True<br />
End Try<br />
Return Locked<br />
End Function<br />
I have my file open in Notepad to test and have the file being accessed, but it falls straight through the code and does not hit the catch. I even opened the file in Excel and had the same result.
Any ideas?
Lost in the vast sea of .NET
|
|
|
|
|
This method will only work if the file being written by the financial system opens the file for exclusive write, meaning no shared readers or writers. Apparently, the financial system is allowing other processes to write to the file and the same time it's writing it.
The only other way I know of to determine if the file is completely written is to enumerate the system handle table, determine if the file is open currently open and:
1) Keep checking this handle every few seconds and wait for it to disappear. This should signal the end of the file write.
2) Depending on how the file is being written, you may find that the file is being opened, written, then closed, repeatedly. You may have to keep checking and waiting for an arbitrary amount of time to see if the file is again opened before considering the file complete.
|
|
|
|
|
I goofed... I was using notepad over and over to try and replicate the file being accessed. I found that notepad doesn't cause the file to show as open. When I tried Excel, right before doing my post, I goofed and opened my output file instead of my input file.
I still need to do some testing when the financial system is writing a file to make sure it works the same way or if I need to take your suggestion and look at the file handle.
Thanks!
Lost in the vast sea of .NET
|
|
|
|
|