Encryption String with Session Key in c#

1.2k views Asked by At

I want to encrypt String with SessionKey. Below is sample code I am using, but I am not getting the correct encrypted answer.

string = test;
SessionKey = "ThisIsASecretKey";

For encryption, I am using the method below:

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

After encryption, I am getting

jZî|ðçê`u0aC

but the correct answer would be

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

There are 0 answers