Encrypt string with Bouncy Castle AES/CBC/PKCS7

前端 未结 2 1538
无人及你
无人及你 2020-12-29 04:45

I have been looking everywhere for some sample code on how to encrypt a simple string with the encryption in the title using the Bouncy Castle Framework.

This code w

2条回答
  •  天命终不由人
    2020-12-29 05:22

    enter link description here

            byte[] k; //32 byte
            string para; // plaintext
            string msgRefNo; // 16byte
    
            byte[] inputBytes = Encoding.UTF8.GetBytes(para);
            byte[] IV = Encoding.UTF8.GetBytes(msgRefNo);
            byte[] key = k;
    
    
            AesEngine engine = new AesEngine();
            CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
            PaddedBufferedBlockCipher cipher1 = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
    
            KeyParameter keyParam = new KeyParameter(key);
            ParametersWithIV keyParamWithIv = new ParametersWithIV(keyParam, IV);
    
    
            cipher1.Init(true, keyParamWithIv); //Error Message thrown
            byte[] outputBytes = new byte[cipher1.GetOutputSize(inputBytes.Length)]; //cip
            int length = cipher1.ProcessBytes(inputBytes, outputBytes, 0);
            cipher1.DoFinal(outputBytes, length); //Do the final block
            string encryptedInput = Convert.ToBase64String(outputBytes);
            return encryptedInput;
    

提交回复
热议问题