2013-04-04 212 views
0

我已經掌握了一些用於加密用戶名和密碼的代碼,並且在文件中寫入,但是當我想要恢復這些值時,我沒有得到原始值,我在讀取時遇到了異常爲了這個目的file.My代碼如下如何在android中加密用戶名和密碼

public class Storage 
{ 
    private final byte[] salt = { 
      (byte)0xf5, (byte)0x33, (byte)0x01, (byte)0x2a, 
      (byte)0xb2, (byte)0xcc, (byte)0xe4, (byte)0x7f 
      }; 
     private int iterationCount = 100; 

     public void encryptAndWriteToFile(String host,String user,String pw,boolean flag) 
     { 
      String usr=host+","+user+","+pw+","+Boolean.toString(flag); 
      Cipher cipher = null; 
      try 
      { 
       PBEKeySpec keySpec =new PBEKeySpec(usr.toCharArray()); 
       SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBEWithMD5AndDES"); 
       SecretKey secretKey = keyFactory.generateSecret(keySpec); 
       PBEParameterSpec parameterSpec =new PBEParameterSpec(salt, iterationCount); 
       cipher = Cipher.getInstance("PBEWithMD5AndDES"); 
       cipher.init(Cipher.ENCRYPT_MODE, secretKey,parameterSpec); 
      }catch (NoSuchAlgorithmException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidKeySpecException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidKeyException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (NoSuchPaddingException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidAlgorithmParameterException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      byte[] outputArray = null; 
      try 
      { 
       outputArray = usr.getBytes("ISO-8859-1"); 
      }catch (UnsupportedEncodingException ex) 
      { 
         Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      File sdCard=Environment.getExternalStorageDirectory();//+"/userData.txt"; 
      String user_credentails=sdCard.getName()+"/userData.txt"; 
      File file = new File(user_credentails); 
      Log.d(user_credentails, "is input file "); 
      FileOutputStream fileOutputStream = null; 
      try 
      { 
       fileOutputStream = new FileOutputStream(file); 
      }catch (IOException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      CipherOutputStream out =new CipherOutputStream(fileOutputStream, cipher); 
      try 
      { 
       out.write(outputArray); 
       out.flush(); 
       out.close(); 
      }catch (IOException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      Vector fileBytes = new Vector(); 
      try 
      { 
       FileInputStream in = new FileInputStream(file); 
       byte contents; 
       while (in.available() > 0) { 
       contents = (byte)in.read(); 
       fileBytes.add(new Byte(contents)); 
       } 
       in.close(); 
      }catch (IOException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      byte[] encryptedText = new byte[ fileBytes.size() ]; 
      for (int i = 0; i < fileBytes.size(); i++) 
      { 
       encryptedText[ i ] = 
       ((Byte) fileBytes.elementAt(i)).byteValue(); 
      } 
     } 
     public String[] readFromFileAndDecrypt(boolean state) 
     { 
      Vector fileBytes = new Vector(); 
      String pw = "123"; 
      String fileName = "security.txt"; 
      String usr="10.0.2.2"+","+"haider"+","+pw+","+Boolean.toString(state); 
      Cipher cipher = null; 
      try 
      { 
       PBEKeySpec keySpec =new PBEKeySpec(usr.toCharArray()); 
       SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBEWithMD5AndDES"); 
       SecretKey secretKey = keyFactory.generateSecret(keySpec); 
       PBEParameterSpec parameterSpec =new PBEParameterSpec(salt, iterationCount); 
       cipher = Cipher.getInstance("PBEWithMD5AndDES"); 

       cipher.init(Cipher.DECRYPT_MODE, secretKey,parameterSpec); 
      }catch (NoSuchAlgorithmException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidKeySpecException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidKeyException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (NoSuchPaddingException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      }catch (InvalidAlgorithmParameterException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      try 
      { 
       File sdCard=Environment.getExternalStorageDirectory(); 
       String user_credentails=sdCard.getAbsolutePath()+"/userData.txt"; 
       Log.d(user_credentails, " is file name and path"); 
       File file = new File(user_credentails); 
       FileInputStream fileInputStream =new FileInputStream(file); 
       CipherInputStream in =new CipherInputStream(fileInputStream, cipher); 
       byte contents = (byte) in.read(); 
       Log.d(Byte.toString(contents)," is line");//.out.println("\n"+contents+"\n"); 
       while (contents != -1) 
       { 
        fileBytes.add(new Byte(contents)); 
        contents = (byte) in.read(); 
       } 
       in.close(); 
      }catch (IOException exception) 
      { 
       exception.printStackTrace(); 
       System.exit(1); 
      } 
      byte[] decryptedText = new byte[ fileBytes.size() ]; 
      for(int i = 0; i < fileBytes.size(); i++) 
      { 
       decryptedText[ i ] =((Byte)fileBytes.elementAt(i)).byteValue(); 
      } 
      Log.d(new String(decryptedText)," is data"); 
      String uUser=new String(decryptedText); 
      String[] delims=uUser.split(","); 
      return delims; 
     } 

} 

回答

1

給出一個合理的方法是簡單地生成一個真正隨機的AES密鑰當一個應用程序第一次啓動

See this