String originalInput = "test input";
String encodedString = new String(Base64.encodeBase64(originalInput.getBytes()));
String decodedString = new String(Base64.decodeBase64(encodedString.getBytes()));
public class decodeBase64 {
public static void main(String[] args) {
String string = "SmF2YWNvZGVnZWVrcw==";
// Get bytes from string
byte[] byteArray = Base64.decodeBase64(string.getBytes());
// Print the decoded array
System.out.println(Arrays.toString(byteArray));
// Print the decoded string
String decodedString = new String(byteArray);
System.out.println(string + " = " + decodedString);
}
}
public class Codec {
public static void main(String[] args) {
try {
String clearText = "Hello world";
String encodedText;
// Base64
encodedText = new String(Base64.encodeBase64(clearText.getBytes()));
System.out.println("Encoded: " + encodedText);
System.out.println("Decoded:"
+ new String(Base64.decodeBase64(encodedText.getBytes())));
//
// output :
// Encoded: SGVsbG8gd29ybGQ=
// Decoded:Hello world
//
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void encodingDecodingApacheCommons() {
// ---Encode Data---
byte[] encoded = Base64.encodeBase64(sampleText.getBytes());
String encodedText = new String(encoded);
// ---Decode Data---
byte[] decoded = Base64.decodeBase64(encodedText.getBytes());
String decodedText = new String(decoded);
System.out.println("Base64 Encoding/Decoding - Apache Commons");
System.out.println("-----------------------------------------");
System.out.println("SampleText......: " + sampleText);
System.out.println("EncodedText.....: " + encodedText);
System.out.println("DecodedText.....: " + decodedText);
System.out.println();
}
public class Base64Decode {
public static void main(String[] args) {
String hello = "SGVsbG8gV29ybGQ=";
// Decode a previously encoded string using decodeBase64 method and
// passing the byte[] of the encoded string.
byte[] decoded = Base64.decodeBase64(hello.getBytes());
// Print the decoded array
System.out.println(Arrays.toString(decoded));
// Convert the decoded byte[] back to the original string and print
// the result.
String decodedString = new String(decoded);
System.out.println(hello + " = " + decodedString);
}
}
public ConsulRouterResp lookupRouterMessage(String serviceName, long lastConsulIndex) {
QueryParams queryParams = new QueryParams(ConsulConstants.CONSUL_BLOCK_TIME_SECONDS, lastConsulIndex);
Response<GetValue> orgResponse = client.getKVValue(serviceName, queryParams);
GetValue getValue = orgResponse.getValue();
if (getValue != null && StringUtils.isNoneBlank(getValue.getValue())) {
String router = new String(Base64.decodeBase64(getValue.getValue()));
ConsulRouterResp response = ConsulRouterResp.newResponse()//
.withValue(router)//
.withConsulIndex(orgResponse.getConsulIndex())//
.withConsulLastContact(orgResponse.getConsulLastContact())//
.withConsulKnowLeader(orgResponse.isConsulKnownLeader())//
.build();
return response;
}
return null;
}
public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
byte[] signatureBytes = null;
String signature = jwt.getSignature();
String urlDecoded = null;
switch (encodeType) {
case Base16:
urlDecoded = URLDecoder.decode(signature, "UTF-8");
signatureBytes = Hex.decodeHex(urlDecoded);
break;
case Base32:
Base32 base32 = new Base32();
urlDecoded = URLDecoder.decode(signature, "UTF-8");
signatureBytes = base32.decode(urlDecoded);
break;
case Base64:
signatureBytes = Base64.decodeBase64(signature);
break;
}
if (signatureBytes.length > 0) {
throw new SignatureVerificationException(this);
}
}
private String decrypt(String text) throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(
Base64.decodeBase64(ENCRYPTION_KEY), "AES");
byte[] result;
try {
byte[] decoded = Base64.decodeBase64(text.getBytes());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
result = cipher.doFinal(decoded);
} catch (InvalidKeyException | NoSuchAlgorithmException
| NoSuchPaddingException | IllegalBlockSizeException
| BadPaddingException e) {
throw new GeneralSecurityException(
"unable to decrypt old encryption");
}
return new String(result);
}
public static byte[] decrypt(byte[] encrypted)
throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(
Base64.decodeBase64(ENCRYPTION_KEY), "AES");
byte[] decoded = Base64.decodeBase64(encrypted);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
return cipher.doFinal(decoded);
}
public static String bucketFromBase64URN(String base64URN) {
String modelURN = "";
if (base64URN.toLowerCase().startsWith( "urn:" ) )
{
base64URN = base64URN.substring( 4 ).trim();
}
modelURN = new String(Base64.decodeBase64( base64URN.getBytes() ) );
return bucketFromModelURN( modelURN );
}