// Encode data on your side using BASE64
byte[] bytesEncoded = Base64.encodeBase64(str.getBytes());
System.out.println("encoded value is " + new String(bytesEncoded));
// Decode data on other side, by processing encoded data
byte[] valueDecoded = Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded));
public class encodeBase64 {
public static void main(String[] args) {
String string = "Javacodegeeks";
// Get bytes from string
byte[] byteArray = Base64.encodeBase64(string.getBytes());
// Print the encoded byte array
System.out.println(Arrays.toString(byteArray));
// Print the encoded string
String encodedString = new String(byteArray);
System.out.println(string + " = " + encodedString);
}
}
public static void encodingDecodingApacheCommonsUTF() {
// ---Encode Data---
byte[] encoded = Base64.encodeBase64(sampleText
.getBytes(StandardCharsets.UTF_8));
String encodedText = new String(encoded, StandardCharsets.UTF_8);
// ---Decode Data---
byte[] decoded = Base64.decodeBase64(encodedText
.getBytes(StandardCharsets.UTF_8));
String decodedText = new String(decoded, StandardCharsets.UTF_8);
System.out.println("Base64 Encoding/Decoding - Apache (UTF_8)");
System.out.println("-----------------------------------------");
System.out.println("SampleText......: " + sampleText);
System.out.println("EncodedText.....: " + encodedText);
System.out.println("DecodedText.....: " + decodedText);
System.out.println();
}
}
public class Base64EncodingExample{
public static void main(String args[]) throws IOException {
String orig = "original String before base64 encoding in Java";
//encoding byte array into base 64
byte[] encoded = Base64.encodeBase64(orig.getBytes());
System.out.println("Original String: " + orig );
System.out.println("Base64 Encoded String : " + new String(encoded));
//decoding byte array into base64
byte[] decoded = Base64.decodeBase64(encoded);
System.out.println("Base 64 Decoded String : " + new String(decoded));
}
}
class Util
{
public static void main(String[] args)
{
String string = "Techie Delight";
// encode string using Base64 encoder
byte[] encodedBytes = Base64.encodeBase64(string.getBytes());
System.out.println("Encoded Data: " + new String(encodedBytes));
// decode the encoded data
String decodedString = new String(Base64.decodeBase64(encodedBytes));
System.out.println("Decoded Data: " + decodedString);
}
}
public static boolean checkConnection(String url,String auth){
boolean flag = false;
try {
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setConnectTimeout(5*1000);
if(auth!=null&&!"".equals(auth)){
String authorization = "Basic "+new String(Base64.encodeBase64(auth.getBytes()));
connection.setRequestProperty("Authorization", authorization);
}
connection.connect();
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
flag = true;
}
connection.disconnect();
}catch (Exception e) {
logger.error("--Server Connect Error !",e);
}
return flag;
}
protected String encrypt(final String message, final String secretKey) throws Exception { // NOSONAR
// SSO digest algorithm used for password. This
final MessageDigest md = MessageDigest.getInstance(getDigest());
final byte[] digestOfPassword = md.digest(secretKey.getBytes(StandardCharsets.UTF_8));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
// Cipher implementation.
final String algo = get("sso.crypt", DEFAULT_IMPL);
final SecretKey key = new SecretKeySpec(keyBytes, algo);
final Cipher cipher = Cipher.getInstance(algo);
cipher.init(Cipher.ENCRYPT_MODE, key);
final byte[] plainTextBytes = message.getBytes(StandardCharsets.UTF_8);
final byte[] buf = cipher.doFinal(plainTextBytes);
final byte[] base64Bytes = Base64.encodeBase64(buf);
return new String(base64Bytes, StandardCharsets.UTF_8);
}
private String computeAccessSignature(String timestamp, HttpMethod method, String urlTxt, ByteBuffer body)
throws GeneralSecurityException {
if (conn == null) {
throw new IllegalStateException("cannot generate exchange request post-disconnect()");
}
String prehash = timestamp + method.name() + urlTxt;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
mac.update(prehash.getBytes());
if (body != null) {
mac.update(body);
}
return new String(Base64.encodeBase64(mac.doFinal()));
}
public void setStartArguments(String[] startArguments) {
if (startArguments.length==0) {
this.startArguments = null;
return;
}
String[] startArgumentsEncoded = new String[startArguments.length];
String encodedArgument = null;
try {
for (int i = 0; i < startArguments.length; i++) {
encodedArgument = new String(Base64.encodeBase64(startArguments[i].getBytes("UTF8")));
startArgumentsEncoded[i] = encodedArgument;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.startArguments = startArgumentsEncoded;
}
public static String AESEncrypt(String keyStr, String plainText) throws Exception {
byte[] encrypt = null;
try {
Key key = generateKey(keyStr);
Cipher cipher = Cipher.getInstance(AES_TYPE);
cipher.init(Cipher.ENCRYPT_MODE, key);
encrypt = cipher.doFinal(plainText.getBytes());
return new String(Base64.encodeBase64(encrypt));
} catch (Exception e) {
LOGGER.error("aes encrypt failure : {}", e);
throw e;
}
}