4

I'm decoding messages with javax.crypto.Cipher and as an output I get byte[]. What is the fastest way to check if my key is correct and byte[] is valid string?

1
  • Getting a vaid UTF-8 string doesn't guarantee that the key is valid. What are you trying to achieve? Commented Oct 20, 2017 at 20:12

1 Answer 1

8

Try This :-

public boolean checkUTF8(byte[] barr){

        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        ByteBuffer buf = ByteBuffer.wrap(barr);

        try {
            decoder.decode(buf);

        }
        catch(CharacterCodingException e){
            return false;
        }

        return true;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.