Generally, the cipertext length will always be the multiples of provider.BlockSize for block ciphers.
C# language, Encryption and decryption
Afon.zhang
396
Reputation points
C # language,
Is there an encryption algorithm that can increase the length of plaintext as well as the length of ciphertext,
For example: plaintext 16 bits, ciphertext 32 bits, plaintext 17 bits, ciphertext 34 bits
The following is my example, but the problem is that if the plaintext is 16 bits, then the ciphertext is 32 bits. If the plaintext is between 16 and 24, then the ciphertext is fixed 48 bits
Plaintext: 0-9 A-Z combination
public static string Encrypt3Des(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
{
try
{
var des = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(aStrKey),
Mode = mode
};
if (mode == CipherMode.CBC)
{
des.IV = Encoding.UTF8.GetBytes(iv);
}
var desEncrypt = des.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(aStrString);
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch (Exception e)
{
return string.Empty;
}
}
/// <summary>
/// 默认密钥
/// </summary>
private static string miyue = "01234567";
public static string DesEncrypt32(string strEncryptString)
{
StringBuilder strRetValue = new StringBuilder();
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(miyue.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Mode = CipherMode.CBC;//兼容其他语言的Des加密算法
provider.Padding = PaddingMode.None;//自动补0
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
//不使用base64编码
//return Convert.ToBase64String(mStream.ToArray());
//组织成16进制字符串
foreach (byte b in mStream.ToArray())
{
strRetValue.AppendFormat("{0:X2}", b);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
return strRetValue.ToString();
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="decryptString"></param>
/// <returns></returns>
public static string DesDecrypt32(string strDecryptString)
{
string strRetValue = "";
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(miyue.Substring(0, 8));
byte[] keyIV = keyBytes;
//不使用base64解码
//byte[] inputByteArray = Convert.FromBase64String(decryptString);
//16进制转换为byte字节
byte[] inputByteArray = new byte[strDecryptString.Length / 2];
for (int x = 0; x < strDecryptString.Length / 2; x++)
{
int i = (Convert.ToInt32(strDecryptString.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Mode = CipherMode.ECB;//兼容其他语言的Des加密算法
provider.Padding = PaddingMode.None;//自动补0
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
//需要去掉结尾的null字符
//strRetValue = Encoding.UTF8.GetString(mStream.ToArray());
strRetValue = Encoding.UTF8.GetString(mStream.ToArray()).TrimEnd('\0');
}
catch (Exception e)
{
Console.WriteLine(e);
}
return strRetValue;
}