【问题标题】:AES encryption using AESManaged in C#在 C# 中使用 AESManaged 进行 AES 加密
【发布时间】:2014-03-26 15:05:55
【问题描述】:

我在 windows phone 8 中做一个项目。我试图使用密钥和 iv 将字节数组 aes 加密为字节数组。还在 android 和 wp8 中传递相同的密钥和 iv。但没有得到相同的输出。我的代码有什么问题???请帮我解决这个问题。

我的 C# aes 加密代码是:

public static byte[] Encrypt (byte[] data, byte[] key) 
{
    byte[] encrypted;

    using (AesManaged AESM = new AesManaged())
    {
        byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        AESM.KeySize = 128;
        AESM.BlockSize = AESM.LegalBlockSizes[0].MaxSize;
        AESM.Key = key;
        AESM.IV = iv;

        ICryptoTransform encryptor = AESM.CreateEncryptor();

        encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
    }
    return encrypted;
}

我的android aes加密代码是:

public static byte[] encryptAESWithIV(byte[] key, byte[] clearText) throws 
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, 
UnsupportedEncodingException 
{
    SecretKeySpec skeySpec = new SecretKeySpec(key, CIPHER_AES);

    Cipher cipher = Cipher.getInstance(CIPHER_AES_MODE);

    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    try 
    {
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
    }
    catch (InvalidAlgorithmParameterException e)
    {
        e.printStackTrace();
    }

    byte[] encrypted = cipher.doFinal(clearText);
    byte[] output = new byte[encrypted.length + iv.length];

    System.arraycopy(iv, 0, output, 0, iv.length);
    System.arraycopy(encrypted, 0, output, iv.length, encrypted.length);

    return output;
}

【问题讨论】:

  • 问题可能是两个系统使用的分组密码模式(例如 ECB、CBC、CFB 等)可能不同。
  • 如何在 C# 中添加相同的密码解决方案????有什么方法可以在 C# 中获得相同的输出?
  • “密码解决方案”是什么意思?听起来您对基本加密货币了解不多。找一本书,了解什么是分组密码模式,然后回来。
  • 你的 CIPHER_AES_MODE 是什么?该字符串通常指定密码、块模式和填充。
  • 我对密码模式不太了解。你能告诉我如何在我的代码中添加它吗????谢谢

标签: c# java android encryption windows-phone-8


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多