【问题标题】:AES 256 algorithm supportAES 256 算法支持
【发布时间】:2017-10-28 10:26:24
【问题描述】:

是否有安全且受信任的 API 允许我在我的 Android 应用程序中使用 AES-256 算法?

我使用 JDK 9 编写了一个简单的 API,使用 javax.cryptojava.security 类,提供对 AES-256 的支持。 因为 Android 仍然坚持使用 JDK 7(JDK 8 不支持 AES-256),所以我无法使用这些 API。那么,如何使用基于 AES-256 算法的加密呢?

【问题讨论】:

  • 请注意,AES-128 与 AES-256 一样安全,即两者都是安全的。
  • Android 一直有 AES-256。只需使用 Cipher.getInstance("AES/<mode>/PKCS5PADDING") 并使用 256 位密钥。

标签: java android algorithm encryption cryptography


【解决方案1】:

您可以通过以下链接使用github上dealforest给出的代码: AES256 encrypt android

如果你想更容易得到它,我把代码放在下面;)



    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.spec.AlgorithmParameterSpec;

    public class AES256Cipher {

        public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) 
                throws java.io.UnsupportedEncodingException, 
                    NoSuchAlgorithmException,
                    NoSuchPaddingException,
                    InvalidKeyException,
                    InvalidAlgorithmParameterException,
                    IllegalBlockSizeException,
                    BadPaddingException {

            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = null;
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
            return cipher.doFinal(textBytes);
        }

        public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) 
                throws java.io.UnsupportedEncodingException, 
                NoSuchAlgorithmException,
                NoSuchPaddingException,
                InvalidKeyException,
                InvalidAlgorithmParameterException,
                IllegalBlockSizeException,
                BadPaddingException {

            AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
            return cipher.doFinal(textBytes);
        }
    }

下面是如何使用这个类:



    String key = "e8ffc7e56311679f12b6fc91aa77a5eb";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    String plainText;
    byte[] cipherData;
    String base64Text;

    //############## Request(crypt) ##############
    plainText  = "crypt text!!";
    cipherData = AES256Cipher.encrypt(ivBytes, keyBytes, plainText.getBytes("UTF-8"));
    base64Text = Base64.encodeToString(cipherData, Base64.DEFAULT);
    Log.d("encrypt", base64Text);

    //############## Response(decrypt) ##############
    base64Text = "72XrlydqnUzVrDfDE7ncnQ==";
    cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
    plainText = new String(cipherData, "UTF-8");
    Log.d("dcrypt", plainText);

我还没有测试过,但我认为代码仍然有效。

【讨论】:

  • 我注意到,如果我将 jdk9 代码复制到我的 android 应用程序的新类中,它就可以工作。问题是如果我用 jdk8 编译。似乎 jdk9 代码可以与 jdk7 一起移植,但不能与 jdk8 一起移植。
  • 哦,好吧,我不知道。在 jdk9 和 7 上运行但不在 jdk8 上运行的代码很奇怪。
  • 是的,这是一种奇怪的行为。
  • 警告:虽然这段代码显示 AES-256 有效,但我不会复制密钥用法、IV 用法或异常处理。对于非常安全的代码,应该考虑使用 GCM 而不是 CBC。 IE。使用 AES-256,但不要使用上述代码
猜你喜欢
  • 2012-03-25
  • 2017-03-05
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2014-12-18
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多