如何加密字符串,使其可以用作文件路径的一部分,然后在Android中解密?
在我的应用程序中,我需要加密一个字符串,该字符串可以包含字符a-z、a-z、0-9、下划线、句点和破折号,然后我们应该能够将该加密字符串用作文件路径的一部分/mnt/sdcard/encryptedstring/file/,然后我也应该能够解密该字符串。你能告诉我有什么方法可以做到这一点吗如何加密字符串,使其可以用作文件路径的一部分,然后在Android中解密?,android,encryption,Android,Encryption,在我的应用程序中,我需要加密一个字符串,该字符串可以包含字符a-z、a-z、0-9、下划线、句点和破折号,然后我们应该能够将该加密字符串用作文件路径的一部分/mnt/sdcard/encryptedstring/file/,然后我也应该能够解密该字符串。你能告诉我有什么方法可以做到这一点吗 谢谢。我正在为我的Android应用程序使用以下类,它工作起来很有魅力: import java.security.SecureRandom; import javax.crypto.Cipher; imp
谢谢。我正在为我的Android应用程序使用以下类,它工作起来很有魅力:
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Encryption {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
谢谢你的回复。我试试看。您能告诉我应该传递什么作为“种子”、“种子”和“值”,它们都是encrypt()方法的参数吗?“seed”是包含字符a-z、a-z、0-9、下划线、句点和破折号的字符串吗?另外,“savedSession”给了我一个错误。您能告诉我它是什么吗?Seed是一个字符串密钥,用于加密解密,就像passwd一样。只需将保存的状态替换为加密字符串,这就是我的示例。更新。现在检查一下谢谢。我使用了它,我的加密和解密是正确的。但是,你能告诉我是否有任何方法可以减少加密字符串的长度吗?如果我尝试加密一个很长的字符串,并将其作为encrypt()方法中的“Value”参数,则加密的字符串将变得很长,并且我无法在文件路径中使用如此大的加密字符串。如果希望加密的值变长,则加密总是会得到更长的字符串。使用的种子越短,得到的加密输出字符串越短。你知道的?
String seed="yourseed";
String EncryptedPass= Encryption.encrypt(seed,Value);
return Encryption.decrypt(seed,EncryptedPass);