Reputation: 93
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?
Upvotes: 4
Views: 4545
Reputation: 1025
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;
}
Upvotes: 8