SMS加密在解密时提供异常

时间:2012-05-05 11:34:23

标签: android sms encryption

我正在开发一个处理短信的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)

1 个答案:

答案 0 :(得分:0)

您可能希望发布完整堆栈跟踪。目前,您的代码存在三个问题:

  • 你的密钥很弱:你应该从一些ASCII字符构造一个密钥。这严重限制了关键空间,并且很容易暴力破解关键
  • 不要使用非标准填充。使用Cipher.getInstance("AES/CBC/PKCS5Padding")来获得正确的填充
  • 不使用固定的IV,为每个加密操作生成一个随机的

此外,使用getBytes()可能会产生奇怪的结果,因为编码未定义(使用平台默认值)。它可能在Android上是一致的,但使用getBytes("UTF-8")来确保从同一个字符串中获取相同的字节。

相关问题