當前位置: 首頁>>代碼示例>>Java>>正文


Java Base64.getEncoder方法代碼示例

本文整理匯總了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);
}
 
開發者ID:obsidian-toaster-quickstarts,項目名稱:redhat-sso,代碼行數:17,代碼來源:EncodeJsonConfig.java

示例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());
}
 
開發者ID:ProgramLeague,項目名稱:strictfp-back-end,代碼行數:17,代碼來源:CheckCert.java

示例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;
    }
}
 
開發者ID:nuls-io,項目名稱:nuls,代碼行數:30,代碼來源:SM4Utils.java

示例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;
    }
}
 
開發者ID:nuls-io,項目名稱:nuls,代碼行數:32,代碼來源:SM4Utils.java

示例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);
}
 
開發者ID:VictorGil,項目名稱:shared-ledger-simulator,代碼行數:34,代碼來源:KeyGenerationTester.java

示例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()));
    }
}
 
開發者ID:woefe,項目名稱:xmlrss,代碼行數:10,代碼來源:GLProof.java

示例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) {}
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:31,代碼來源:TestBase64.java

示例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");
}
 
開發者ID:Tejas07PSK,項目名稱:maven_EWorld_OpenShift,代碼行數:11,代碼來源:AES.java

示例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;
}
 
開發者ID:FlyingHe,項目名稱:UtilsMaven,代碼行數:9,代碼來源:TestImgCompress.java

示例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);
}
 
開發者ID:FlyingHe,項目名稱:UtilsMaven,代碼行數:11,代碼來源:TestImgCompress.java

示例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;
}
 
開發者ID:Vedang18,項目名稱:ProBOT,代碼行數:20,代碼來源:PasswordCoder.java

示例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();
}
 
開發者ID:fbiville,項目名稱:hands-on-neo4j-devoxx-fr-2017,代碼行數:7,代碼來源:ExerciseExporter.java

示例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();
    }

}
 
開發者ID:level11data,項目名稱:databricks-client-java,代碼行數:49,代碼來源:DbfsHelper.java

示例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()))));
}
 
開發者ID:alphagov,項目名稱:verify-hub,代碼行數:6,代碼來源:ProtectiveMonitoringLogFormatterTest.java

示例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);
}
 
開發者ID:woefe,項目名稱:xmlrss,代碼行數:6,代碼來源:GSSignatureValue.java


注:本文中的java.util.Base64.getEncoder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。