【问题标题】:AES cbc padding encryption/decryption on cross platform (.net c# and codename one bouncy castle)跨平台上的 AES cbc 填充加密/解密(.net c# 和代号一充气城堡)
【发布时间】:2014-08-08 07:02:27
【问题描述】:

加密/解密不能在跨平台工作。

我已使用此链接在代号一中使用充气城堡 AES 密码加密/解密文本。

AES Encryption/Decryption with Bouncycastle Example in J2ME

虽然来自服务器端(.net),但我正在使用此链接来实现相同的方法。

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

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

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

代号一的代码:

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

感谢您的建议。

【问题讨论】:

  • 请提供您的代码。现在我们必须猜测。我的第一个猜测是:不。跨平台的加密和解密没有被破坏。它是你的代码。也许您没有填充源以加密到正确的块大小。也许你没有刷新输出,让阅读器 dycrypter 不完整的块。直到我们可以检查一些代码。
  • 嗨,Marvin,我在这里添加了代码。请检查。
  • 看不到任何明显的东西。使用的 KeySize 会有所不同吗?我不知道这两种实现方式。

标签: c# .net bouncycastle codenameone


【解决方案1】:

这个问题已经解决...它只是密钥/IV 字节问题。因为在 .net 中,当我在 java 中使用不同的 IV 时,有相同的密钥和 IV。

java代码中的更正:

而不是这个

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

使用这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 2015-04-26
    • 2014-09-19
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多