Search in sources :

Example 21 with Decoder

use of java.util.Base64.Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64Golden method test1.

private static void test1() throws Exception {
    byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
    Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
    byte[] encoded = encoder.encode(src);
    Decoder decoder = Base64.getMimeDecoder();
    byte[] decoded = decoder.decode(encoded);
    if (!Objects.deepEquals(src, decoded)) {
        throw new RuntimeException();
    }
}
Also used : Encoder(java.util.Base64.Encoder) BASE64Encoder(sun.misc.BASE64Encoder) Decoder(java.util.Base64.Decoder) BASE64Decoder(sun.misc.BASE64Decoder)

Example 22 with Decoder

use of java.util.Base64.Decoder in project jdk8u_jdk by JetBrains.

the class TestBase64 method testDecodeUnpadded.

private static void testDecodeUnpadded() throws Throwable {
    byte[] srcA = new byte[] { 'Q', 'Q' };
    byte[] srcAA = new byte[] { 'Q', 'Q', 'E' };
    Base64.Decoder dec = Base64.getDecoder();
    byte[] ret = dec.decode(srcA);
    if (ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A failed");
    ret = dec.decode(srcAA);
    if (ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA failed");
    ret = new byte[10];
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 && ret[0] != 'A')
        throw new RuntimeException("Decoding unpadding input A from stream failed");
    if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 && ret[0] != 'A' && ret[1] != 'A')
        throw new RuntimeException("Decoding unpadding input AA from stream failed");
}
Also used : Base64(java.util.Base64) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 23 with Decoder

use of java.util.Base64.Decoder in project java-example by 1479005017.

the class CipherHelper method toPrivateKey.

/**
 * 转换私钥格式
 *
 * @param algorithm
 * @param privateCode
 * @return
 */
public static PrivateKey toPrivateKey(Algorithm algorithm, String privateCode) {
    try {
        Base64.Decoder decoder = Base64.getDecoder();
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decoder.decode(privateCode));
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm.getName());
        return keyFactory.generatePrivate(privateKeySpec);
    } catch (Exception e) {
        logger.info("私钥转换失败", e);
    }
    return null;
}
Also used : Base64(java.util.Base64) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 24 with Decoder

use of java.util.Base64.Decoder in project java-example by 1479005017.

the class CipherHelper method toSecretKey.

/**
 * 转换密钥格式
 *
 * @param algorithm
 * @param secretCode
 * @return
 */
public static SecretKey toSecretKey(Algorithm algorithm, String secretCode) {
    try {
        Base64.Decoder decoder = Base64.getDecoder();
        SecretKeySpec secretKeySpec = new SecretKeySpec(decoder.decode(secretCode), algorithm.getName());
        return secretKeySpec;
    } catch (Exception e) {
        logger.info("密钥转换失败", e);
    }
    return null;
}
Also used : Base64(java.util.Base64) SecretKeySpec(javax.crypto.spec.SecretKeySpec)

Example 25 with Decoder

use of java.util.Base64.Decoder in project FP-PSP-SERVER by FundacionParaguaya.

the class ImageParser method parse.

public static ImageDTO parse(String fileString, String imageDirectory) {
    ImageDTO image = null;
    // Image format validation (MIME type: image/jpeg, image/png, image/bmp, ...)
    String data = fileString.substring(0, fileString.indexOf(';'));
    String attribute = data.substring(0, data.indexOf(':'));
    if ("data".equals(attribute)) {
        String contentType = data.substring(data.indexOf(':') + 1);
        String type = contentType.substring(0, contentType.indexOf('/'));
        if ("image".equals(type)) {
            String format = contentType.substring(contentType.indexOf('/') + 1);
            String base64 = fileString.substring(fileString.indexOf(',') + 1);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] fileBytes = decoder.decode(base64);
            File file;
            try {
                file = File.createTempFile("file", ".tmp");
                Files.write(fileBytes, file);
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
                throw new InternalServerErrorException(e);
            }
            image = new ImageDTO();
            image.setFile(file);
            image.setFormat(format);
            image.setImageDirectory(imageDirectory);
        }
    }
    return image;
}
Also used : Base64(java.util.Base64) InternalServerErrorException(py.org.fundacionparaguaya.pspserver.common.exceptions.InternalServerErrorException) IOException(java.io.IOException) MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File)

Aggregations

Decoder (java.util.Base64.Decoder)27 Base64 (java.util.Base64)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 Encoder (java.util.Base64.Encoder)7 KeyFactory (java.security.KeyFactory)6 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)6 IOException (java.io.IOException)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)5 JOSEObjectType (com.nimbusds.jose.JOSEObjectType)4 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)4 JWSHeader (com.nimbusds.jose.JWSHeader)4 JWSSigner (com.nimbusds.jose.JWSSigner)4 MACSigner (com.nimbusds.jose.crypto.MACSigner)4 JWKSource (com.nimbusds.jose.jwk.source.JWKSource)4 DefaultJOSEObjectTypeVerifier (com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier)4 JWSKeySelector (com.nimbusds.jose.proc.JWSKeySelector)4 JWSVerificationKeySelector (com.nimbusds.jose.proc.JWSVerificationKeySelector)4 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)4 SignedJWT (com.nimbusds.jwt.SignedJWT)4 RSAPublicKey (java.security.interfaces.RSAPublicKey)4