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

时间:2010-12-22 18:24:33

标签: c# encryption cryptographicexception tripledes

我正在使用TDES加密一个大字符串并尝试解密它。无法理解什么是错的。 当它在解密时执行READ功能时,我从加密异常中得到“坏数据”。 (我用评论标记了这一行)

生成并加密字符串:

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);


    }

1 个答案:

答案 0 :(得分:0)

就像Marvin Smit在评论中所说:加入同花顺,我做到了并且奏效了! 积分归他所有!