0

Hello I want to encrypt a unique random value using a key in Java. I will send this unique random value to each webservices to make system secure so nobody can hit my web services url on rest client.

Please guide me a way to achieve this.

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

1 Answers1

1

There are two solutions for that problem:

In summary, the first; you encrypt the data over sockets (by reverse engineering, like brute-force, you can break the password used to encrypt). The second; use a SSL (Security Socket Layer). I had used the first solution, then I can detail for you how to implement. Here you are:

1- There are some API's to help you doing that. I used jasypt a time ago, and I recommend. But there are others too; like bouncy castle.

Usually, they are simple to implement. In jasypt, you can solve this issue like that, just run to test:

public class SecurityUtil {

private static String passEncrypt;

/*
 * Get the message encrypted
 * @param String string to encrypt
 * @return encrypted message
 */
public static String Encryptor(String message){
    SecurityUtil.testEncryptPassSet();
    StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    encryptor.setPassword(SecurityUtil.passEncrypt);        
    String encryptedText = encryptor.encrypt(message);
    return encryptedText;
}
/*
 * Get the decrypt message
 * @param encrypted message
 * @return String decrypted message
 * 
 */
public static String Decryptor(String message) {
    SecurityUtil.testEncryptPassSet();
    StandardPBEStringEncryptor decryptor = new StandardPBEStringEncryptor();
    decryptor.setPassword(SecurityUtil.passEncrypt);  
    String decryptedText = decryptor.decrypt(message);
    return decryptedText;
}


/*
 * set the encryption password
*/
public static void setPassEncrypt(String passEncrypt) {
    SecurityUtil.passEncrypt = passEncrypt;
}

public static void testEncryptPassSet() {
    if (SecurityUtil.passEncrypt == null){
        System.out.println("Must set the password after");
    }
}

public static void main (String[] args){
    SecurityUtil.setPassEncrypt("Test"); //here you key
    String encrypted;
    System.out.println("Encrypted: "+(encrypted = SecurityUtil.Encryptor("This is a test message")));

    System.out.println("Decryp: "+SecurityUtil.Decryptor(encrypted));
}

}

OUTPUT:

Encrypted: eESU3c2IzRSl2VvHs4Otyh+Q3aBisiP6XPfyKpbXMdQ=

Decryp: This is a test message

2- You can study how implement SSL over sockets here. Also, here are some examples. And here we have a question of similar subject in StackOverflow.

Community
  • 1
  • 1
G Bisconcini
  • 764
  • 2
  • 6
  • 25
  • Thanks, Nice answer but there again if I do same thing in android app then anyone can get this "Test" as key after doing some reverse engineering. I want this should be much secure so nobody can crack it – N Sharma Jan 30 '15 at 11:32
  • You are right. Doing reverse engineering is possible to break the security. It's difficult; but can be done. Another solution is use SSL over socket. I can tell you, you can break an SSL connection too, but is more difficult then first solution. I will update the answer for you. – G Bisconcini Jan 30 '15 at 12:52
  • @Williams done, I updated the answer, Hope you can have a clue on how to solve your problem. In the final of my answer, I wrote some links with examples on how to implement SSL over sockets. SSL over sockets I never done before, but seems to be quite easy too, just see the examples. – G Bisconcini Jan 30 '15 at 13:11