|
Hi all, i have been using a Rijndael algorithm to encrypt and decrypt a sequence of bytes, but when i run the code i get the following exeption "Padding is invalid and cannot be removed.", this exeption belongs to decrypt function at line <code>int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
</code>
i writ my code here:
<pre>public static byte[] Encrypt(byte[] clearData)
{
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
Rijndael rijKey = Rijndael.Create();
rijKey.Mode = CipherMode.;
rijKey.Padding = PaddingMode.PKCS7;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,rijKey.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.FlushFinalBlock();
byte[] cipherTextBytes = ms.ToArray();
ms.Close();
cs.Close();
return cipherTextBytes;
}
public static byte[] Decrypt(byte[] encryptedData)
{
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
Rijndael rijKey = Rijndael.Create();
rijKey.Mode = CipherMode.ECB;
rijKey.Padding = PaddingMode.PKCS7;
MemoryStream memoryStream = new MemoryStream(encryptedData);
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijKey.CreateDecryptor(),
CryptoStreamMode.Read);
byte[] pTextBytes = new byte[encryptedData.Length];
int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return pTextBytes;
}</pre>
thanks.....................
|
|
|
|
|
Format the code.
Remove the duplicate post before some replies to it. I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Your code is unformatted, you need to ensure that the > & <g; appear, rather than > etc.
The problem you are getting is due to extra characters being read from the stream. The length of encrypyted string will always be a multiple of a set length (which itself is a power of two). Our encryption rountine always produces blocks of length 16, So the string "1" as does "12345678901234" would produce an encrypted string 16 hex characters long, "123456789012345" produces 32 character long string. I thnk the actual block size relates to the key and/or the IV value.
In this case, for a string to be decrypted, the count of hex characters must be evenly divisible by 16.
You are probably either reading past the end of the string in the memory stream, or the stream is including padding characters. In either case, you must stop reading before then.Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
|
|
|
|
|
Hi,
you have been around for several months, so you should know code needs to be shown with PRE tags so it keeps its formatting; and posting the same stuff twice isn't acceptable either. Please delete the one without replies, and edit the one heading this thread.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
Actually, the Pre tags are there, its just that he's used < instead of < I'm assuming he had Encode "<" (and other HTML) characters when pasting ticked. Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
|
|
|
|
|
You're right, I did not notice as I don't look at unformatted code at all...
If the poster isn't willing to do the posting right, then he is beyond help as far as I'm concerned.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
How to change the Index of one row of datatable ?
I have 10 items in datatable and now i wanna to bring the 6th item to zeroIndex of Datatable ?
|
|
|
|
|
Sort the datasource being used to populate the datatable or remove the item in question and use InsertAt to place it at index 0 I know the language. I've read a book. - _Madmatt
|
|
|
|
|
|
Hi everyone,
Does anyone have any idea how to do the following code snippet i posted below in web method?
protected void Page_Load(object sender, EventArgs e)
{
// Get the hostname
string myHost = System.Net.Dns.GetHostName();
// Get the IP from the host name
//string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[0].ToString();
string connect = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=C:\\inetpub\\wwwroot\\Request\\App_Data\\TV.mdb";
string query = "SELECT COUNT(IPAddress) FROM Request WHERE IPAddress = ?";
int result = 0;
using (OleDbConnection conn = new OleDbConnection(connect))
{
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("IPAddress", myHost);
conn.Open();
result = (int)cmd.ExecuteScalar();
}
}
if (result <= 0)
{
Response.Redirect("Unautorized.aspx");
}
thanks.
|
|
|
|
|
Do you mean using a method decorated with the WebMethod attribute and called from JavaScript? I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Ya, something like that. But i need to use C# instead of Javascript. Can you show me some examples on it?
thanks.
|
|
|
|
|
This make no sence. If you are using C# then call the method like any other method. I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi
I have one MDI(NewRoot) form and 2 child forms(StartUpForm ,StartUpForm1 ), in MDI formload i have assigned MDI using below code.
StartUpForm suf = new StartUpForm();
suf.MdiParent = this;
in StartUpForm i want to open StartUpForm1 with same mdi(NewRoot) so in StartUpForm's button click i have used below code
StartUpForm1 suf1 = new StartUpForm1(); suf1.Show();
Even though the form is opening but it is opening as seperate form.I want to open it as single window/form.
Thnaks & Regards
Pawan
|
|
|
|
|
If you're doing it in suf then you can assign it's MdiParent as the parent of the other form (you will have to do this after the constructor)
StartUpForm1 suf1 = new StartUpForm1();
suf1.MdiParent = this.MdiParent;
|
|
|
|
|
Hi
U Solved my issue
Thanku so much....
Pawan.
|
|
|
|
|
Hi,
I'm working on a project where I have to parse HTML pages in a C# Forms application. Parsing HTML is actually quite easy, espetially with mshtml. I just use the built in web browser and get all the information I need from the HTMLDocument and IHTMLDocument2.
I wanted to go a step further and also parse the javascript code, that is included in the web page. For now I am just able to get the tag and the included javascript code as a string.
Because javascript is one of the most widely spread script langues on the web, I assumed it would be as easy to parse as HTML. But I didn't find anything that would support my theory.
I just can't believe thet java script managing is not included in C#, but still, maybe I wasn't searching for an answer hard enough...
Does anyone know, if there is an easier way to parse javascript code than to write my own parser?
and sory for my bad english, i'm not a native speaker.
|
|
|
|
|
Ziga1 wrote: get all the information I need from the HTMLDocument and IHTMLDocument2.
Unnecessary, all you need is to get the WebResponse from a WebRequest.
What are you trying to accomplish with the JavaScript? I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Could you explain this? I don't see a way to get the parsed HTML directly from a WebResponse
|
|
|
|
|
|
Parsing the HTML by hand, ok, but surely that wouldn't be the best way all the time (maybe he needs a complete DOM tree? to me that seems likely, seeing as he even wants to parse the javascript)
edit: I wrote this post before reading his second post
|
|
|
|
|
Thats exactly what I would like .
As I said, geting the complete HTML DOM tree is not that hard.
But the script part is shown as one element. Maybe I overlooked something, but for now it seems that the script part is only accessible as the inner HTML of the element.
Thats my question. Is it possible to access and identify the elements inside the javascript part otherwise than to parse it as a raw string?
|
|
|
|
|
AFAIK there is no method to parse the javascript other than to process the string. Using a Regular Expression this shouldn't be that difficult and I would agrue easier with the string returned by WebRequest rather than walking the DOM in an HTMLDocument I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Ziga1 wrote: Is it possible to access and identify the elements inside the javascript part otherwise than to parse it as a raw string?
Not that I know of. Microsoft provides a scripting library that can parse and execute JavaScript, but as far as I've been able to find out it doesn't let you have the AST (or something similar).
|
|
|
|
|
There is no such DOM for JavaScript as there is for HTML. HTML is easy. It kind of resembles XML, with a tree of markup tags. Java has no such easy structure. It's more like C# and VB.NET. There are object models for those, Visual Studio uses them. But, Visual Studio does not have a DOM for JavaScript, nor does the .NET Framework.
The closest you may get is to research Sun's options and see if they have an equiv' DOM that you can use. Don't get your hopes up though...
|
|
|
|