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();
}
}
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");
}
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;
}
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;
}
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;
}
Aggregations