使用DESCryptographer解密时,加密的文本返回奇怪的字符

时间:2018-07-19 06:02:42

标签: c# encryption cryptography

如果我在相同的括号内定义加密文本和解密文本,

class Program
{
    static void Main(string[] args)
    {
        SymmetricCryptograperManager symmetricCryptograperManager = new SymmetricCryptograperManager();
        int key = 55999;
        var schema = symmetricCryptograperManager.GetSymmetricCryptographer(SymmetricCryptographyStrategy.DESCryptography, "000" + key.ToString());

        string textToBeEncrpted = "ozgur547@gmail.com";
        Console.WriteLine(textToBeEncrpted);

        string encryptedText = schema.Encrypt(textToBeEncrpted);
        Console.WriteLine(encryptedText);

        string decryptedText = schema.Decrypt(encryptedText);            
        Console.WriteLine(decryptedText);

        Console.ReadKey();
    }
}

输出正常:

ozgur547@gmail.com
E+jPjrGaU9+IveRt/XdZzUHAerV8+VBG
ozgur547@gmail.com

但是,在我的代码中的某个时刻,我必须像这样手动定义E+jPjrGaU9+IveRt/XdZzUHAerV8+VBG加密文本,

class Program
{
    static void Main(string[] args)
    {

        SymmetricCryptograperManager symmetricCryptograperManager = new SymmetricCryptograperManager();
        int key = 55999;
        var schema = symmetricCryptograperManager.GetSymmetricCryptographer(SymmetricCryptographyStrategy.DESCryptography, "000" + key.ToString());

        //string textToBeEncrpted = "ozgur547@gmail.com";
        //Console.WriteLine(textToBeEncrpted);

        //string encryptedText = schema.Encrypt(textToBeEncrpted);
        //Console.WriteLine(encryptedText);

        string decryptedText = schema.Decrypt("E+jPjrGaU9+IveRt/XdZzUHAerV8+VBG");            
        Console.WriteLine(decryptedText);

        Console.ReadKey();
    }
}

很遗憾,我回来了?6i?547@gmail.com

以下是加密和解密的来源:

public class DESCryptographer : SymmetricCryptographer
{
    private readonly DESCryptoServiceProvider _des = new DESCryptoServiceProvider();
    public DESCryptographer(string key) : base(key)
    {

        _des.GenerateIV();
        IV = _des.IV;
    }

    public DESCryptographer(string key, string iV) : base(key, Encoding.UTF8.GetBytes(iV))
    {

    }

    public override string Encrypt(string plainText)
    {
        MemoryStream memoryStream = new MemoryStream();

        ICryptoTransform cEncryptor = _des.CreateEncryptor(Encoding.UTF8.GetBytes(Key), IV);
        CryptoStream cryptoStream = new CryptoStream(memoryStream,
            cEncryptor, CryptoStreamMode.Write);
        StreamWriter writer = new StreamWriter(cryptoStream, Encoding.UTF8);
        writer.Write(plainText);
        writer.Flush();
        cryptoStream.FlushFinalBlock();
        writer.Flush();
        return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
    }

    public override string Decrypt(string encryptedText)
    {
        MemoryStream memoryStream = new MemoryStream
            (Convert.FromBase64String(encryptedText));
        ICryptoTransform cDecryptor = _des.CreateDecryptor(Encoding.UTF8.GetBytes(Key), IV);
        CryptoStream cryptoStream = new CryptoStream(memoryStream,
            cDecryptor, CryptoStreamMode.Read);
        StreamReader reader = new StreamReader(cryptoStream, Encoding.UTF8);
        return reader.ReadToEnd();
    }
}

这是主要类:

public abstract class SymmetricCryptographer
{
    public string Key { get; }
    public byte[] IV { get; protected set; }

    protected SymmetricCryptographer(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException(nameof(key), "Key should not be null");
        }

        this.Key = key;
    }

    protected SymmetricCryptographer(string key, byte[] iV)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException(nameof(key), "Key should not be null");
        }

        this.Key = key;
        this.IV = iV ?? throw new ArgumentNullException(nameof(iV), "IV should not be null");
    }

    public abstract string Encrypt(string plainText);
    public abstract string Decrypt(string encryptedText);
}

手动输入加密文本后如何获得正确的结果?

谢谢!

0 个答案:

没有答案