本文整理汇总了Java中java.util.Base64.getEncoder方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.getEncoder方法的具体用法?Java Base64.getEncoder怎么用?Java Base64.getEncoder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Base64
的用法示例。
在下文中一共展示了Base64.getEncoder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.Base64; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = EncodeJsonConfig.class.getResourceAsStream("/master.json");
byte[] tmp = new byte[1024];
int length = is.read(tmp);
while (length > 0) {
baos.write(tmp, 0, length);
length = is.read(tmp);
}
is.close();
baos.close();
tmp = baos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
String encoded = encoder.encodeToString(tmp);
System.out.printf(" sso-demo.json: %s\n", encoded);
}
示例2: test
import java.util.Base64; //导入方法依赖的package包/类
@Test(timeout = 7500)
public void test() throws IOException {
//FIXME 无法执行。存在405错误。
Base64.Encoder encoder = Base64.getEncoder();
Connection con = Jsoup.connect("http://localhost/api/v0/auth/check_cert")
.data("uname", encoder.encodeToString("磷".getBytes(StandardCharsets.UTF_8)))
.data("email", encoder.encodeToString("[email protected]".getBytes(StandardCharsets.UTF_8)))
.data("zhihu", encoder.encodeToString("ice1000".getBytes(StandardCharsets.UTF_8)))
.data("github", encoder.encodeToString("Ray-Eldath".getBytes(StandardCharsets.UTF_8)))
.data("stackoverflow", encoder.encodeToString("VonC".getBytes(StandardCharsets.UTF_8)))
.data("brief", encoder.encodeToString("测试".getBytes(StandardCharsets.UTF_8)))
.data("introduce", encoder.encodeToString("测试lalala".getBytes(StandardCharsets.UTF_8)))
.timeout(8000);
Document doc = con.get();
System.out.println(doc.text());
}
示例3: encryptData_ECB
import java.util.Base64; //导入方法依赖的package包/类
public String encryptData_ECB(String plainText) {
try {
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;
byte[] keyBytes;
if (hexString) {
keyBytes = Util.hexStringToBytes(secretKey);
} else {
keyBytes = secretKey.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("GBK"));
Base64.Encoder encoder = Base64.getEncoder();
String cipherText = encoder.encodeToString(encrypted);
if (cipherText != null && cipherText.trim().length() > 0) {
Matcher m = PATTERN_1.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例4: encryptData_CBC
import java.util.Base64; //导入方法依赖的package包/类
public String encryptData_CBC(String plainText) {
try {
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;
byte[] keyBytes;
byte[] ivBytes;
if (hexString) {
keyBytes = Util.hexStringToBytes(secretKey);
ivBytes = Util.hexStringToBytes(iv);
} else {
keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes();
}
SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("UTF-8"));
Base64.Encoder encoder = Base64.getEncoder();
String cipherText = encoder.encodeToString(encrypted);
if (cipherText != null && cipherText.trim().length() > 0) {
Matcher m = PATTERN_1.matcher(cipherText);
cipherText = m.replaceAll("");
}
return cipherText;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例5: main
import java.util.Base64; //导入方法依赖的package包/类
public static void main(String[] args){
KeyPairGenerator kpg = null;
try{
kpg = KeyPairGenerator.getInstance("RSA");
} catch(NoSuchAlgorithmException ex){
log.error(ex, ex);
throw new RuntimeException(ex);
}
kpg.initialize(1024);
KeyPair keyPair = kpg.generateKeyPair();
Key privateKey = keyPair.getPrivate();
Key publicKey = keyPair.getPublic();
Base64.Encoder encoder = Base64.getEncoder();
String privateKeyBase64Str = encoder.encodeToString(privateKey.getEncoded());
log.info("Private key in Base64 format:\n" + privateKeyBase64Str);//it creates 1623 chars or 1620 chars
Base64.Decoder decoder = Base64.getDecoder();
byte[] privateKeyBytes = decoder.decode(privateKeyBase64Str);
log.info("The private Key is " + privateKeyBytes.length + " bytes long");
String privateKeyHex = String.format("%040x", new BigInteger(1, privateKeyBytes));
log.info("The private key in hexadecimal digits:\n" + privateKeyHex);
String publicKeyBase64Str = encoder.encodeToString(publicKey.getEncoded());
log.info("Public key in Base64 format:\n" + publicKeyBase64Str);//it creates 392 chars and again 392 chars for 2048 bits
//it creates 162 bytes for 1024 bits, an Ethereum address is 20 bytes (40 hexadecimal digits/characters long)
//324 hexadecimal characters, and we use the last 40 as the Ethereum address
byte[] publicKeyBytes = decoder.decode(publicKeyBase64Str);
log.info("The public Key is " + publicKeyBytes.length + " bytes long");
String publicKeyHex = String.format("%040x", new BigInteger(1, publicKeyBytes));
log.info("The public key in hexadecimal digits:\n" + publicKeyHex);
}
示例6: GLProof
import java.util.Base64; //导入方法依赖的package包/类
public GLProof(GLRSSSignatureOutput.GLRSSSignedPart signedPart) {
Base64.Encoder encoder = Base64.getEncoder();
this.randomValue = encoder.encodeToString(signedPart.getRandomValue());
this.accumulatorValue = encoder.encodeToString(signedPart.getAccumulatorValue());
this.gsProof = encoder.encodeToString(signedPart.getGsProof());
for (ByteArray byteArray : signedPart.getWitnesses()) {
witnesses.add(encoder.encodeToString(byteArray.getArray()));
}
}
示例7: testDecodeIgnoredAfterPadding
import java.util.Base64; //导入方法依赖的package包/类
private static void testDecodeIgnoredAfterPadding() throws Throwable {
for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
byte[][] src = new byte[][] {
"A".getBytes("ascii"),
"AB".getBytes("ascii"),
"ABC".getBytes("ascii"),
"ABCD".getBytes("ascii"),
"ABCDE".getBytes("ascii")
};
Base64.Encoder encM = Base64.getMimeEncoder();
Base64.Decoder decM = Base64.getMimeDecoder();
Base64.Encoder enc = Base64.getEncoder();
Base64.Decoder dec = Base64.getDecoder();
for (int i = 0; i < src.length; i++) {
// decode(byte[])
byte[] encoded = encM.encode(src[i]);
encoded = Arrays.copyOf(encoded, encoded.length + 1);
encoded[encoded.length - 1] = nonBase64;
checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
byte[] decoded = new byte[src[i].length];
decM.decode(encoded, decoded);
checkEqual(decoded, src[i], "Non-base64 char is not ignored");
try {
dec.decode(encoded);
throw new RuntimeException("No IAE for non-base64 char");
} catch (IllegalArgumentException iae) {}
}
}
}
示例8: encrypt
import java.util.Base64; //导入方法依赖的package包/类
public String encrypt(String plainText)throws Exception
{
byte [] plainTextByte = plainText.getBytes();
cpr.init(Cipher.ENCRYPT_MODE, sk);
byte [] encryptedByte = cpr.doFinal(plainTextByte);
Base64.Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
String encryptedKey = encoder.encodeToString(sk.getEncoded());
return (encryptedText+"\n"+encryptedKey+"\n");
}
示例9: encode
import java.util.Base64; //导入方法依赖的package包/类
public String encode(BufferedImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", baos);
byte[] bytes = baos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
String imgstr = encoder.encodeToString(bytes);
return imgstr;
}
示例10: testVerifyCodeBase64
import java.util.Base64; //导入方法依赖的package包/类
@Test
public void testVerifyCodeBase64() throws IOException {
VerificationCodeImage codeImage = new VerificationCodeImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(codeImage.getImage(), "jpeg", baos);
byte[] bytes = baos.toByteArray();
Base64.Encoder encoder = Base64.getEncoder();
String imgstr = encoder.encodeToString(bytes);
System.out.println(imgstr);
}
示例11: encrypt
import java.util.Base64; //导入方法依赖的package包/类
/**
* Encrypts given password with secret key
* @param plainText
* @return
* @throws Exception
*/
public String encrypt(String plainText) throws Exception
{
logger.debug("Encryption algo running");
Cipher cipher = Cipher.getInstance(ALGO + "/" + MODE + "/" + PADDING);
byte[] plainTextByte = plainText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedByte = cipher.doFinal(plainTextByte);
Base64.Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
logger.debug("Encryption algo executed");
return encryptedText;
}
示例12: ExerciseExporter
import java.util.Base64; //导入方法依赖的package包/类
public ExerciseExporter(String boltUri, AuthToken authToken) {
this.boltUri = boltUri;
this.authToken = authToken;
kryo = new Kryo();
encoder = Base64.getEncoder();
}
示例13: putFile
import java.util.Base64; //导入方法依赖的package包/类
public static void putFile(DbfsClient client,
File file,
String path,
boolean overwrite) throws FileNotFoundException, IOException, HttpException {
FileInputStream fileInputStreamReader = new FileInputStream(file);
try {
byte[] fileBytes = new byte[(int)file.length()];
fileInputStreamReader.read(fileBytes);
//System.out.println("Total Num of raw Bytes from File=" + file.length());
//encode bytes to Base64
Base64.Encoder encoder = Base64.getEncoder();
byte[] fileBase64Bytes = encoder.encode(fileBytes);
long bytesLeftToSend = fileBase64Bytes.length;
//System.out.println("Total Num of Base64 Bytes to Send="+bytesLeftToSend);
//TODO add call to .put() if less than MAX_BLOCK_SIZE
//open handler to DBFS
long dbfsHandle = client.create(path, overwrite);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileBase64Bytes);
while(bytesLeftToSend > 0) {
int numBytesToSend = (int) bytesLeftToSend > MAX_BLOCK_SIZE ? MAX_BLOCK_SIZE : (int) bytesLeftToSend;
byte[] bytesToSend = new byte[(int)numBytesToSend];
//read the next batch of bytes that fits into the byte array
int numBytesRead = byteArrayInputStream.read(bytesToSend);
//TODO add retry logic; inside or outside the client?
//add block to DBFS
//System.out.println("BEFORE addBlock; base64Bytes="+numBytesToSend);
client.addBlock(dbfsHandle, bytesToSend);
bytesLeftToSend = bytesLeftToSend - numBytesRead;
}
//close handler to DBFS
client.close(dbfsHandle);
} catch (Throwable e) {
throw e;
} finally {
fileInputStreamReader.close();
}
}
示例14: readXmlFile
import java.util.Base64; //导入方法依赖的package包/类
private String readXmlFile(String xmlFile) throws IOException, URISyntaxException {
Base64.Encoder encoder = Base64.getEncoder();
URL resource = getClass().getClassLoader().getResource(xmlFile);
return new String(encoder.encode(Files.readAllBytes(Paths.get(resource.toURI()))));
}
示例15: GSSignatureValue
import java.util.Base64; //导入方法依赖的package包/类
public GSSignatureValue(byte[] dsigValue, byte[] accumulatorValue) {
Base64.Encoder encoder = Base64.getEncoder();
this.dsigValue = encoder.encodeToString(dsigValue);
this.accumulatorValue = encoder.encodeToString(accumulatorValue);
}