0

I have a requirement wherein I have to generate a URL where one of the parameter is signature and signature has to be generated using below requirement in a Java Application:

The other 4 URL parameter values should be hashed (in the order specified below) using MD5 and sign using the private certificate. (The signature will be DER-encoded PKCS #1 block as defined in RSA Laboratory's Public Key Cryptography Standards Note #1.) The resulting digest should be converted to ASCII character set using base64 and then encoded to comply with HTTP URL character set limitations.

Order                   Parameter
1                       [queryparameter1]
2..                     [queryparameter …] *
3                       Expiration

The final url should look something like

https://<ServerName>:<Port>/imageRet/pod?ID=123456789&build=XHB&date=201102151326&expiration=20110218155523&signature=H767dhghjKJ#23mxi

I have never worked on Cryptography before and hence don't know how to start. Can somebody help how can this be achived.

Neel
  • 199
  • 3
  • 18
  • 1
    The bit about "I have never worked on Cryptography before" is a little like a podiatrist saying "I have never never worked on heart surgery before" but I'll just ask in SurgeryOverflow, how hard can it be to get it right. IOW, get a cryptographic domain expert. – zaph Jun 28 '16 at 13:24
  • @zaph What is wrong in learning something new. I don't have Cryptography expert, that's why I am asking the experts here. – Neel Jun 28 '16 at 13:31
  • Good point if learning is what it is about and there are many books and much information available. But if you are just interested in a quick fix there is a problem, it is very easy even for those experienced in crypto to make a mistake. A crypto mistake is not just a bug that something does not work correctly or a crash where a re-boot is all that is necessary, a successful attack is permanent and puts all of the users at risk. Users trust the developers and that trust is the developer's responsibility. – zaph Jun 28 '16 at 14:25
  • "I don't have Cryptography expert", there are cryptographic experts who have years of experience behind them for hire, I have hired them to vett my work. Yes, they cost money just like you and I do. – zaph Jun 28 '16 at 14:27
  • MD5 should not be used for signatures anymore. Your URL definitely allows to much variance for MD5 to be an option; there seem to be few mitigating circumstances; the only issue may be the character encoding required for the parameters. 1. learn crypto 2. implement crypto; in that order. – Maarten Bodewes Jun 28 '16 at 20:41

1 Answers1

0

This will be the signature code

Signature sig = Signature.getInstance("MD5withRSA");
sig.initSign(privateKey);
sig.update(canonicalize(params));
byte signature[] = sig.sign();
String signatureB64UrlEncoded = Base64.getUrlEncoder().encodeToString(signature);

Where canonicalize(params) means converting the String parameters of the url to byte[] the way your service specified. You have not given details. This step is not trivial at all because equivalent urls may generate different signatures.

For example

 q=hello%20world   --> Qazz_tVB-guYai5oW0Eef6BbVP ...
 q=hello world     --> JJWDEPMQDmffcsjR0dP3vnrkFT ...

An example implementation, but surely not valid...

//Convert params[] to byte[] converting each String to byte with default charset and concatenating results
public byte[] canonicalize(String params[] ) throws IOException{
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    for (String param: params){
        out.write(param.getBytes());
    }
    return out.toByteArray();
}

Take a look at Amazon AWS to see an example of how canonicalize a URL

If you finally decide to use a more secure algorithm, simply replace MD5withRSA with for example SHA256withRSA

pedrofb
  • 37,271
  • 5
  • 94
  • 142