简体   繁体   English

android加密

[英]android encryption

I want to encrypt/decrypt some passwords in the SQLite database of my application. 我想对我的应用程序的SQLite数据库中的一些密码进行加密/解密。 To do that I have searched on the internet and I have found the AES algorithm. 为此,我在互联网上进行了搜索,发现了AES算法。 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: ERROR/ACTIVITY(782): java.security.InvalidKeyException: Key length not 128/192/256 bits. 01-27 14:50:51.698:错误/活动(782):java.security.InvalidKeyException:密钥长度不是128/192/256位。

I have seen some other cases here on stackoverflow but I want to give the key to the AES not to generate it... 我在stackoverflow上看到过其他情况,但我想将密钥提供给AES而不生成它...

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. 如果有其他加密方法可以使用,但又不使用其他jar或外部类,请允许我提供密钥。

Thank you very much! 非常感谢你!

The error message makes it perfectly clear: your encryption key must be of certain size: 128, 192 or 256 bits. 该错误消息清楚地表明:您的加密密钥必须具有一定的大小:128、192或256位。 And your key is 104 bits. 您的密钥是104位。 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. 请注意,由于您只想在密钥中使用可打印的字符,因此密钥的长度应为192或更长的位,这会导致字母(使用的字符集)使加密功能变弱。

Usual practice is such: 通常的做法是:

  1. Get password (in your case String key = "FMVWf8d_sm#fz"; ) 获取密码(在您的情况下, String key = "FMVWf8d_sm#fz";
  2. Generate using some hash function key with length 128, 192 or 256 使用一些长度为128、192或256的哈希函数键生成
  3. Put it into encryption algorithm 放入加密算法
  4. Have fun 玩得开心

So you are missing key generation stage. 因此,您缺少密钥生成阶段。 Do smth like: 做类似的事情:

       // Get the KeyGenerator

       KeyGenerator kgen = KeyGenerator.getInstance("AES");
       kgen.init(128); // 192 and 256 bits may not be available


       // Generate the secret key specs.
       SecretKey skey = kgen.generateKey();
       byte[] raw = skey.getEncoded();

       SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");


       // Instantiate the cipher

       Cipher cipher = Cipher.getInstance("AES");

       cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

       byte[] encrypted =
         cipher.doFinal((args.length == 0 ?
          "This is just an example" : args[0]).getBytes());
       System.out.println("encrypted string: " + asHex(encrypted));

       cipher.init(Cipher.DECRYPT_MODE, skeySpec);
       byte[] original =
         cipher.doFinal(encrypted);
       String originalString = new String(original);
       System.out.println("Original string: " +
         originalString + " " + asHex(original));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM