跨平台上的AES cbc填充加密/解密(.net c#和代号一个充气城堡) [英] AES cbc padding encryption/decryption on cross platform (.net c# and codename one bouncy castle)

查看:134
本文介绍了跨平台上的AES cbc填充加密/解密(.net c#和代号一个充气城堡)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加密/解密在跨平台上不起作用。

Encryption/Decryption won't work in cross platform.

我使用此链接使用代号为1的弹性城堡AES密码对文本进行加密/解密。

I have used this link to encrypt/decrypt text using bouncy castle AES cipher within codename one.

在J2ME中使用Bouncycastle进行AES加密/解密示例

从服务器端(.net)开始,我正在使用此链接来实现相同的方法。

While from server side (.net) , i am using this link to implement same method.

http://zenu.wordpress.com/2011/09/21/aes-128bit-cross-platform-java-and-c-encryption-compatibility/

现在我没有收到任何错误,但是从代号1加密后,不会在服务器端得到完全解密,反之亦然。

now i am not getting any error but encrypted from codename one will not getting fully decrypted on server side and vice a versa.

任何人都请帮我解决这个问题。

any one please help me out on this.

来自代号一的代码:

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Base64;


public class Test
{
    private static PaddedBufferedBlockCipher cipher = null;
    public static void main(String[] args)
    {
        try
        {
            byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8");
            byte[] iv = new byte[16];

            PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                    new CBCBlockCipher(
                    new AESEngine()) );

            //Encryption
            String plainText = "Hello How are you !2#&*()% 123456@";
            byte[] plainData = plainText.getBytes("UTF-8");
            KeyParameter keyParam = new KeyParameter(key);
            CipherParameters ivAndKey = new ParametersWithIV(keyParam, iv);
            cipher.init(true, ivAndKey);
            byte[] ciptherBytes = cipherData(plainData); //48
            String cipherText = new String(Base64.encode(ciptherBytes), "UTF-8");//FileUtil.getStringFromByteArray(Base64.encode(ciptherBytes));


            System.out.println("encrypted >> "+cipherText);
            //Decryption

            byte[] cipherData = Base64.decode(cipherText);
            ivAndKey = new ParametersWithIV(keyParam, iv);
            cipher.init(false, ivAndKey);
            plainText = new String(cipherData(cipherData), "UTF-8");//FileUtil.getStringFromByteArray(cipherData(cipherData));

            System.out.println("decrypted >> "+plainText);


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private static byte[] cipherData(byte[] data)
            throws CryptoException
    {
        int minSize = cipher.getOutputSize(data.length);
        byte[] outBuf = new byte[minSize];
        int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
        int length2 = cipher.doFinal(outBuf, length1);
        int actualLength = length1 + length2;
        byte[] result = new byte[actualLength];
        System.arraycopy(outBuf, 0, result, 0, result.length);
        return result;
    }

.net中的代码:

 public static RijndaelManaged GetRijndaelManaged(String secretKey)
        {
            var keyBytes = new byte[16];
            var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);
            Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
            return new RijndaelManaged
            {
                Mode = CipherMode.CBC,
                Padding = PaddingMode.PKCS7,
                KeySize = 128,
                BlockSize = 128,
                Key = keyBytes,
                IV = keyBytes
            };
        }

        public static byte[] EncryptCBC(byte[] plainBytes, RijndaelManaged rijndaelManaged)
        {
            return rijndaelManaged.CreateEncryptor()
                .TransformFinalBlock(plainBytes, 0, plainBytes.Length);
        }

        public static byte[] DecryptCBC(byte[] encryptedData, RijndaelManaged rijndaelManaged)
        {
            return rijndaelManaged.CreateDecryptor()
                .TransformFinalBlock(encryptedData, 0, encryptedData.Length);
        }

        public static String EncryptCBCStr(String plainText, String key)
        {
            var plainBytes = Encoding.UTF8.GetBytes(plainText);
            return Convert.ToBase64String(EncryptCBC(plainBytes, GetRijndaelManaged(key)));
        }

         public static String DecryptCBCStr(String encryptedText, String key)
        {
            var encryptedBytes = Convert.FromBase64String(encryptedText);
            return Encoding.UTF8.GetString(DecryptCBC(encryptedBytes, GetRijndaelManaged(key)));
        }

// call

var PlainText = "Hello How are you !2#&*()% 123456@";
var EncryptionKey = "MAKV2SPBNI992122";
var cypherCBC = EncryptCBCStr(PlainText, EncryptionKey);
var decryptCBC = DecryptCBCStr(cypherCBC, EncryptionKey);

感谢adv。

推荐答案

此问题已得到解决...只是键/ IV字节的问题。在.net中,当在Java中我使用不同的IV时,键和IV相同。

This issue has been fixed...it is just key/IV bytes issue.as in .net there is same key and IV when in java i have used different IV.

Java代码中的更正:

correction in java code:

代替此

byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8");
byte[] iv = new byte[16];

使用此功能。

byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8");
byte[] iv =  "MAKV2SPBNI992122".getBytes("UTF-8");

这篇关于跨平台上的AES cbc填充加密/解密(.net c#和代号一个充气城堡)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆