I have had to create a simple string encryption program for coursework, I
have completed the task and now have to do a write up on how it could be
improved at a later date. If you could look through the code and point me in
the correct direction one would be very grateful.
Example Input : j1mb0jay
Example Output 1 :
rZHKZbYZWn/4UgL9mAjN2DUz7X/UpcpRxXM9SO1Qkv kOe5nOPEKnZldps B7uHUNZ
Example Output 2 :
8SFgIdt0K0GqOgg Ot5VUzRc+sVtgPP QJt5xen7WksC3Sl jaXC/H38pWpjZ37tHyY
Example Outout 3 :
an+RFZnhJpyv+Ug dViO6SlZtPZ66Dz Z1tGFifpq3QkHr9 MX9O/JQkojuS2O0IYIG
As seen above I have used the time as a factor when creating the passwords,
so two users with the same password will not have the same hash stored in
the database.
public string JJEncryption(st ring password)
{
//Creates a random number generator.
Random random = new Random();
//Creates a random int.
double randomNo = random.NextDoub le();
//Turns the double into a number that i can use.
double roundedRandomNo = randomNo * 100;
//Case the double into and int (loosing all decimal places)
int randomInt = (int)roundedRan domNo;
//Gets the current milli second.
int milli = DateTime.Now.Mi llisecond;
//Convert the milli second and the random int into a string and
add it to an empty string;
string ePassword = ConvertToBase64 (milli.ToString ()) + "-" +
ConvertToBase64 (randomInt.ToSt ring());
//Update the value of milli by adding the random number to it.
milli = milli + randomInt;
//Foreach character in the paratmeter string "password"
foreach (char c in password)
{
//Convert the letter into a number.
int i = Convert.ToInt32 (c);
//Add the value of milli to the number representation of the
current letter.
i = i + milli;
//Add this as a string to the return string
ePassword = ePassword + "-" + i.ToString();
}
//Return the enrypted password.
ePassword = MD5Encrypt(ePas sword, true);
return ePassword;
}
private string ConvertToBase64 (string text)
{
try
{
byte[] enc = new byte[text.Length];
for (int i = 0; i < text.Length; i++)
{
enc[i] = System.Convert. ToByte(text[i]);
}
return System.Convert. ToBase64String( enc);
}
catch
{
}
return string.Empty;
}
//Helped from CodeProject.com
private string MD5Encrypt(stri ng toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UT F8.GetBytes(toE ncrypt);
// Get the key from config file
string key = ApplicationSett ings.MeetySetti ngs.Key;
//System.Windows. Forms.MessageBo x.Show(key);
//If hashing use get hashcode regards to your key
if (useHashing)
{
MD5CryptoServic eProvider hashmd5 = new
MD5CryptoServic eProvider();
keyArray =
hashmd5.Compute Hash(UTF8Encodi ng.UTF8.GetByte s(key));
//Always release the resources and flush data of the
Cryptographic service provide. Best Practice
hashmd5.Clear() ;
}
else
keyArray = UTF8Encoding.UT F8.GetBytes(key );
TripleDESCrypto ServiceProvider tdes = new
TripleDESCrypto ServiceProvider ();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes. We choose
ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKC S7;
ICryptoTransfor m cTransform = tdes.CreateEncr yptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.Tran sformFinalBlock (toEncryptArray , 0, toEncryptArray. Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase6 4String(resultA rray, 0,
resultArray.Len gth);
}
--
Regards JJ (UWA)
--
Regards JJ (UWA)
have completed the task and now have to do a write up on how it could be
improved at a later date. If you could look through the code and point me in
the correct direction one would be very grateful.
Example Input : j1mb0jay
Example Output 1 :
rZHKZbYZWn/4UgL9mAjN2DUz7X/UpcpRxXM9SO1Qkv kOe5nOPEKnZldps B7uHUNZ
Example Output 2 :
8SFgIdt0K0GqOgg Ot5VUzRc+sVtgPP QJt5xen7WksC3Sl jaXC/H38pWpjZ37tHyY
Example Outout 3 :
an+RFZnhJpyv+Ug dViO6SlZtPZ66Dz Z1tGFifpq3QkHr9 MX9O/JQkojuS2O0IYIG
As seen above I have used the time as a factor when creating the passwords,
so two users with the same password will not have the same hash stored in
the database.
public string JJEncryption(st ring password)
{
//Creates a random number generator.
Random random = new Random();
//Creates a random int.
double randomNo = random.NextDoub le();
//Turns the double into a number that i can use.
double roundedRandomNo = randomNo * 100;
//Case the double into and int (loosing all decimal places)
int randomInt = (int)roundedRan domNo;
//Gets the current milli second.
int milli = DateTime.Now.Mi llisecond;
//Convert the milli second and the random int into a string and
add it to an empty string;
string ePassword = ConvertToBase64 (milli.ToString ()) + "-" +
ConvertToBase64 (randomInt.ToSt ring());
//Update the value of milli by adding the random number to it.
milli = milli + randomInt;
//Foreach character in the paratmeter string "password"
foreach (char c in password)
{
//Convert the letter into a number.
int i = Convert.ToInt32 (c);
//Add the value of milli to the number representation of the
current letter.
i = i + milli;
//Add this as a string to the return string
ePassword = ePassword + "-" + i.ToString();
}
//Return the enrypted password.
ePassword = MD5Encrypt(ePas sword, true);
return ePassword;
}
private string ConvertToBase64 (string text)
{
try
{
byte[] enc = new byte[text.Length];
for (int i = 0; i < text.Length; i++)
{
enc[i] = System.Convert. ToByte(text[i]);
}
return System.Convert. ToBase64String( enc);
}
catch
{
}
return string.Empty;
}
//Helped from CodeProject.com
private string MD5Encrypt(stri ng toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UT F8.GetBytes(toE ncrypt);
// Get the key from config file
string key = ApplicationSett ings.MeetySetti ngs.Key;
//System.Windows. Forms.MessageBo x.Show(key);
//If hashing use get hashcode regards to your key
if (useHashing)
{
MD5CryptoServic eProvider hashmd5 = new
MD5CryptoServic eProvider();
keyArray =
hashmd5.Compute Hash(UTF8Encodi ng.UTF8.GetByte s(key));
//Always release the resources and flush data of the
Cryptographic service provide. Best Practice
hashmd5.Clear() ;
}
else
keyArray = UTF8Encoding.UT F8.GetBytes(key );
TripleDESCrypto ServiceProvider tdes = new
TripleDESCrypto ServiceProvider ();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes. We choose
ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKC S7;
ICryptoTransfor m cTransform = tdes.CreateEncr yptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.Tran sformFinalBlock (toEncryptArray , 0, toEncryptArray. Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase6 4String(resultA rray, 0,
resultArray.Len gth);
}
--
Regards JJ (UWA)
--
Regards JJ (UWA)
Comment