本文整理匯總了Java中java.util.Base64.Encoder.encodeToString方法的典型用法代碼示例。如果您正苦於以下問題:Java Encoder.encodeToString方法的具體用法?Java Encoder.encodeToString怎麽用?Java Encoder.encodeToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.Base64.Encoder
的用法示例。
在下文中一共展示了Encoder.encodeToString方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: rfc4648Base64Encode
import java.util.Base64.Encoder; //導入方法依賴的package包/類
static String rfc4648Base64Encode(String arg) throws UnsupportedEncodingException {
Encoder encoder = Base64.getMimeEncoder(0, new byte[0]);
String res = encoder.encodeToString(arg.getBytes(TEXT_ENCODING));
res = res.replace("/", "_");
res = res.replace("+", "-");
res = res.replace("=", "");
return res;
}
示例2: aesEncryptString
import java.util.Base64.Encoder; //導入方法依賴的package包/類
public static String aesEncryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
byte[] contentBytes = content.getBytes(charset);
byte[] keyBytes = key.getBytes(charset);
byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(encryptedBytes);
}
示例3: createContent
import java.util.Base64.Encoder; //導入方法依賴的package包/類
public static Content createContent(IExtensionHelpers helpers, byte[] httpResponse) {
String[] response = new String(httpResponse).split("\r\n\r\n");
// size
int size = 0;
if (response.length > 1) {
size = response[1].getBytes().length;
}
// mimeType
String mimeType = getMimeType(httpResponse);
String base64String = "";
if (response.length > 1) {
IResponseInfo iResInfo = helpers.analyzeResponse(httpResponse);
int bodyPos = iResInfo.getBodyOffset();
byte[] bodybyte = Arrays.copyOfRange(httpResponse,bodyPos,httpResponse.length);
Encoder encoder = Base64.getMimeEncoder();
encoder = Base64.getMimeEncoder();
base64String = encoder.encodeToString(bodybyte);
}
Content retContent = new Content.Builder().size(size).compression(0).encoding("base64").mimeType(mimeType)
.text(base64String).build();
return retContent;
}
示例4: aesEncryptString
import java.util.Base64.Encoder; //導入方法依賴的package包/類
public static String aesEncryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
byte[] contentBytes = content.getBytes(charset);
byte[] keyBytes = key.getBytes(charset);
byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(encryptedBytes);
}
示例5: generateApiKey
import java.util.Base64.Encoder; //導入方法依賴的package包/類
public static String generateApiKey(){
Encoder e = Base64.getEncoder();
ByteBuffer bb = ByteBuffer.wrap(new byte[16*2]);
for(int i=0;i<2; i++){
UUID u = UUID.randomUUID();
bb.putLong(u.getMostSignificantBits());
bb.putLong(u.getLeastSignificantBits());
}
return e.encodeToString(bb.array());
}
示例6: encryptIntegers
import java.util.Base64.Encoder; //導入方法依賴的package包/類
/**
* Encrypt an array of integers to a String.
*
* @param integers
* @param context
* @return
* @throws ServletException
*/
public static String encryptIntegers(int integers[], String password) throws ServletException {
/* Generate salt. */
SecureRandom rand = new SecureRandom();
byte salt[] = new byte[8];
rand.nextBytes(salt);
byte[] iv;
byte[] ciphertext;
try {
/* Derive the key, given password and salt. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
/* Encrypt the SampleSetID. */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
iv = params.getParameterSpec(IvParameterSpec.class).getIV();
ByteBuffer buff = ByteBuffer.allocate(integers.length * 4);
for (int i = 0; i < integers.length; ++i) {
buff.putInt(integers[i]);
}
ciphertext = cipher.doFinal(buff.array());
} catch (Exception ex) {
throw new ServletException(ex);
}
/* Store the encrypted SampleSetID in a cookie */
Encoder encoder = Base64.getEncoder();
String encryptedStr = encoder.encodeToString(ciphertext) + "|" + encoder.encodeToString(iv) + "|" + encoder.encodeToString(salt);
return encryptedStr;
}
示例7: encryptInteger
import java.util.Base64.Encoder; //導入方法依賴的package包/類
/**
* Encrypt an integer to a String.
*
* @param integer
* @param context
* @return
* @throws ServletException
*/
private static String encryptInteger(Integer integer, ServletContext context) throws ServletException {
/* Get password. */
String password = context.getInitParameter("SampleSetIDEncryptionPassword");
/* Generate salt. */
SecureRandom rand = new SecureRandom();
byte salt[] = new byte[8];
rand.nextBytes(salt);
byte[] iv;
byte[] ciphertext;
try {
/* Derive the key, given password and salt. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
/* Encrypt the SampleSetID. */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
iv = params.getParameterSpec(IvParameterSpec.class).getIV();
ciphertext = cipher.doFinal(ByteBuffer.allocate(4).putInt(integer).array());
} catch (Exception ex) {
throw new ServletException(ex);
}
/* Store the encrypted SampleSetID in a cookie */
Encoder encoder = Base64.getEncoder();
String encryptedStr = encoder.encodeToString(ciphertext) + "|" + encoder.encodeToString(iv) + "|" + encoder.encodeToString(salt);
return encryptedStr;
}
示例8: encode
import java.util.Base64.Encoder; //導入方法依賴的package包/類
/**
* Returns an encoded BASE64 string of the value.
*
* @param salt
* the salt to use for encoding
* @param value
* value to encode
*
* @return encoded BASE64 string of the value or null if no value given
*
* @throws CipherException
*/
public static String encode(String salt, String value)
throws CipherException
{
// verify value
if (null == value || value.isEmpty())
{
return null;
}
// create cipher object
SecretKeySpec keySpec = createAESCipher(salt);
Cipher cipher;
try
{
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] enc = cipher.doFinal(value.getBytes());
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(enc);
}
catch (Exception e)
{
throw new CipherException("Error during encryption.", e);
}
}
示例9: encodePropertyValue
import java.util.Base64.Encoder; //導入方法依賴的package包/類
private String encodePropertyValue(final String value) throws IllegalArgumentException, EncryptionException {
// simply obfuscate the values from clear text.
final Encoder encoder = Base64.getUrlEncoder();
final String encoded = encoder.encodeToString(value.getBytes());
return encoded;
}
示例10: toBase64
import java.util.Base64.Encoder; //導入方法依賴的package包/類
public static String toBase64(byte[] bytes){
Encoder b64 = Base64.getEncoder();
return b64.encodeToString(bytes);
}
示例11: of
import java.util.Base64.Encoder; //導入方法依賴的package包/類
static Crypted of(String info, byte[] content, byte[] iv) {
Encoder encoder = getEncoder();
String contentBase64 = encoder.encodeToString(content);
String ivBase64 = encoder.encodeToString(iv);
return new AutoValue_ContentCryptographer_Crypted(info, contentBase64, ivBase64);
}
示例12: encodeBase64
import java.util.Base64.Encoder; //導入方法依賴的package包/類
/**
* 對byte做base64編碼
*/
public static String encodeBase64(byte[] data) {
Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(data);
}