【问题标题】:Volley encryption on http request基于 http 请求的 Volley 加密
【发布时间】:2018-04-22 06:02:37
【问题描述】:

我正在使用Volley 将数据发送到我的服务器,并将所有必要的数据放入stringRequest 的标头和正文中。

发送请求后,我可以使用WireShark 捕获包,并且可以看到所有已发送的数据,从标头中的token 到正文中的所有字段(@ 987654327@等)。

如何在使用 Volley 的网络连接中使用加密?

有什么方法可以加密请求头和正文中的数据吗?

【问题讨论】:

  • 你使用的是http还是https?
  • http,在WireShark抓包中可以看到
  • 使用 https 并使用证书固定来进一步保护您的连接。
  • 我不确定我是否能够在服务器上使用 https(几乎可以肯定我不能),这就是为什么我试图将问题集中在 http 上,我无法控制顺便说一下服务器。
  • 那么您可以简单地在应用端使用任何加密算法并将其发送到服务器,然后服务器将使用私钥对其进行解密。您将使用它来加密数据。

标签: android http encryption android-volley


【解决方案1】:

你可以使用AES算法

import javax.crypto.Cipher;
     import javax.crypto.SecretKey;
     import javax.crypto.spec.SecretKeySpec;

    public class AESEncryptionDecryption {

    private static final byte[] keyValue =
            new byte[]{'c', 'o', 'd', 'i', 'n', 'g', 'a', 'f', 'f', 'a', 'i', 'r', 's', 'c', 'o', 'm'};


    public static String encrypt(String cleartext)
            throws Exception {
        byte[] rawKey = getRawKey();
        byte[] result = encrypt(rawKey, cleartext.getBytes());
        return toHex(result);
    }

    public static String decrypt(String encrypted)
            throws Exception {

        byte[] enc = toByte(encrypted);
        byte[] result = decrypt(enc);
        return new String(result);
    }

    private static byte[] getRawKey() throws Exception {
        SecretKey key = new SecretKeySpec(keyValue, "AES");
        byte[] raw = key.getEncoded();
        return raw;
    }

    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKey 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[] encrypted)
            throws Exception {
        SecretKey skeySpec = new SecretKeySpec(keyValue, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

    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));
}

加密使用此方法

String encrypted = "";
try {
    encrypted = AESEncryptionDecryption.encrypt(plain_text);
    Log.d(Constants.firebase_app, "encrypted:" + encrypted);
} catch (Exception e) {
    e.printStackTrace();
}

解密用这个方法

String decrypted = "";
try {
    decrypted = AESEncryptionDecryption.decrypt(encrypted);
    Log.d(Constants.firebase_app, "decrypted:" + decrypted);
} catch (Exception e) {
    e.printStackTrace();
}

【讨论】:

  • 感谢您的回答。看来Android默认为19以上的版本实现TLSv1.2,你知道这是否正确吗?就我而言,我正在为v24开发。
  • 我觉得对于加解密版本没关系。
  • 好的,根据下面的答案,他们说对于 Android 16-19 版本,TLS 未激活,您可能需要手动执行,这就是我询问 Android 版本的原因. stackoverflow.com/questions/31269425/…
  • 如果你的 url 是 https 那么不需要其他任何东西,但正如你所说你的 url 只是 http 那么你需要进行加密和解密。
  • 是的,这就是重点,为此,TLSv1.2 应该足够了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多