c#中带有会话密钥的加密字符串

时间:2016-05-02 10:36:11

标签: c# asp.net encryption bouncycastle session-keys

我想用String加密SessionKey。下面是我正在使用的示例代码,但我没有得到正确的加密答案。

string = test;
SessionKey = "ThisIsASecretKey";

对于加密,我使用以下方法:

 byte[] array = Encoding.ASCII.GetBytes("ThisIsASecretKey");

         byte[] array1 = Encoding.ASCII.GetBytes("test");
         byte[] reult = encryptUsingSessionKey(array, array1);

 public byte[] encryptUsingSessionKey(byte[] skey,byte[] data)
    {
        Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher cipher = new Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher(new AesEngine(), new Pkcs7Padding());

        cipher.Init(true, new Org.BouncyCastle.Crypto.Parameters.KeyParameter(skey));

        int outputSize = cipher.GetOutputSize(data.Length);

        byte[] tempOP = new byte[outputSize];
        int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
        int outputLen = cipher.DoFinal(tempOP, processLen);

        byte[] result = new byte[processLen + outputLen];

        tempOP.CopyTo(result, 0);
       // tempOP.CopyTo(tempOP,0,result,0,result.Length);
        return result;
    }

加密后,我正在

jZî|ðçê`u0aC

但正确答案是

ƒ_jZî|ðç_ê‹\`u0aC

0 个答案:

没有答案