|
Yes, I tried it before. The exception goes away, but the result is not what I expect. As result I get a string with asian character, but i know that the result must be a comprehensible alphanumerical string.
|
|
|
|
|
The padding mode needs to be set on both sides of the encryption. The alternative is for you to pad your key with a different character. Why does it have to be #?
|
|
|
|
|
My answer to Dave K. in the last reply will responds to your question. Thank you so much.
|
|
|
|
|
You've got a ton of problems with this code.
First, The two lines where you have:
Encoding.Unicode.GetBytes(...);
are getting the bytes all right, but then completely ignoring the returned data and dropping it. These lines do absolutely nothing.
Next, the length of the IV in bytes must be the block size divided by 8, not the key size. This line:
int length = keysize / 0x10;
should be
int length = blockSize / 16;
Also, you're using the Unicode encoding to convert a string to bytes. This limits your key string to 16 caharacters (for a 256 bit block size) since Unicode will return 2 bytes for every character in your IV and key strings. You're cutting the maximum size of your source string in half by using it.
I'd also factor out the code that does the string trimming and conversion to bytes with the following. I think it's pretty obvious what it does and how you use it:
public static byte[] UnicodeConvertStringToVector(string source, int sizeInBits)
{
int maxStringLength = sizeInBits / (UnicodeEncoding.CharSize * 8);
if (source.Length > maxStringLength) {
source = source.Substring(0, maxStringLength);
} else if (source.Length < maxStringLength) {
source = source.PadRight(maxStringLength, "#");
}
return Encoding.Unicode.GetBytes(source);
}
And finally, from the point where you declare bytes and rgbIV , your code is just a freaking mess. It's no wonder it doesn't work correctly. I don't know what the hell you're doing in there, but it shouldn't be returning a string. It should be returning an array of bytes.
I'm probably shooting myself in the foot by posting this, but here's the "cleaned up" code for this part:
byte[] keyData = UnicodeConvertStringToVector(myKeyString, keySize);
byte[] ivData = UnicodeConvertStringToVector(myIV, length);
byte[] encryptedData = null;
using (MemoryStream outputStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream(outputStream, managed.CreateEncryptor(keyData, ivData), CryptoStreamMode.Write))
{
using (StreamWriter encryptStream = new StreamWriter(cryptStream))
{
encryptStream.Write(strInputString);
}
encryptedData = outputStream.ToArray;
}
}
return encryptedData;
Your code might make a bit more sense if you used meaningful variable names instead of the generic "stream1", "stream2" garbage. Also, nobody puts "str" on the front of their variable names anymore.
modified 1-Aug-12 11:24am.
|
|
|
|
|
Dear Dave,
please accept my apologies for not explaining before my situation.
That two functions are not mine. I'm developing an application that will be installed on a client that will communicates with an ASMX server. This application haft to do so much things, but one of these, is decrypting data received via SOAP from the ASMX server. Given that this server is not mine, its sys administrator sent me these two functions, in order to implement them in my software. So i can't control either the encrypt function (because the data are encrypted from the server, and it sends them already encrypted) or the server. The sysadmin said me that the data received via SOAP must be decrypted in this way, when i explained my problem to him, he just said "there are hundreds clients working with that function, so the problem is yours. Check your .NET Framework and VStudio installations".
This evening i will apply your suggestions on my code, and i'll check the result. In the meanwhile, thank you so much for your help.
|
|
|
|
|
If he gave you the decryption function and the correct keys, and it still doesn't work, the problem is entirely HIS, not yours. If the code he gave you doesn't work as he advertised, then HE needs to get you code that does work with his broken implementation.
This has nothing to do with the .NET or VS installations. This has everything to do with poorly written code.
|
|
|
|
|
Dave Kreskowiak wrote: If he gave you the decryption function and the correct keys, and it still doesn't work, the problem is entirely HIS, not yours.
Yes, I absolutely believe in your opinion. Unfortunately, he's working for the Government, he created the Web Service that i haft to query, and he makes the rules, so I can say him what you said me, but knowing his behavior, surely he will not accept my advices. In Italian Government there is not a Supervisor Authority which may act against bad IT technicians.
Seeing your culture, may I ask an information? I've not developed the C# soap client yet, so in order to test the most complex function of this client (the decrypt function), i'm requesting the data with a PHP SOAP Client. My PHP SOAP Client print the result of the SOAP request in a textarea, so i copy-and-paste my three values in three textboxes in a C# Windows Form Application (encrypted, key and IV), and print the result in another textbox.
It can be differences between receiving directly the data with a C# SOAP Client, and receiving them with a PHP SOAP Client? The clients (developed by others) that he compares to the mine, are done completely in C#. My client is able to decrypt only strings where their key doesn't need to be padded with "#", their clients decrypt indistinctly all the encrypted strings...
|
|
|
|
|
The SOAP wrapper shouldn't matter with this function, based on what I've seen.
|
|
|
|
|
Really, he shouldn't be distributing source code for this to you to use in your own app. He should be giving you a library .DLL that you can use that handles this stuff.
Seriously, how much are you paying this guy and for what?? This is piss-poor quality code you've been given.
|
|
|
|
|
Dave Kreskowiak wrote: He should be giving you a library .DLL that you can use that handles this stuff.
He did it. He sent me a .DLL containing the Decrypt method:
public string Decrypt(strInputString, strKeyString, myIV);
but when i call that function, the program writes a log file showing "Cryptographic Exception: Padding is invalid and cannot be removed". After that, i decompiled the .DLL and asked to the sysadmin if the function i found in the .DLL (what i posted here) was correct, and he said it was. Running the function from the sourcecode, and running it from .DLL added by reference, shows the same error.
Unfortunately, i tried your suggestions, and they doesn't work. I've changed the length assignment, from keySize/0x10 to blockSize/16. I've changed the function which I use to retrieve bytes of Key and IV, using your UnicodeConvertStringToVector(). I had to fix it, because Visual Studio considers characters in "" like a string and '' like a char, so i substituted them. But it shows always the same exception at the same point:
stream2.WriteByte((byte)num5);
in the last while{}.
I'm burning my brains on this from more than 2 weeks. Unfortunately I'm a PHP/C/C++ Guru, and i know C# & RijndaelManaged only a bit.
In the last try i noticed one thing. When debugging, managed.Key and managed.IV don't contain what bytes and rgbIV do. I was thinking that the algorithm doesn't assign correctly the values in the RijndaelManaged object, but if it is true, why can i decrypt all the strings where the key does not need to be padded ?
Thank you so much for your help.
|
|
|
|
|
If he sent you a .DLL and it's not working, then the problem is either in what you're doing to test this with your SOAP client app or it's with the code he's using to "encrypt" the data and "decrypt" it. Frankly, what he's doing in that code is ... suspicious.
|
|
|
|
|
I am going to find a .NET extension library in my .NET application development.
Do you have any recommendation for high quality .NET extension library? free or commercial both are okay.
diligent hands rule....
|
|
|
|
|
What kind of functionality are you looking for?
[Edit]
Programmer's Paradise[^] lists almost 300 libraries of various kinds. A popular source of free (source code included) libraries is CodePlex[^].
[/Edit]
/ravi
|
|
|
|
|
cover base classes in .NET framework etc.. thanks
diligent hands rule....
|
|
|
|
|
That's still a pretty wide area. The BCL encompasses file I/O, graphics, database database interactions, XML document manipulation and much more. Can you not narrow down your requirements?
/ravi
|
|
|
|
|
As Ravi has stated, please narrow down what your requirements are. What is wrong with the base classes that are already in the .NET Framework?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
You really don't have any idea what you want, do you?
You've been asked multiple times and you still can't narrow this down to anything other than "something"?
|
|
|
|
|
|
it really depends what you need to be done by the library.
I am currently in the process of creating such a library - the code is open source: https://github.com/FlorianRappl/WinFormX. The library can be downloaded by using NuGet.
This one will be specialized on extending Windows Forms and GDI+. Currently on animations and further drawing abilities are implemented, but more is to come (when I have time).
|
|
|
|
|
Your work on this looks very interesting, Florian ! I've downloaded your latest build, and look forward to examining it.
May I suggest you post about this on the "Windows Forms" forum here, if you have not done so, already.
best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr
|
|
|
|
|
fyi: your latest build loads with no problems in Visual Studio 12 RC.
The transparency window demo creates a dark red-purple window for whatever reason. Win 7 64 bit.
All you need to do is set the 'Demo project to be the Start-Up.
best, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr
|
|
|
|
|
Yeah, thanks. The transparency uses the method provided by the .NET-Framework, therefore the error must lie there. I experienced no problem there (also Win7 64bit), so it is probably theme related.
The selected demo just shows the animate (extension) method, which can be applied on any control.
The next steps in the project include:
- Include a new kind of control XControl<T> where T is of type Control , which will enable more display possibilities (like percent units for width / height and x / y)
- Change some of the GDI+ extension methods to use Marshal.Copy(ptr, values, 0, bytes) . In the first code I tried to do all those methods without any unsafe code, but I did not know about the possibility of Marshal.Copy() , which gives us must of those (unsafe) possibilities without using any unsafe code
- Include a real cool Metro like form (or to be more precise: Zune / Github client like form) for Windows Forms
- Extend the Animation method even further
So any help (or suggestions) would be highly appreciated!
|
|
|
|
|
Hi everybody,
i am trying to insert the values into a Laptop table which has a primary key as autonumber . I am using Access 2007 as my database and VB.NET as my front end . This is the code i have written
Private Sub addLaptopForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Documents and Settings\Jain\My Documents\jainProto3m.mdb"
Me.TblLaptopTableAdapter1.Fill(Me.JainProto3mDataSet1.tblLaptop)
Dim cmd As New OleDb.OleDbCommand
Dim dr As OleDbDataAdapter
Dim ds As DataSet
Try
con.Open()
Try
cmd.Connection = con
Dim qrymax As String
qrymax = "SELECT MAX(pkLaptopID) as abc FROM tblLaptop"
cmd.CommandText = qrymax
ds = New DataSet()
dr = New OleDbDataAdapter(cmd)
dr.Fill(ds)
Dim i As Integer
Dim g As String
g = ds.Tables(0).Rows(0).Item("abc").ToString()
i = Convert.ToInt32(g) + 1
Me.txtAddLaptopLaptopID.Text = i.ToString()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Exit Sub
Finally
con.Close()
End Try
End Sub
i am getting the following error
CODE
no value given for one or more required parameter.
I'd really appreciate any suggestions thank you.
I new to VB.NET , I am VB.NET 2008,
Regards
Jain
|
|
|
|
|
Drop the reader, drop the dataset; you'll only need a connection and a command, if you need to retrieve a single value from the database. Use the ExecuteScalar -method.
There's an example in the documentation[^].
--edit
Edited an incomplete sentence, nothing special
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I've just lost a day and a half of my working life trying to track down a bug which was causing word documents i am changing to include a mail merge source to error when opened in word 2007. The documents work fine in word 2010. After an awful lot of digging, I've tracked the issue down to the URI for the mdb file that is the data source for the merge. My non working in 2007 code was as follows:
using (var docx = WordProcessingDocument.Open(filePath, true))
{
FileInfo mdbFile = FileHelper.GetResourceFile("Merge.mdb");
Uri mdbUri = new Uri(mdbFile.FullName, UriKind.Absolute);
var settings = DocHelper.GetOrCreateSettingsPart(document);
settings.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/mailMergeSource", mdbUri, id1);
}
After an awful lot of checking i found that the relationship file was recording the uri without a leading "file:///", some more looking into this it appears that the openXML SDK is writing out the OrigionalString. The change i had to make to my code for it to work was as follows:
Uri mdbUri = new Uri("file:///" + mdbFile.FullName, UriKind.Absolute);
I guess my question is, was i doing something wrong when working with the Uri or is this something that the SDK is not getting right. Additionally i really hope it is not a bug that was found, and fixed in Office 2010 by working with the Uri missing the "file:///" prefix insted of changing the SDK to write out Uri.ToString();
|
|
|
|
|