Encrypt string with fixed lenght and characters

Sarah 186 Reputation points
2022-10-09T10:09:26.807+00:00

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

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,391 questions
{count} votes