|
Thanx Davey!
U did so much for me! U r great! Actually, I am not that expert like u guys. I am only a begineer, trying to figure myself out in the IT industry. Can u give me your Email address such that I can send u a greetings of compliment. Hope to hear from u soon in the forum.
Take care Genius!
Rajdeep
|
|
|
|
|
You're welcome. That's why we come here - to help, and be helped when needed.
We were all beginners at one time so don't worry about that, and welcome to CodeProject.
(You can email me through the link that's at the bottom of each post.)
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
No problem
I normally use my own version of the link label which I've just added a URL property to and overridden the OnClick so it starts the process automatically with no coding needed.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
namespace WinForms.Controls
{
public class URLLabel : LinkLabel
{
public URLLabel()
{
URL = "about:blank";
}
[DefaultValue("about:blank"),
Description("The URL that will launched in the default browser when the label is clicked.")]
public string URL
{
get;
set;
}
protected override void OnClick(EventArgs e)
{
try
{
Process.Start(URL);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
base.OnClick(e);
}
}
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I've not tested this, but have you tried something like System.Diagnostics.Process.Start("HTTP://www.microsoft.com"); ??
oooo, the Jedi's will feel this one....
|
|
|
|
|
Hi Everyone.
I'm pretty new to C#, and I'm having a bit of a problem. I have an application which will be saving passwords to an xml document, so I want to encrypt them. I have created some simple code which manages to encrypt the information but complains when I try to decrypt it with “Padding is invalid and cannot be removed”. I have tried different padding modes including PaddingMode.None with no luck.
I’m sure I'm missing something simple... Please can somebody help me???
Thanks
Code
class Program
{
static void Main(string[] args)
{
string Orig = "Paul Unsworth";
string Key = "as^le)ds~{3sjlk3*$ake^ia29?jf%a!";
string IV = "ad|k#f(*&lk$df()";
string Encrypted = Encrypt(Orig, Key, IV);
string Decrypted = Decrypt(Encrypted, Key, IV);
Console.WriteLine("Original Text:\r\n {0}", Orig);
Console.WriteLine("Encrypted Text:\r\n {0}", Encrypted);
Console.WriteLine("Decrypted Text:\r\n {0}", Decrypted);
Console.ReadLine();
}
static string Encrypt(string val, string key, string iv)
{
byte[] bVal = toBytes(val);
byte[] bKey = toBytes(key);
byte[] bIV = toBytes(iv);
MemoryStream ms = new MemoryStream();
Rijndael r = Rijndael.Create();
r.Key = bKey;
r.IV = bIV;
r.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, r.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(bVal, 0, bVal.Length);
cs.Close();
string result = toString(ms.ToArray());
ms.Close();
return result;
}
static string Decrypt(string val, string key, string iv)
{
byte[] bVal = toBytes(val);
byte[] bKey = toBytes(key);
byte[] bIV = toBytes(iv);
MemoryStream ms = new MemoryStream();
Rijndael r = Rijndael.Create();
r.Key = bKey;
r.IV = bIV;
r.Padding = PaddingMode.PKCS7;
CryptoStream cs = new CryptoStream(ms, r.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(bVal, 0, bVal.Length);
cs.Close();
string result = toString(ms.ToArray());
ms.Close();
return result;
}
static byte[] toBytes(string str)
{
ASCIIEncoding a = new ASCIIEncoding();
return a.GetBytes(str);
}
static string toString(byte[] b)
{
ASCIIEncoding a = new ASCIIEncoding();
return a.GetString(b);
}
}
oooo, the Jedi's will feel this one....
|
|
|
|
|
Hey Monkey,
Your problem seems a bit tough to me... why not consult some expert programmer and if possible shorten the code. Good programmers often find it harassing reading such long codes.
Happy Programming.....
|
|
|
|
|
Hmm forums say that this might be a problem if everything is not read to the end. Call cs.Flush() before you close the CryptoStream.
If this doesn't work, I would think about using an hash-algorithm because you don't need to decrypt the password ever again. Just encrypt the user input and check if this equals the stored hash before.
Regards
Sebastian
|
|
|
|
|
Thanks for your reply Sebastian.
I've tried cs.Flush() and unfortunatly it did not work.
The pain in the backside is I need to be able to decrypt the password, as it is for SQL Server Authentication. The app is to run/create a load of old long standing reports automatically, and for this, the User Name and Password need to be passed to SQL Server. (We have people unwilling to move away from excel workbooks to better reporting methods in this company )
Thanks for you help...
oooo, the Jedi's will feel this one....
|
|
|
|
|
Hi, I had a look and can't see where this is going wrong. I have the below code though that I know works and which I use to encrypt passwords to and from the Database.
public static string Encrypt(string plainText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,
salValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor,
CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
public static string Decrypt(string cipherText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);
return plainText;
}
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
I have been having a look at the below and I can't figure this out as well. However, I have the below code that works which I use to encrypt passwords to and from the DB.
//Encrypt Value formatted as a base64-encoded string. public static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize) { //Covert strings into byte arrays. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] salValueBytes = Encoding.ASCII.GetBytes(saltValue); //Convert our plaintext password to a byte array. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); //Create a password from which the value will be derived PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salValueBytes, hashAlgorithm, passwordIterations); //Use the password to generate psuedo-random bytes for encryption key. //Specify the size of the key in bytes (intead of bits). byte[] keyBytes = password.GetBytes(keySize / 8); //Create uninitialized Rijndael encryption object. RijndaelManaged symmetricKey = new RijndaelManaged(); //Set encryption mode to Cipher Block Chaining(CBC). symmetricKey.Mode = CipherMode.CBC; //Generate encryptor from the existing key bytes and initialization vector. ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); //Define memorystream to hold encrypted data. MemoryStream memoryStream = new MemoryStream(); //Define Cryptographic stream. (Always use write mode) CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); //Start Encrypting cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); //Finish encrypting cryptoStream.FlushFinalBlock(); //Convert encrypted stream to a byte array byte[] cipherTextBytes = memoryStream.ToArray(); //Close both streams memoryStream.Close(); cryptoStream.Close(); //Convert to encrypted into base64-encoding string string cipherText = Convert.ToBase64String(cipherTextBytes); //return encrypted string return cipherText; } //Now for the Decyption. public static string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] cipherTextBytes = Convert.FromBase64String(cipherText); PasswordDeriveBytes password = new PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keySize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initVectorBytes); MemoryStream memoryStream = new MemoryStream(cipherTextBytes); CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); memoryStream.Close(); cryptoStream.Close(); string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); return plainText; }
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Thats great. Thanks guys. I'll give your code a try...
oooo, the Jedi's will feel this one....
|
|
|
|
|
how can i get all files handled by a specific process without using WMI
in c#?
|
|
|
|
|
Do you mean this one?
foreach(ProcessModule pm in Process.GetCurrentProcess().Modules)
{
Console.WriteLine(pm.FileName);
}
|
|
|
|
|
No,I didn't mean modules of a process.
What i need get non executable files associated to a process like .doc , .pdf
|
|
|
|
|
|
one of running processes in the windows task manager is winword.exe
I need to retrieve all running files with .doc extension(as an example)
|
|
|
|
|
I have a DataGridView for which two if its columns are 'Period' and 'Year'.
How can I filter the grid to display, say, all the rows which fall between period 9 2007 and period 5 2008 please?
|
|
|
|
|
(Period > 9 AND Year > 2007) AND (Period < 5 AND Year < 2008)
Something like this?
I are troll
|
|
|
|
|
You have to use DataView obj=new DataView(ds.tables[0]);
obj.RowFilter="your condition here";
now put obj as your datasource
|
|
|
|
|
Hi everyone,
My question is simply about how to execute such an anonymous pl/sql block from ado, the below example works fine if pasted in my Oracle Sql*Plus terminal but not from my application, what's more, if I made it simple like just CREATE SEQUENCE MY_SEQUENCE it would work but as you can see, my script is a bit more complicated than that and I dont know how to make ado do it for me like it's being executed directly at the database terminal..
Please advise...
string strSEQ =
@"DECLARE
N NUMBER(10);
BEGIN
SELECT MAX(ID)+1 INTO N FROM MY_TABLE;
-- CREATING SEQUENCE
Execute immediate ('CREATE SEQUENCE SEQ_TEMP
MINVALUE 1
START WITH '||N||'
INCREMENT BY 1
CACHE 20
ORDER');
END;
/
"
OracleCommand cmd = new OracleCommand(String.Empty, _connection);
cmd.CommandText = strSEQ;
cmd.ExecuteNonQuery();
Internal .Net Framework Data Provider error 30.
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
Just found another way round by generating the desired script file, storing it in a temporary file and execute it from SQLPLUS. Here's how:
string strSEQ =
@"DECLARE
N NUMBER(10);
BEGIN
SELECT MAX(ID)+1 INTO N FROM "+ strTableName +@";
-- CREATING SEQUENCE
Execute immediate ('DROP SEQUENCE SEQ_TEMP');
Execute immediate ('CREATE SEQUENCE SEQ_TEMP
MINVALUE 1
START WITH '||N||'
INCREMENT BY 1
CACHE 20
ORDER');
END;
/
";
string strTempFile = Environment.GetEnvironmentVariable("TEMP")+"\\tmp_SCRIPT.SQL";
string CR = "\r\n";
System.IO.StreamWriter sw;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.EnableRaisingEvents = true;
p.Exited += new System.EventHandler(p_Exited);
p.StartInfo.FileName = @"SQLPLUS.exe";
sw = new System.IO.StreamWriter(strTempFile);
sw.Write(strSEQ);
sw.Flush();
sw.Close();
p.StartInfo.Arguments = @"USERNAME/PASSWORD @" + strTempFile + " >> " +
strTempFile.Replace(".sql",".log");
p.Start();
}
private void p_Exited(object sender, EventArgs e)
{
MessageBox.Show("Done");
}
All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)
|
|
|
|
|
How to lock datagrid column width adjustment at runtime
thanks
|
|
|
|
|
Use the AllowUserToResizeColumns property. Read all about it here[^].
I are troll
|
|
|
|
|
Hi,
you can use the template of DataGrid using table and set the table td width
I am sure this will solve your problem
Thank You
Vinod Kr.
|
|
|
|
|
hello....i have created a socket in my application and i need to send the data over an interval of 10 seconds so am creating a new socket in the timer.
the problem with this approach is that the application is creating a new socket every 10 seconds so that my server is getting lots of exceptions...
may i know is it possible to send the data over a single socket insted of creating a new socket every time i need to send the data..
thanks...
|
|
|
|
|