2017-10-12 61 views
0

我是Android中的文本文件加密中的新功能。并尝试了很多文本加密的例子,但我很困惑如何申请。 我有来自json响应的5个字符串记录,我想将它们保存在文本文件(外部存储)和“加密格式”中。我试过了cipher_text_encoding的代码,但是它真的和很多类混淆了。 请建议我为文本加密好教程或给我提示如何编码。 在此先感谢。加密并保存在外部存储中作为文本文件的Json的字符串响应

回答

0

加密和解密使用AES私钥算法

产生AES的密钥:

public static byte[] generateAesSecretKey(){ 
    String SALT2 = "strong_salt_value"; 
    String username = "user_name"; 
    String password = "strong_password"; 
    byte[] key = (SALT2 + username + password).getBytes(); 
    SecretKey secretKeySpec = null; 

    try { 
     MessageDigest sha = MessageDigest.getInstance("SHA-1"); 
     key = sha.digest(key); 
     key = Arrays.copyOf(key, 16); 
     secretKeySpec = new SecretKeySpec(key, "AES"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return secretKeySpec.getEncoded(); 
} 

加密:

public static byte[] encodeFile(byte[] secretKey, byte[] fileData) { 
    SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES"); 
    byte[] encrypted = null; 
    try { 
     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 
     encrypted = cipher.doFinal(fileData); 

     // Now write your logic to save encrypted data to sdcard here 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     e.printStackTrace(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
    return encrypted; 
} 

解密:

public static byte[] decodeFile(byte[] key, byte[] fileData) { 
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); 
    byte[] decrypted = null; 
    try { 
     Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
     decrypted = cipher.doFinal(fileData); 
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException | BadPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch(Exception e){ 
     // for all other exception 
     e.printStackTrace(); 
    } 
    return decrypted; 
} 

希望以上方法对你有用!

+0

谢谢你soo多@zeeali,让我试试你的代码。 – nidhi

+0

欢迎您!你可以接受我的答案,如果它可以帮助你! – zeeali

0

因为每个初学者都会感到困惑,而不是自己动手,一切都学会利用代码重用或书面共享库。这将利用代码抽象,因为您只对JSON/Sting的加密和解密感兴趣。

对于一个完整的文件:

对于可重复使用的(爪哇/ Android的)库:

简体le用法:

String plainText = "Your String"; 
String encryptionKey = "Your Encryption Key"; 
String IV = "Your Initial Vector"; 

// To Encrypt 
String cipherText = AES.encrypt(plainText, encryptionKey, IV); 

// To Decrypt returned value same as plainText 
String originalText = AES.decrypt(cipherText, encryptionKey, IV); 

干杯。

+0

谢谢@ Al-Kathiri Khalid和yess它真的让我感到困惑,我会尝试你的链接。 – nidhi

+0

很高兴我可以帮助,如果你发现我的回答有帮助和相关,请接受它,以便对他人也有帮助。 –

+0

绝对,再次感谢。 – nidhi

相关问题