2008-09-23 69 views

回答

0

你確定你不是說URL編碼

編碼可通過java.net.URLEncoder.encode獲得。

3

做到這一點的唯一方法是使用SSL/TLS(HTTPS)。如果您使用普通的舊HTTP,則URL一定會以明文形式發送。

1

這取決於您的威脅模型。例如,如果要保護Java應用程序發送給服務器的參數與攻擊者可訪問通​​信通道的信息有關,則應考慮通過TLS/SSL與服務器通信(例如,HTTPS)和喜歡。如果您想保護訪問Java客戶端應用程序運行所在的計算機的攻擊者的參數,那麼您遇到了更大的麻煩。

1

如果你真的不能使用SSL,我會建議一個預先共享的密鑰方法,並添加一個隨機的iv。

你可以使用任何體面的對稱加密方法,如前。 AES使用您在帶外(電子郵件,電話等)進行通信的預共享密鑰。

然後你生成一個隨機初始化向量,並用這個iv和密鑰加密你的字符串。最後你連接你的密文和iv,並把它作爲你的參數。 iv可以在沒有任何風險的情況下以清晰的方式傳達。

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

}

0

加密HTTP流量的標準方式是使用SSL。 但是,即使通過HTTPS,URL和其中的任何參數(即GET請求)也將以明文形式發送。您需要使用SSL並執行POST請求才能正確加密數據。

作爲參數將被不管你用什麼HTTP方法加密的評論中指出的,只要你使用SSL連接。

+0

這是正確的嗎?當使用SSL/TLS時,我認爲URL被加密爲HTTP命令的參數(例如,GET,POST)。域以明文形式發送,但我認爲其餘部分在數據包的TLS部分內加密。 – 2010-02-14 19:53:16