Android 基于http请求的截击加密

Android 基于http请求的截击加密,android,http,encryption,android-volley,Android,Http,Encryption,Android Volley,我正在使用Volley向我的服务器发送数据,并将所有必要的数据放入stringRequest的标题和正文中 发送请求后,我可以使用WireShark捕获包,并且我能够看到所有已发送的数据,从标头中的标记到正文中的所有字段(userId,等等) 如何使用Volley在网络连接中使用加密 有没有办法加密请求头和请求体中的数据 您可以使用AES算法 import javax.crypto.Cipher; import javax.crypto.SecretKey; import j

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

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

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

有没有办法加密请求头和请求体中的数据


您可以使用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();
}

您使用的是http还是https?http,可以在WireShark captureuse https中看到。为了进一步保护您的连接,请使用证书固定。我不确定我是否能够在服务器上使用https(几乎可以肯定我不能),这就是为什么我试图将问题集中在http上,顺便说一下,我对服务器没有控制权。然后你可以简单地在应用程序端使用任何加密算法并将其发送到服务器,然后服务器将使用私钥对其进行解密。您将使用它来加密数据。谢谢您的回答。Android似乎默认为19以上版本实现了
TLSv1.2
,你知道这是否正确吗?在我的例子中,我是为v24开发的。我认为加密和解密版本无关紧要。好的,根据下面的答案,他们告诉我,对于Android版本16-19,TLS没有激活,你可能需要手动操作,这就是我问Android版本的原因。如果你的url是https,那么不需要其他任何东西,但正如你所说的,你的url只是http,那么你需要进行加密和解密。是的,这就是所有这些的重点,对于这一点,TLSv1.2应该足够了。