使用 TDES 加密和解密时出现错误数据

发布于 2024-10-08 19:51:56 字数 4314 浏览 4 评论 0原文

我正在使用 TDES 加密一个大字符串并尝试解密它。无法理解出了什么问题。 当它在解密中执行读取功能时,我从加密异常中得到“错误数据”。 (我用注释标记了该行)

生成并加密字符串:

public void MssGenerateKeyPair(string ssSymmetricKey, out string ssCipheredKeyPair, out string ssPublicKey) {
        ssCipheredKeyPair = string.Empty;
        ssPublicKey = string.Empty;

        //Symmetric key is the hash of user's signing password
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
        byte[] key = new byte[24];
        tempKey.CopyTo(key, 0);
        //if symmetric keys is < 24
        for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
        {
            key[i] = tempKey[index];
        }

        //define symmetric encryption data
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = key;
        tdes.IV = new byte[8];
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.PKCS7;

        //define assymetric stuff
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(), CryptoStreamMode.Write);
        byte[] toChipher = Encoding.UTF8.GetBytes(rsa.ToXmlString(true));
        cryptoStream.Write(toChipher, 0, toChipher.Length);
        byte[] cipheredKeyPair = memoryStream.ToArray();
        ssCipheredKeyPair = Convert.ToBase64String(cipheredKeyPair);
        ssPublicKey = rsa.ToXmlString(false);
        cryptoStream.Close();
        memoryStream.Close();


    } // MssGenerateKeyPair

解密字符串

public void MssSignData(string ssSymmetricKey, byte[] ssDataToSign, byte[] ssCipheredKeyPair, out byte[] ssSignature, out byte[] ssSignedData) {
        ssSignature = new byte[] {};
        ssSignedData = new byte[] {};

        //Symmetric key is the hash of user's signing password
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
        byte[] key = new byte[24];
        tempKey.CopyTo(key, 0);
        //if symmetric keys is < 24
        for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
        {
            key[i] = tempKey[index];
        }

        //define symmetric encryption data
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = key;
        tdes.IV = new byte[8];
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.PKCS7;

        MemoryStream memoryStream = new MemoryStream(ssCipheredKeyPair);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateDecryptor(), CryptoStreamMode.Read);
        MemoryStream plainMemoryStream = new MemoryStream();

        byte[] tempPlainBytes = new byte[1024];

        int read = 0;
        int totalRead = 0;
        do
        {
            //################################################################
            //ERROR IN THE FOLLOWING LINE
            //################################################################
            read = cryptoStream.Read(tempPlainBytes, 0, tempPlainBytes.Length);
            totalRead += read;
            plainMemoryStream.Write(tempPlainBytes, 0, read);
        } while (read > 0);

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(Encoding.UTF8.GetString(plainMemoryStream.ToArray()));

        ssSignature = rsa.SignData(ssDataToSign, new SHA1CryptoServiceProvider());
        ssSignedData = ssDataToSign;
        cryptoStream.Close();
        memoryStream.Close();
        plainMemoryStream.Close();

    } // MssSignData

要测试的代码

 private void button1_Click(object sender, EventArgs e)
    {
        string key;
        string pkey;
        byte[] sig;
        byte[] avs;

        OutSystems.NssPseudoCertificates.CssPseudoCertificates c = new OutSystems.NssPseudoCertificates.CssPseudoCertificates();

        c.MssGenerateKeyPair("xpto",out key, out pkey);
        c.MssSignString("xpto", "hello", key, out sig, out avs);


    }

I'm encrypting a big string with TDES and trying to decrypt it. Can't understand what's is wrong.
When it does the READ function in decryption I get "Bad Data" from the Cryptographic Exception. (I marked the line with comments)

To generate and encrypt the string:

public void MssGenerateKeyPair(string ssSymmetricKey, out string ssCipheredKeyPair, out string ssPublicKey) {
        ssCipheredKeyPair = string.Empty;
        ssPublicKey = string.Empty;

        //Symmetric key is the hash of user's signing password
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
        byte[] key = new byte[24];
        tempKey.CopyTo(key, 0);
        //if symmetric keys is < 24
        for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
        {
            key[i] = tempKey[index];
        }

        //define symmetric encryption data
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = key;
        tdes.IV = new byte[8];
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.PKCS7;

        //define assymetric stuff
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateEncryptor(), CryptoStreamMode.Write);
        byte[] toChipher = Encoding.UTF8.GetBytes(rsa.ToXmlString(true));
        cryptoStream.Write(toChipher, 0, toChipher.Length);
        byte[] cipheredKeyPair = memoryStream.ToArray();
        ssCipheredKeyPair = Convert.ToBase64String(cipheredKeyPair);
        ssPublicKey = rsa.ToXmlString(false);
        cryptoStream.Close();
        memoryStream.Close();


    } // MssGenerateKeyPair

To decrypt the string

public void MssSignData(string ssSymmetricKey, byte[] ssDataToSign, byte[] ssCipheredKeyPair, out byte[] ssSignature, out byte[] ssSignedData) {
        ssSignature = new byte[] {};
        ssSignedData = new byte[] {};

        //Symmetric key is the hash of user's signing password
        SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
        byte[] tempKey = sha1.ComputeHash(Encoding.UTF8.GetBytes(ssSymmetricKey));
        byte[] key = new byte[24];
        tempKey.CopyTo(key, 0);
        //if symmetric keys is < 24
        for (int index = 0, i = tempKey.Length; i < key.Length; index++, i++)
        {
            key[i] = tempKey[index];
        }

        //define symmetric encryption data
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = key;
        tdes.IV = new byte[8];
        tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.PKCS7;

        MemoryStream memoryStream = new MemoryStream(ssCipheredKeyPair);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, tdes.CreateDecryptor(), CryptoStreamMode.Read);
        MemoryStream plainMemoryStream = new MemoryStream();

        byte[] tempPlainBytes = new byte[1024];

        int read = 0;
        int totalRead = 0;
        do
        {
            //################################################################
            //ERROR IN THE FOLLOWING LINE
            //################################################################
            read = cryptoStream.Read(tempPlainBytes, 0, tempPlainBytes.Length);
            totalRead += read;
            plainMemoryStream.Write(tempPlainBytes, 0, read);
        } while (read > 0);

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(Encoding.UTF8.GetString(plainMemoryStream.ToArray()));

        ssSignature = rsa.SignData(ssDataToSign, new SHA1CryptoServiceProvider());
        ssSignedData = ssDataToSign;
        cryptoStream.Close();
        memoryStream.Close();
        plainMemoryStream.Close();

    } // MssSignData

Code to test

 private void button1_Click(object sender, EventArgs e)
    {
        string key;
        string pkey;
        byte[] sig;
        byte[] avs;

        OutSystems.NssPseudoCertificates.CssPseudoCertificates c = new OutSystems.NssPseudoCertificates.CssPseudoCertificates();

        c.MssGenerateKeyPair("xpto",out key, out pkey);
        c.MssSignString("xpto", "hello", key, out sig, out avs);


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-10-15 19:51:56

就像 Marvin Smit 在他的评论中所说:为了添加同花,我做到了并且成功了!
功劳归他所有!

Like Marvin Smit said in his comments: to add the flush, I did it and worked!
Credits go to him!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文