Bad Data when Encrypt and Decrypt with TDES

馋奶兔 提交于 2019-12-25 09:57:33

问题


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


    }

回答1:


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



来源:https://stackoverflow.com/questions/4512348/bad-data-when-encrypt-and-decrypt-with-tdes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!