android加密 [英] android encryption

查看:232
本文介绍了android加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密/解密我的应用程序的SQLite数据库中的一些密码。为了做到这一点,我在互联网上搜索,我找到了AES算法。
我有这个代码:

I want to encrypt/decrypt some passwords in the SQLite database of my application. To do that I have searched on the internet and I have found the AES algorithm. I have this code:

public String encript(String dataToEncrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    // I'm using AES encription

    if(!dataToEncrypt.equals("")){
        String key = "FMVWf8d_sm#fz";

        Cipher c = Cipher.getInstance("AES");
        SecretKeySpec k;
        try {
            k = new SecretKeySpec(key.getBytes(), "AES");
            c.init(Cipher.ENCRYPT_MODE, k);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return new String(c.doFinal(Base64.decode(dataToEncrypt)));
    }
    return "";
}

public String decript(String encryptedData)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    if(!encryptedData.equals("")){
        String key = "FMVWf8d_sm#fz";

        Cipher c = Cipher.getInstance("AES");
        SecretKeySpec k = new SecretKeySpec(Base64.decode(key), "AES");
        c.init(Cipher.DECRYPT_MODE, k);
        return new String(c.doFinal(Base64.decode(encryptedData)));
    }
    return "";
}

运行后,我在加密方法中收到此错误:

After running this I get this error on encrypt method:


01-27 14:50:51.698:错误/活动(782):
java.security.InvalidKeyException:密钥长度不是128/192 / 256位。

01-27 14:50:51.698: ERROR/ACTIVITY(782): java.security.InvalidKeyException: Key length not 128/192/256 bits.

我在stackoverflow上看到一些其他情况,但是我想给AES的密钥不生成它。 ..

I have seen some other cases here on stackoverflow but I want to give the key to the AES not to generate it...

有人可以帮我吗?如果还有其他的加密方法可以使用但不使用另外的jar或外部类,并让我给出关键。

Can somebody help me with this? If there is other encryption method to use but without using another jars or external classes and to let me give the key.

非常感谢!

推荐答案

错误信息完全清楚:您的加密密钥必须具有一定的大小:128,192或256位。你的密钥是104位。请注意,如果您只想使用密钥中的可打印字符,则密钥的长度应该是192或更长的位,因为您的字母表(您使用的字符集)使加密更弱。

The error message makes it perfectly clear: your encryption key must be of certain size: 128, 192 or 256 bits. And your key is 104 bits. Note, that as you want to use only printable characters in your key, the length of the key should be 192 or longer bits, cause your alphabet (set of characters that you use) makes encryption weaker.

这篇关于android加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆