Click here to Skip to main content
15,910,130 members
Home / Discussions / C#
   

C#

 
GeneralRe: Hello again.... need help! Pin
DaveyM691-Apr-09 3:46
professionalDaveyM691-Apr-09 3:46 
GeneralRe: Thanx to Davey and Hugo Monkey! Pin
DaveyM691-Apr-09 0:44
professionalDaveyM691-Apr-09 0:44 
AnswerRe: Problem with LinkLabel Pin
Paul Unsworth1-Apr-09 0:25
Paul Unsworth1-Apr-09 0:25 
QuestionEncryption Problem Pin
Paul Unsworth1-Apr-09 0:15
Paul Unsworth1-Apr-09 0:15 
AnswerRe: Encryption Problem Pin
Rajdeep.NET is BACK1-Apr-09 0:36
Rajdeep.NET is BACK1-Apr-09 0:36 
AnswerRe: Encryption Problem Pin
SeMartens1-Apr-09 0:43
SeMartens1-Apr-09 0:43 
GeneralRe: Encryption Problem Pin
Paul Unsworth1-Apr-09 2:08
Paul Unsworth1-Apr-09 2:08 
AnswerRe: Encryption Problem Pin
MumbleB1-Apr-09 2:53
MumbleB1-Apr-09 2:53 
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.
//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.

AnswerRe: Encryption Problem Pin
Rajdeep.NET is BACK1-Apr-09 2:53
Rajdeep.NET is BACK1-Apr-09 2:53 
GeneralRe: Encryption Problem Pin
Paul Unsworth1-Apr-09 3:22
Paul Unsworth1-Apr-09 3:22 
Questionhandled files Pin
lost_in_code1-Apr-09 0:13
lost_in_code1-Apr-09 0:13 
AnswerRe: handled files Pin
stancrm1-Apr-09 0:34
stancrm1-Apr-09 0:34 
GeneralRe: handled files Pin
lost_in_code1-Apr-09 0:39
lost_in_code1-Apr-09 0:39 
AnswerRe: handled files Pin
0x3c01-Apr-09 0:44
0x3c01-Apr-09 0:44 
GeneralRe: handled files Pin
lost_in_code1-Apr-09 0:58
lost_in_code1-Apr-09 0:58 
QuestionDataGridView.RowFilter question Pin
kanchoette31-Mar-09 23:51
kanchoette31-Mar-09 23:51 
AnswerRe: DataGridView.RowFilter question Pin
Eddy Vluggen31-Mar-09 23:59
professionalEddy Vluggen31-Mar-09 23:59 
AnswerRe: DataGridView.RowFilter question Pin
vinodkrebc1-Apr-09 1:24
vinodkrebc1-Apr-09 1:24 
QuestionExecuting PL/Sql Anonymous block from ADO Pin
Muammar©31-Mar-09 23:27
Muammar©31-Mar-09 23:27 
AnswerRe: Executing PL/Sql Anonymous block from ADO Pin
Muammar©1-Apr-09 2:46
Muammar©1-Apr-09 2:46 
QuestionHow to lock the adjustment of column runtime. Pin
saksp31-Mar-09 23:14
saksp31-Mar-09 23:14 
AnswerRe: How to lock the adjustment of column runtime. Pin
Eddy Vluggen1-Apr-09 0:21
professionalEddy Vluggen1-Apr-09 0:21 
AnswerRe: How to lock the adjustment of column runtime. Pin
vinodkrebc1-Apr-09 1:26
vinodkrebc1-Apr-09 1:26 
Questionis sending data over a same socket possible Pin
Mubeen.asim31-Mar-09 23:10
Mubeen.asim31-Mar-09 23:10 
AnswerRe: is sending data over a same socket possible Pin
stancrm31-Mar-09 23:13
stancrm31-Mar-09 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.