Domanda

There are many people here on SO suggesting different approaches to the problem that I am having, but non of them seem to work for me. I am currently testing a timestamp server and (among other things, I am sending the server a message) and I need to read that the message exists in the answer. So I am creating a request and preparing it:

MessageDigest digest = MessageDigest.getInstance("SHA1");
String s = "Trolololoooo";
DigestInputStream stream = new DigestInputStream(new ByteArrayInputStream(s.getBytes("UTF-8")), digest)
byte[] digest2 = stream.getMessageDigest().digest();

// timestamp stuff is all org.bouncycastle.tsp.*
TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();
timeStampRequestGenerator.setReqPolicy(String.valueOf(new ASN1ObjectIdentifier("1.3.6.1.4.1.13762.3")));
TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(TSPAlgorithms.SHA1, digest2, BigInteger.valueOf(666));
byte request[] = timeStampRequest.getEncoded();

... skipped the sending part, ok got the answer

InputStream in = con.getInputStream();
TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
TimeStampResponse response = new TimeStampResponse(resp);
response.validate(timeStampRequest);

Now, from the response, I can successfully read the byte stream like:

byte[] messageImprintDigest1 = response.getTimeStampToken().getTimeStampInfo().getMessageImprintDigest();

for( byte b : messageImprintDigest1) System.out.print(b);

Will output: -3857-93-189410775135085-65-17-1079624-112-81-4079

Anyway, I have been blindly trying all the suggestion that I have found, that would translate back to "Trolololoooo", but no success. Some (not limited to) things I have tried:

    String s1 = DigestUtils.sha1Hex(messageImprintDigest1);
    String s2 = new String(Hex.decodeHex(s1.toCharArray()), "UTF-8");
    // how could that help me..? but nothing to lose here.
    String s3 = Hex.encodeHexString(messageImprintDigest1);

    String convert = convert(s1);
//    String convert1 = convert(s2);
//    String convert2 = convert(s3);

    int len = s1.length();
    byte[] cStr = new byte[len/2];
    for(int i = 0; i < len; i+=2) {
        cStr[i/2] = (byte)Integer.parseInt(s1.substring(i, i+2), 16);
    }
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    ByteBuffer wrap = ByteBuffer.wrap(cStr);
    CharBuffer decode = decoder.decode(wrap);

    CharBuffer cb = decoder.decode( ByteBuffer.wrap( cStr ));
    String s4 = cb.toString();

...

public static String convert(String hex){
    ByteBuffer buff = ByteBuffer.allocate(hex.length()/2);
    for (int i = 0; i < hex.length(); i+=2) {
        buff.put((byte)Integer.parseInt(hex.substring(i, i+2), 16));
    }
    buff.rewind();
    Charset cs = Charset.forName("UTF-8");
    CharBuffer cb = cs.decode(buff);
    return  cb.toString();
}

Anyway, it is probably some obvious stuff I am missing, like that the messageImprintDigest1 doesn't look like a hex string at all (pardon me, I studied geology in university).. This stuff is all very new to me, so it is hard to argue with the compiler or some other stuff out there.

È stato utile?

Soluzione

You can't get the original text back from a message digest. It's a one-way process.

Altri suggerimenti

Have you tried this simple solution?

byte[] b = "Trolololoooo".getBytes();
String s = new String(b);       
System.out.println(s);

will output Trolololoooo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top