我正在开发一个处理 SMS 的 android 应用程序。
我已经完成了应用程序,但最后一件事仍未解决:加密。
我怎样才能解决这个问题来加密传入的短信?
public class enc {
private String iv = "fedcba9876543210";
private IvParameterSpec ivspec;
private SecretKeySpec keyspec;
private Cipher cipher;
private String SecretKey = "0123456789abcdef";
public enc()
{
ivspec = new IvParameterSpec(iv.getBytes());
keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String encrypt(String text) throws Exception
{
if(text == null || text.length() == 0)
throw new Exception("Empty string");
byte[] encrypted = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(padString(text).getBytes());
} catch (Exception e)
{
throw new Exception("[encrypt] " + e.getMessage());
}
String s = new String(encrypted);
Log.v("TAG", s);
return s;
}
public String decrypt(String code) throws Exception
{
if(code == null || code.length() == 0)
throw new Exception("Empty string");
byte[] decrypted = null;
try {
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
decrypted = cipher.doFinal(hexToBytes(code));
} catch (Exception e)
{
throw new Exception("[decrypt] " + e.getMessage());
}
Log.v("TAG", decrypted.toString());
String s = new String(decrypted);
Log.v("TAG", s);
return s;
}
public static String bytesToHex(byte[] data)
{
if (data==null)
{
return null;
}
int len = data.length;
String str = "";
for (int i=0; i<len; i++) {
if ((data[i]&0xFF)<16)
str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
else
str = str + java.lang.Integer.toHexString(data[i]&0xFF);
}
return str;
}
public static byte[] hexToBytes(String str) {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
}
return buffer;
}
}
private static String padString(String source)
{
char paddingChar = ' ';
int size = 16;
int x = source.length() % size;
int padLength = size - x;
for (int i = 0; i < padLength; i++)
{
source += paddingChar;
}
return source;
}
}
我试过这个,但它在解密时给出了一个异常,它无法解析“A字符”。
我所说的字符是指显示无法解析的字母 a、b、c。
有什么帮助吗?
04-24 13:56:17.191: V/(2031): Message Recieved
04-24 13:56:22.811: V/TAG(2031): [B@44ebb318
04-24 13:56:22.811: V/Encrypted(2031): [B@44ebb318
04-24 13:56:22.931: W/System.err(2031): java.lang.Exception: [decrypt] unable to parse '[B' as integer
04-24 13:56:22.941: W/System.err(2031): at sms.app.enc.decrypt(enc.java:86)
04-24 13:56:22.951: W/System.err(2031): at sms.app.SMSReceiver.onReceive(SMSReceiver.java:86)
04-24 13:56:22.961: W/System.err(2031): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
04-24 13:56:22.961: W/System.err(2031): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
04-24 13:56:22.970: W/System.err(2031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
04-24 13:56:22.981: W/System.err(2031): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 13:56:22.981: W/System.err(2031): at android.os.Looper.loop(Looper.java:123)
04-24 13:56:22.991: W/System.err(2031): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 13:56:23.001: W/System.err(2031): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 13:56:23.001: W/System.err(2031): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 13:56:23.011: W/System.err(2031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 13:56:23.021: W/System.err(2031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 13:56:23.021: W/System.err(2031): at dalvik.system.NativeStart.main(Native Method)