public static byte [ ] toBytes ( char [ ] chars ) { final CharBuffer charBuffer = CharBuffer . wrap ( chars ) ; final ByteBuffer byteBuffer = Charset . forName ( "UTF-8" ) . encode ( charBuffer ) ; final byte [ ] bytes = Arrays . copyOfRange ( byteBuffer . array ( ) , byteBuffer . position ( ) , byteBuffer . limit ( ) ) ; Arrays . fill ( charBuffer . array ( ) , ' ' ) ; // clear sensitive data Arrays . fill ( byteBuffer . array ( ) , ( byte ) 0 ) ; // clear sensitive data return bytes ; }
Converts a char array to a byte array without the use of Strings .