本文整理匯總了Java中javax.xml.bind.DatatypeConverter.printBase64Binary方法的典型用法代碼示例。如果您正苦於以下問題:Java DatatypeConverter.printBase64Binary方法的具體用法?Java DatatypeConverter.printBase64Binary怎麽用?Java DatatypeConverter.printBase64Binary使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.bind.DatatypeConverter
的用法示例。
在下文中一共展示了DatatypeConverter.printBase64Binary方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: EncryptAES
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
public static String EncryptAES(String input, SecretKey key) throws CryptoException {
// encode input
input = DatatypeConverter.printBase64Binary(input.getBytes());
byte[] ciphertext = null;
try {
ciphertext = AES.encrypt(input.getBytes(), key);
} catch (Exception e) {
throw new CryptoException(e);
}
// encode encrypted input
//return DatatypeConverter.printBase64Binary(ciphertext.getBytes());
return DatatypeConverter.printHexBinary(ciphertext);
}
示例2: encodeToString
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
/**
* Base64-encode the given byte array to a String.
*
* @param src
* the original byte array (may be {@code null})
* @return the encoded byte array as a UTF-8 String (or {@code null} if the
* input was {@code null})
*/
public static String encodeToString(byte[] src) {
if (src == null) {
return null;
}
if (src.length == 0) {
return "";
}
if (delegate != null) {
// Full encoder available
return new String(delegate.encode(src), DEFAULT_CHARSET);
} else {
// JAXB fallback for String case
return DatatypeConverter.printBase64Binary(src);
}
}
示例3: encrypt
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
/**
* Encrypt plaintext.
* @param str Plaintext to encrypt
* @return Cipher text
*/
public String encrypt(final String str) {
if (str == null) {
throw new NullPointerException("Argument cannot be null");
}
try {
final SecureRandom random = new SecureRandom();
final byte[] salt = new byte[16];
random.nextBytes(salt);
final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
final KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 128);
final SecretKey tmp = factory.generateSecret(spec);
final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
final AlgorithmParameters params = cipher.getParameters();
final byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
final byte[] encryptedText = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
// concatenate salt + iv + ciphertext
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(salt);
outputStream.write(iv);
outputStream.write(encryptedText);
// properly encode the complete cipher text
return DatatypeConverter.printBase64Binary(outputStream.toByteArray());
} catch (final Exception exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
}
示例4: addBasicAuth
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private void addBasicAuth(Packet context, Map<String, List<String>> reqHeaders) {
String user = (String) context.invocationProperties.get(BindingProvider.USERNAME_PROPERTY);
if (user != null) {
String pw = (String) context.invocationProperties.get(BindingProvider.PASSWORD_PROPERTY);
if (pw != null) {
StringBuilder buf = new StringBuilder(user);
buf.append(":");
buf.append(pw);
String creds = DatatypeConverter.printBase64Binary(buf.toString().getBytes());
reqHeaders.put("Authorization", Collections.singletonList("Basic "+creds));
}
}
}
示例5: send
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
public void send(EmailModel model) throws VaVedemEmailException {
Email from = new Email(model.getFrom());
String subject = model.getSubject();
Email to = new Email(model.getTo());
Content content = new Content("text/plain", model.getContent());
Mail mail = new Mail(from, subject, to, content);
Map<String, String> fileNames = listFormulare(formulareDir);
final String formular = model.getFormular();
final String fileName = fileNames.get(formular);
if (StringUtils.isBlank(fileName) && StringUtils.isNotBlank(formular)) {
logger.error("[VaVedem] : formularul : " + formular + " nu a fost gasit pe disk. ");
throw new VaVedemEmailException("Eroare la trimiterea emailului.");
}
String base64 = null;
try {
base64 = DatatypeConverter.printBase64Binary(Files.readAllBytes(
Paths.get(fileName)));
} catch (IOException e) {
logger.error("[VaVedem] Eroare la conversia in base64 a fisierului: " + fileName);
throw new VaVedemEmailException("Eroare la trimiterea emailului.");
}
Attachments ats = new Attachments();
ats.setContent(base64);
ats.setFilename(formular);
mail.addAttachments(ats);
SendGrid sg = new SendGrid(sendGridApiKey);
System.out.println("LOG [VaVedem] : send grid api-key: " + sendGridApiKey);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
} catch (IOException ex) {
ex.printStackTrace();
throw new VaVedemEmailException("Eroare la trimiterea emailului.");
}
}
示例6: sendItemStackToClient
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
protected static void sendItemStackToClient(EntityPlayerMP player, MalmoMessageType message, ItemStack is)
{
ByteBuf buf = Unpooled.buffer();
ByteBufUtils.writeItemStack(buf, is);
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(0, bytes);
String data = DatatypeConverter.printBase64Binary(bytes);
MalmoMod.MalmoMessage msg = new MalmoMod.MalmoMessage(message, data);
MalmoMod.network.sendTo(msg, player);
}
示例7: sign
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
public static String sign(byte[] plainText, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature privateSignature = Signature.getInstance(signVerifyAlgorithm);
privateSignature.initSign(privateKey);
privateSignature.update(plainText);
return DatatypeConverter.printBase64Binary(privateSignature.sign());
}
示例8: encodeImg2String
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
/**
* Encodes the given image to a base64 coded string.
*
* @param image image to encode
* @param type type of the image: jpeg, bmp, png, gif etc.
* @return encoded string
*/
public static String encodeImg2String(BufferedImage image, String type) {
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
imageString = DatatypeConverter.printBase64Binary(imageBytes);
bos.close();
} catch (IOException ex) {
LOGGER.error(ex.getMessage(), ex);
}
return imageString;
}
示例9: deflateAndBase64
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private String deflateAndBase64(final String data) {
try {
final Deflater deflater = new Deflater();
deflater.setInput(data.getBytes(HttpConstants.UTF8_ENCODING));
deflater.finish();
final byte[] buffer = new byte[data.length()];
final int resultSize = deflater.deflate(buffer);
final byte[] output = new byte[resultSize];
System.arraycopy(buffer, 0, output, 0, resultSize);
return DatatypeConverter.printBase64Binary(output);
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException("Cannot find encoding:" + HttpConstants.UTF8_ENCODING, e);
}
}
示例10: generateChallenge
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private String generateChallenge() {
byte[] challengeBytes = CryptoUtil.secureRandom(16);
currentChallenge = DatatypeConverter.printBase64Binary(challengeBytes);
return currentChallenge;
}
示例11: toBase64
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private static String toBase64(byte[] array) {
return DatatypeConverter.printBase64Binary(array);
}
示例12: getBasicAuthHeader
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private static String getBasicAuthHeader(String username, String password) throws UnsupportedEncodingException {
return "Basic " + DatatypeConverter.printBase64Binary((username + ":" + password).getBytes("UTF-8"));
}
示例13: getKeyAsString
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
private String getKeyAsString(ByteArray key)
throws UnsupportedEncodingException {
return DatatypeConverter.printBase64Binary(key.get());
}
示例14: getSecret
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
/**
* Creates the secret key from a static string
*
* @return byte[] conversion of secretKey
*/
private static byte[] getSecret() {
String base64Key = DatatypeConverter.printBase64Binary(secretKey.getBytes());
return DatatypeConverter.parseBase64Binary(base64Key);
}
示例15: toMessage
import javax.xml.bind.DatatypeConverter; //導入方法依賴的package包/類
String toMessage() {
if (error != null)
return "e=" + error;
else
return "v=" + DatatypeConverter.printBase64Binary(serverSignature);
}