微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android KeyStore 规则?

如何解决Android KeyStore 规则?

我对您可以在 KeyStore android 中存储什么类型的字符串有疑问。我目前存储包含拉丁字母、数字和特殊字符(如句点、破折号、括号等)的常规字符串。一旦我决定加密到服务器的 url,我的应用程序就开始崩溃。它在字符串中包含“http://smth/smth/smth”这样的字符。当我删除 url 的加密时,一切又开始工作了。

我的问题是,您可以在 KeyStore android 中存储什么样的字符串,是否有具体的规则?或者可能是我们可以在 KeyStore 中存储的最大字符串数是多少?我目前存储 4 个字符串。或者可能是算法起作用(我使用的是 AES/CBC/nopadding)或者其他我不确定。

异常我得到: 空对象引用上的 javax.crypto.SecretKey java.security.KeyStore$SecretKeyEntry.getSecretKey()'

        if(keyGenerator == null)
        {
            try
            {
                keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore");
                mKeyGenerator.init(
                        new KeyGenParameterSpec.Builder("MyKeyAlias",KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                                .build()
                );
                mKeyGenerator.generateKey();
            }
            catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e)
            {
                e.printstacktrace();
            }

        }

    private static SecretKey getKey()
    throws KeyStoreException,CertificateException,NoSuchAlgorithmException,IOException,UnrecoverableEntryException
    {
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        keyStore.load(null);

        KeyStore.SecretKeyEntry  secretKeyEntry = (KeyStore.SecretKeyEntry) keyStore.getEntry("MyKeyAlias",null);

        return  secretKeyEntry.getSecretKey();
    }

    private static Pair<byte[],byte[]> encryptData(String data)
    throws NoSuchPaddingException,UnrecoverableEntryException,KeyStoreException,InvalidKeyException,BadPaddingException,IllegalBlockSizeException
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");

        byte [] iv = null;
        byte [] encryptedBytes = null;
        StringBuilder temp = new StringBuilder(data);

        while (temp.toString().getBytes().length % 16 != 0)
        {
            temp.append('\u0020');
        }

        cipher.init(Cipher.ENCRYPT_MODE,getKey());

        iv = cipher.getIV();
        encryptedBytes = cipher.doFinal(temp.toString().getBytes(Charsets.UTF_8));

        return Pair.create(iv,encryptedBytes);
    }

    public static String decryptData(byte[] ivBytes,byte[] data)
    throws NoSuchPaddingException,InvalidAlgorithmParameterException,IllegalBlockSizeException
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/nopadding");
        IvParameterSpec spec = new IvParameterSpec(ivBytes);

        cipher.init(Cipher.DECRYPT_MODE,getKey(),spec);

        return new String(cipher.doFinal(data)).trim();
    }


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。