Encrypt string with fixed lenght and characters
Sarah
186
Reputation points
I use the following methods to encrypt and decrypt a string:
private byte[] key = new byte[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
private byte[] iv = new byte[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
public string Crypt(string text)
{
SymmetricAlgorithm algorithm = DES.Create();
ICryptoTransform transform = algorithm.CreateEncryptor(key, iv);
byte[] inputbuffer = Encoding.Unicode.GetBytes(text);
byte[] outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
return Convert.ToBase64String(outputBuffer);
}
public string Decrypt(string text)
{
SymmetricAlgorithm algorithm = DES.Create();
ICryptoTransform transform = algorithm.CreateDecryptor(key, iv);
byte[] inputbuffer = Convert.FromBase64String(text);
byte[] outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
return Encoding.Unicode.GetString(outputBuffer);
}
Is there a way to define a specific length and character for the encrypted string?
For example:
const string chars = "abcdeABCDE12345";
const int resLen = 5;
String to encrypt:: HelloWord
Result: CE1b5
Sign in to answer