I'm quite new in programming .I wrote the below code in order to prompt the user for a password to encrypting a file, But it just work when the length of password is 8, What can I do on order to accepting any number of characters for the password?
string pass = textBox2.Text.ToString();
string password = @"" + pass + "";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(@"c:\\users\\new", FileMode.Create);
name = fsCrypt.Name;
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(filename, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
The Advanced Encryption Standard (AES), also known by its original name Rijndael (Dutch pronunciation: [ˈrɛindaːl]), is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001.
The Rijndael algorithm, in conjunction with safe configuration values (i.e. AES ), is very robust and secure. The only true measure of an encryption algorithm's security is its consistent and long-lived exposure to cryptanalysis and attempts to defeat it by many cryptographers.
AES is a reduced version of Rijndael where it is only defined for block sizes of 128 bit whereas Rijndael is defined for block sizes of 128, 192 and 256 bit. If a different block size between encryption and decryption is used, then it is not possible to recover the original plaintext.
Out of 128-bit, 192-bit, and 256-bit AES encryption, which progressively use more rounds of encryption for improved security, 128-bit AES encryption is technically the least secure.
You need a function that is going to get a valid key length for Rijndael from your password, and at the moment, your use of UnicodeEncoding.GetBytes
is only going to give this for certain discrete lengths of password, as you've discovered.
You should use another function to get a key from your password - perhaps take the byte array you have generated, and run a cryptographic hash function like SHA1 on it. SHA1 will give you a 128 bit length, like your 8 character passwords currently do, but regardless of the length of the password.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With