使用Java中的参数加密URL的最佳方法是什么?
答案 0 :(得分:3)
执行此操作的唯一方法是使用SSL / TLS(https)。如果您使用普通的旧HTTP,则肯定会以明文形式发送URL。
答案 1 :(得分:2)
不幸的是,在java :-)中几乎没有什么是简单的,对于这个简单而通常的任务我找不到准备好的库,我最后写了这个(this was the source):
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEParameterSpec;
/**
* An easy to use class to encrypt and decrypt a string. Just call the simplest
* constructor and the needed methods.
*
*/
public class StringEncryptor {
private Cipher encryptCipher;
private Cipher decryptCipher;
private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
private sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
final private String charset = "UTF-8";
final private String defaultEncryptionPassword = "PAOSIDUFHQWER98234QWE378AHASDF93HASDF9238HAJSDF923";
final private byte[] defaultSalt = {
(byte) 0xa3, (byte) 0x21, (byte) 0x24, (byte) 0x2c,
(byte) 0xf2, (byte) 0xd2, (byte) 0x3e, (byte) 0x19 };
/**
* The simplest constructor which will use a default password and salt to
* encode the string.
*
* @throws SecurityException
*/
public StringEncryptor() throws SecurityException {
setupEncryptor(defaultEncryptionPassword, defaultSalt);
}
/**
* Dynamic constructor to give own key and salt to it which going to be used
* to encrypt and then decrypt the given string.
*
* @param encryptionPassword
* @param salt
*/
public StringEncryptor(String encryptionPassword, byte[] salt) {
setupEncryptor(encryptionPassword, salt);
}
public void init(char[] pass, byte[] salt, int iterations) throws SecurityException {
try {
PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
decryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE, k, ps);
} catch (Exception e) {
throw new SecurityException("Could not initialize CryptoLibrary: " + e.getMessage());
}
}
/**
*
* method to decrypt a string.
*
* @param str
* Description of the Parameter
*
* @return String the encrypted string.
*
* @exception SecurityException
* Description of the Exception
*/
public synchronized String encrypt(String str) throws SecurityException {
try {
byte[] utf8 = str.getBytes(charset);
byte[] enc = encryptCipher.doFinal(utf8);
return URLEncoder.encode(encoder.encode(enc),charset);
}
catch (Exception e)
{
throw new SecurityException("Could not encrypt: " + e.getMessage());
}
}
/**
*
* method to encrypting a string.
*
* @param str
* Description of the Parameter
*
* @return String the encrypted string.
*
* @exception SecurityException
* Description of the Exception
*/
public synchronized String decrypt(String str) throws SecurityException {
try {
byte[] dec = decoder.decodeBuffer(URLDecoder.decode(str,charset));
byte[] utf8 = decryptCipher.doFinal(dec);
return new String(utf8, charset);
} catch (Exception e) {
throw new SecurityException("Could not decrypt: " + e.getMessage());
}
}
private void setupEncryptor(String defaultEncryptionPassword, byte[] salt) {
java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
char[] pass = defaultEncryptionPassword.toCharArray();
int iterations = 3;
init(pass, salt, iterations);
}
}
答案 2 :(得分:1)
java security api(http://java.sun.com/javase/technologies/security/)+ url encoding
答案 3 :(得分:1)
这取决于您的威胁模型。例如,如果要保护Java应用程序从有权访问通信通道的攻击者发送到服务器的参数,则应考虑通过TLS / SSL(即您的情况下为HTTPS)与服务器进行通信。喜欢。如果您想保护有权访问运行Java客户端应用程序的计算机的攻击者的参数,那么您就会陷入更深的麻烦。
答案 4 :(得分:1)
如果您确实无法使用 SSL ,我建议使用预共享密钥方法并添加随机iv。
您可以使用任何体面的对称加密方法。 AES使用您正在通过带外通信的预共享密钥(电子邮件,电话等)。
然后生成一个随机初始化向量,并使用此iv和密钥加密您的字符串。最后,连接密文和iv,并将其作为参数发送。 iv可以明确地传达,没有任何风险。
答案 5 :(得分:0)
您确定不是指网址编码吗?
可通过java.net.URLEncoder.encode
进行编码。
答案 6 :(得分:0)
加密HTTP流量的标准方法是使用SSL。 但是,即使通过HTTPS,URL和其中的任何参数(即GET请求)也将以明文形式发送。您需要使用SSL并执行POST请求以正确加密数据。
正如注释中所指出的,无论您使用何种HTTP方法,只要您使用SSL连接,参数都将被加密。