2008-09-23 23 views

Respuesta

0

¿Estás seguro de que no significa URL codifica?

La codificación está disponible a través de java.net.URLEncoder.encode.

3

La única manera de hacer esto es utilizar SSL/TLS (HTTPS). Si usa HTTP antiguo simple, la URL definitivamente se enviará sin cifrar.

1

Depende de su modelo de amenaza. Por ejemplo, si desea proteger los parámetros enviados por su aplicación Java a su servidor de un atacante que tiene acceso al canal de comunicación, debe considerar comunicarse con el servidor a través de TLS/SSL (es decir, HTTPS en su caso) y el gustos. Si desea proteger los parámetros de un atacante que tiene acceso a la máquina donde se ejecuta su aplicación de cliente Java, entonces tiene más problemas.

1

Si realmente no puede usar SSL, le sugiero un enfoque de clave precompartida y agregar un iv aleatorio.

Puede utilizar cualquier método de cifrado simétrico decente, por ej. AES usando una clave precompartida a la que se está comunicando fuera de banda (correo electrónico, teléfono, etc.).

Luego genera un vector de inicialización aleatorio y encripta su cadena con este iv y la clave. Finalmente concatenas tu texto de cifrado y el iv y lo envías como tu parámetro. El iv puede ser comunicado sin ningún riesgo.

2

Desafortunadamente casi de mención es simple en :-) java, para esta tarea simple y habitual yo no era capaz de encontrar una biblioteca preparada, terminé escribiendo esto (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

La forma estándar de encriptar el tráfico HTTP es usar SSL. Sin embargo, incluso a través de HTTPS, la URL y cualquier parámetro en ella (es decir, una solicitud GET) se enviarán sin cifrar. Debería usar SSL y hacer una solicitud POST para encriptar correctamente sus datos.

Como se señala en los comentarios, los parámetros se cifrarán independientemente del método HTTP que utilice, siempre que utilice una conexión SSL.

+0

¿Es esto correcto? Al usar SSL/TLS, pensé que la URL estaba encriptada como un parámetro del comando HTTP (por ejemplo, GET, POST). El dominio se envía sin cifrar, pero creo que el resto está encriptado dentro de la porción TLS del paquete. –

+1

No es cierto. La solicitud de DNS se envía en forma clara. Luego se configura un túnel SSL. A continuación, la solicitud HTTP, incluido el URI, se envía a través de ese túnel. – Quentin