17/01/08

Criptare e decriptare con Visualstudio 2005

Ecco due comode funzioni per criptare e decriptare le stringhe:

public static string Encrypt(string encoding, string key)
{ try { byte[] encBytes = Encoding.ASCII.GetBytes(encoding.ToCharArray()); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
des.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(key));
des.Mode = CipherMode.ECB;
ICryptoTransform Transform = des.CreateEncryptor();
byte[]res = Transform.TransformFinalBlock(encBytes, 0, encBytes.Length);
return Convert.ToBase64String(res);
} catch (Exception)
{ throw new ApplicationException("Impossibile criptare la stringa: " + encoding);
}
}


public static string Decrypt(string encoded, string key)
{ try { byte[] encBytes = Convert.FromBase64String(encoded); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
des.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(key));
des.Mode = CipherMode.ECB;
ICryptoTransform Transform = des.CreateDecryptor();
byte[]res = Transform.TransformFinalBlock(encBytes, 0, encBytes.Length);
return Encoding.ASCII.GetString(res);
} catch (Exception)
{ throw new ApplicationException("Impossibile decriptare la stringa: " + encoded);
}

Nessun commento:

Posta un commento

Cosa ne pensi?

Forse ti potrebbero interessare anche:

Related Posts Plugin for WordPress, Blogger...