0

我正在用 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);


    }
4

1 回答 1

0

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

于 2010-12-24T16:02:33.747 回答