如何在KeyStore中存储secretKey并检索它

时间:2014-06-15 15:32:59

标签: java android security encryption keystore

我目前正在开发一个Android应用程序,但由于存储和检索到密钥库中的SecretKey,我预计会出现一些问题

这是我的代码: 在这里,我生成SecretKey,然后将其保存到KeyStore,并使用它来加密我的数据

try {
                        KeyStore keyStore=null;
                        keyStore= KeyStore.getInstance(KeyStore.getDefaultType());
                        char[] passwordKS="network".toCharArray();

                             SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
                             sr.setSeed("any data used as random seed".getBytes());
                             KeyGenerator kg = KeyGenerator.getInstance("AES");
                             kg.init(128, sr);
                             key= kg.generateKey();
                             keyToSave=key.getEncoded();
                             sks = new SecretKeySpec(keyToSave, "AES");

                             try
                             {
                             keyStore.load(null,null);
                             keyStore.setKeyEntry("aliasKey",key,passwordKS, null);

                             }
                             catch(Exception ex)
                             {

                             }
                             FileOutputStream ksout=openFileOutput("keyStoreName", Context.MODE_PRIVATE);
                             keyStore.store(ksout, passwordKS);
                             ksout.close();
                         }


                     } catch (Exception e) {
                     }
                byte[] userLongENC = null;
                byte[] userLatENC=null;
                try {
                    Cipher c = Cipher.getInstance("AES");
                    c.init(Cipher.ENCRYPT_MODE,sks ); 
                    userLatENC = c.doFinal(userLat.getBytes());
                    userLongENC = c.doFinal(userLong.getBytes());
                } catch (Exception e) {
                 }

在另一项活动中 我尝试从密钥库中取回密钥并使用它来解密我的数据 不幸的是我得到了这个异常:javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整

SecretKeySpec sks = null; // Or, equivalently SecretKey sk = null;
        SecretKey sk =null;
        try {
            KeyStore keyStore= KeyStore.getInstance(KeyStore.getDefaultType());
            char[] passwordKS="network".toCharArray();
            FileInputStream fis =null;
            try
             {
                 fis = openFileInput("keyStoreName");
             }catch (Exception ex)
            {
                }
            keyStore.load(fis,passwordKS);
            //sk=(SecretKey) keyStore.getKey("aliasKey", passwordKS);
             sk=(SecretKey) keyStore.getKey("aliasKey", passwordKS);
            sks=new SecretKeySpec((keyStore.getKey("aliasKey", passwordKS)).getEncoded(), "AES");
           } catch (Exception e) {
           }
        byte[] latDEC=null;
        byte[] longDEC=null;
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, sks);
            latDEC = c.doFinal(lat.getBytes());
            longDEC = c.doFinal(longit.getBytes());
        } catch (Exception e) {
           }

0 个答案:

没有答案