首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AES 256算法支持

AES 256算法支持
EN

Stack Overflow用户
提问于 2017-10-28 18:26:24
回答 1查看 668关注 0票数 0

有没有安全可信的API可以让我在Android应用程序中使用AES-256算法?

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

EN

回答 1

Stack Overflow用户

发布于 2017-10-28 18:53:10

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

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

代码语言:javascript
运行
复制
    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);
        }
    }

下面是如何使用这个类:

代码语言:javascript
运行
复制
    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);

我还没有测试过它,但我认为代码仍然可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46988680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档