【问题标题】:Android AES Encryption from C# to Java从 C# 到 Java 的 Android AES 加密
【发布时间】:2015-05-08 07:17:28
【问题描述】:

我正在将我的 C# 加密代码转换为 Android。

我面临无法像 C# 一样加密文本的问题。 下面我复制粘贴两个代码。

两者都是关于使用它的工作代码,你可以使用任何密码和任何纯文本。你会发现两者都有不同的输出。

C# 代码

System.security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.MD5CryptoServiceProvider Hash_AES = new System.Security.Cryptography.MD5CryptoServiceProvider();

final MessageDigest Hash_AES = MessageDigest.getInstance("MD5");

        String encrypted = "";
        try {
            byte[] hash = new byte[32];
            byte[] temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass));
            final byte[] temp = Hash_AES.digest(pass.getBytes("US-ASCII"));

            Array.Copy(temp, 0, hash, 0, 16);
            Array.Copy(temp, 0, hash, 15, 16);
            AES.Key = hash;
            AES.Mode = System.Security.Cryptography.CipherMode.ECB;
            System.Security.Cryptography.ICryptoTransform DESEncrypter = AES.CreateEncryptor();
            byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(input);
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length));
        } catch (Exception ex) {

        }
        return encrypted;

这是我的 Android java 代码。

安卓 JAVA 代码

private static String TRANSFORMATION = "AES/ECB/NoPadding";
private static String ALGORITHM = "AES";
private static String DIGEST = "MD5";
byte[] encryptedData;

public RijndaelCrypt(String password,String plainText) {

    try {

        //Encode digest
        MessageDigest digest;           
        digest = MessageDigest.getInstance(DIGEST);            
        _password = new SecretKeySpec(digest.digest(password.getBytes()), ALGORITHM);

        //Initialize objects
        _cipher = Cipher.getInstance(TRANSFORMATION);

       _cipher.init(Cipher.ENCRYPT_MODE, _password);
        encryptedData = _cipher.doFinal(text);

    } catch (InvalidKeyException e) {
        Log.e(TAG, "Invalid key  (invalid encoding, wrong length, uninitialized, etc).", e);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Invalid or inappropriate algorithm parameters for " + ALGORITHM, e);
        return null;
    } catch (IllegalBlockSizeException e) {
        Log.e(TAG, "The length of data provided to a block cipher is incorrect", e);
        return null;
    } catch (BadPaddingException e) {
        Log.e(TAG, "The input data but the data is not padded properly.", e);
        return null;
    }               

    return Base64.encodeToString(encryptedData,Base64.DEFAULT);
}

我应该在 pass 中使用“US-ASCII”还是需要它?

【问题讨论】:

  • 我认为您需要在 Java 代码的 getBytes() 方法中传递“US-ASCII”编码方案。通过 Charset 方案始终是一个好习惯。

标签: java c# android encryption aes


【解决方案1】:
  • 使用相同的操作模式:ECB 或 CBC
  • 使用相同的字符集:最好坚持“UTF-8”
  • 使用相同的密钥:在 C# 代码中,您将 128 位密钥加倍为 256 位

当使用带有随机 IV 的 CBC 时,预计相同明文的密文会有所不同。解密是决定你是否成功的操作。

请注意,ECB 在语义上并不安全。将 CBC 与随机 IV 一起使用。 IV 不必保密,因此您只需将其添加到密文中并在解密之前将其切掉即可。

最好使用 GCM 或 EAX 之类的身份验证模式,或者如果它没有提供 encrypt-then-MAC 方案。自己很难正确实现它,所以坚持使用一些为你做这件事的库,比如 RNCryptor。

【讨论】:

  • @Artojom 感谢您的建议。我删除了随机 IV。但我发现密码给了我错误的值。知道代码出了什么问题吗?
  • 您应该使用相同的密钥。我扩展了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 2021-02-06
  • 2017-04-15
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
相关资源
最近更新 更多