本文整理了Java中org.apache.commons.codec.binary.Base64.decode()
方法的一些代码示例,展示了Base64.decode()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Base64.decode()
方法的具体详情如下:
包路径:org.apache.commons.codec.binary.Base64
类名称:Base64
方法名:decode
[英]Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[].
[中]使用base64算法解码对象。提供此方法是为了满足解码器接口的要求,如果提供的对象不是byte[]类型,则将抛出DecoderException。
代码示例来源:origin: commons-codec/commons-codec
/**
* Decodes Base64 data into octets.
* <p>
* <b>Note:</b> this method seamlessly handles data encoded in URL-safe or normal mode.
* </p>
*
* @param base64Data
* Byte array containing Base64 data
* @return Array containing decoded data.
*/
public static byte[] decodeBase64(final byte[] base64Data) {
return new Base64().decode(base64Data);
}
代码示例来源:origin: commons-codec/commons-codec
@Test
public void testStringToByteVariations() throws DecoderException {
final Base64 base64 = new Base64();
final String s1 = "SGVsbG8gV29ybGQ=\r\n";
final String s2 = "";
final String s3 = null;
final String s4a = "K/fMJwH+Q5e0nr7tWsxwkA==\r\n";
final String s4b = "K_fMJwH-Q5e0nr7tWsxwkA";
final byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090"); // for
// url-safe
// tests
assertEquals("StringToByte Hello World", "Hello World", StringUtils.newStringUtf8(base64.decode(s1)));
assertEquals("StringToByte Hello World", "Hello World",
StringUtils.newStringUtf8((byte[]) base64.decode((Object) s1)));
assertEquals("StringToByte static Hello World", "Hello World",
StringUtils.newStringUtf8(Base64.decodeBase64(s1)));
assertEquals("StringToByte \"\"", "", StringUtils.newStringUtf8(base64.decode(s2)));
assertEquals("StringToByte static \"\"", "", StringUtils.newStringUtf8(Base64.decodeBase64(s2)));
assertEquals("StringToByte null", null, StringUtils.newStringUtf8(base64.decode(s3)));
assertEquals("StringToByte static null", null, StringUtils.newStringUtf8(Base64.decodeBase64(s3)));
assertTrue("StringToByte UUID", Arrays.equals(b4, base64.decode(s4b)));
assertTrue("StringToByte static UUID", Arrays.equals(b4, Base64.decodeBase64(s4a)));
assertTrue("StringToByte static-url-safe UUID", Arrays.equals(b4, Base64.decodeBase64(s4b)));
}
代码示例来源:origin: commons-codec/commons-codec
/**
* Decodes a Base64 String into octets.
* <p>
* <b>Note:</b> this method seamlessly handles data encoded in URL-safe or normal mode.
* </p>
*
* @param base64String
* String containing Base64 data
* @return Array containing decoded data.
* @since 1.4
*/
public static byte[] decodeBase64(final String base64String) {
return new Base64().decode(base64String);
}
代码示例来源:origin: apache/avro
private static String fromBase64(String base64String) {
Base64 base64decoder = new Base64();
return new String(base64decoder.decode(base64String.getBytes()));
}
代码示例来源:origin: Javen205/IJPay
/**
* 公钥验证签名
* @param data
* @param sign
* @param publicKey
* @return
* @throws Exception
*/
public static boolean checkByPublicKey(String data, String sign, String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = base64.decode(publicKey);
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
java.security.Signature signature = java.security.Signature.getInstance("SHA256WithRSA");
signature.initVerify(pubKey);
signature.update(data.getBytes("utf-8"));
return signature.verify(base64.decode(sign.getBytes("UTf-8")));
}
代码示例来源:origin: cloudfoundry/uaa
public static PublicKey getRsaPublicKey(JsonWebKey key) {
final Base64 decoder = new Base64(true);
String e = (String) key.getKeyProperties().get("e");
String n = (String) key.getKeyProperties().get("n");
BigInteger modulus = new BigInteger(1, decoder.decode(n.getBytes(StandardCharsets.UTF_8)));
BigInteger exponent = new BigInteger(1, decoder.decode(e.getBytes(StandardCharsets.UTF_8)));
try {
return KeyFactory.getInstance("RSA").generatePublic(
new RSAPublicKeySpec(modulus, exponent)
);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e1) {
throw new IllegalStateException(e1);
}
}
}
代码示例来源:origin: robovm/robovm
/**
* Decodes an Object using the base64 algorithm. This method
* is provided in order to satisfy the requirements of the
* Decoder interface, and will throw a DecoderException if the
* supplied object is not of type byte[].
*
* @param pObject Object to decode
* @return An object (of type byte[]) containing the
* binary data which corresponds to the byte[] supplied.
* @throws DecoderException if the parameter supplied is not
* of type byte[]
*/
public Object decode(Object pObject) throws DecoderException {
if (!(pObject instanceof byte[])) {
throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]");
}
return decode((byte[]) pObject);
}
代码示例来源:origin: Javen205/IJPay
/**
* 从字符串中加载公钥
*
* @param publicKeyStr
* 公钥数据字符串
* @throws Exception
* 加载公钥时产生的异常
*/
public static PublicKey loadPublicKey(String publicKeyStr) throws Exception {
try {
byte[] buffer = base64.decode(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("公钥非法");
} catch (NullPointerException e) {
throw new Exception("公钥数据为空");
}
}
代码示例来源:origin: Javen205/IJPay
/**
* 从字符串中加载私钥<br>
* 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
*
* @param privateKeyStr
* @return
* @throws Exception
*/
public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
try {
byte[] buffer = base64.decode(privateKeyStr);
// X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
}
}
代码示例来源:origin: Javen205/IJPay
/**
* 私钥签名
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(base64.decode(privateKey));
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
java.security.Signature signature = java.security.Signature.getInstance("SHA256WithRSA");
signature.initSign(priKey);
signature.update(data.getBytes("UTf-8"));
byte[] signed = signature.sign();
return base64.encodeToString(signed);
}
/**
代码示例来源:origin: apache/usergrid
@SuppressWarnings("unused")
public static synchronized String decrypt( String secret, String codedText ) {
secret = substring( rightPad( secret, 16 ), 0, 16 );
SecretKey key = new SecretKeySpec( secret.getBytes(), "AES" );
byte[] encypted = coder.decode( codedText.getBytes() );
try {
cipher.init( Cipher.DECRYPT_MODE, key );
byte[] decrypted = cipher.doFinal( encypted );
return new String( decrypted );
}
catch ( Exception e ) {
logger.error( "Decryption error", e );
}
return null;
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
result = base64.decode(value);
代码示例来源:origin: apache/hive
@Override
public boolean next(LongWritable key, BytesWritable value) throws IOException {
while (reader.next(key, text)) {
// text -> byte[] -> value
byte[] textBytes = text.getBytes();
int length = text.getLength();
// Trim additional bytes
if (length != textBytes.length) {
textBytes = Arrays.copyOf(textBytes, length);
}
byte[] binaryData = base64.decode(textBytes);
// compare data header with signature
int i;
for (i = 0; i < binaryData.length && i < signature.length
&& binaryData[i] == signature[i]; ++i) {
;
}
// return the row only if it's not corrupted
if (i == signature.length) {
value.set(binaryData, signature.length, binaryData.length
- signature.length);
return true;
}
}
// no more data
return false;
}
代码示例来源:origin: commons-codec/commons-codec
@Test
public void testObjectDecodeWithValidParameter() throws Exception {
final String original = "Hello World!";
final Object o = Base64.encodeBase64(original.getBytes(CHARSET_UTF8));
final Base64 b64 = new Base64();
final Object oDecoded = b64.decode(o);
final byte[] baDecoded = (byte[]) oDecoded;
final String dest = new String(baDecoded);
assertEquals("dest string does not equal original", original, dest);
}
代码示例来源:origin: commons-codec/commons-codec
@Test
public void testObjectDecodeWithInvalidParameter() throws Exception {
final Base64 b64 = new Base64();
try {
b64.decode(Integer.valueOf(5));
fail("decode(Object) didn't throw an exception when passed an Integer object");
} catch (final DecoderException e) {
// ignored
}
}
代码示例来源:origin: org.apache.hadoop/hadoop-common
/**
* Modify the writable to the value from the newValue.
* @param obj the object to read into
* @param newValue the string with the url-safe base64 encoded bytes
* @throws IOException
*/
private static void decodeWritable(Writable obj,
String newValue) throws IOException {
if (newValue == null) {
throw new HadoopIllegalArgumentException(
"Invalid argument, newValue is null");
}
Base64 decoder = new Base64(0, null, true);
DataInputBuffer buf = new DataInputBuffer();
byte[] decoded = decoder.decode(newValue);
buf.reset(decoded, decoded.length);
obj.readFields(buf);
}
代码示例来源:origin: commons-codec/commons-codec
/**
* Tests a lineSeparator much bigger than DEFAULT_BUFFER_SIZE.
*
* @see "<a href='http://mail-archives.apache.org/mod_mbox/commons-dev/201202.mbox/%3C4F3C85D7.5060706@snafu.de%3E'>dev@commons.apache.org</a>"
*/
@Test
@Ignore
public void testHugeLineSeparator() {
final int BaseNCodec_DEFAULT_BUFFER_SIZE = 8192;
final int Base64_BYTES_PER_ENCODED_BLOCK = 4;
final byte[] baLineSeparator = new byte[BaseNCodec_DEFAULT_BUFFER_SIZE * 4 - 3];
final Base64 b64 = new Base64(Base64_BYTES_PER_ENCODED_BLOCK, baLineSeparator);
final String strOriginal = "Hello World";
final String strDecoded = new String(b64.decode(b64.encode(StringUtils.getBytesUtf8(strOriginal))));
assertEquals("testDEFAULT_BUFFER_SIZE", strOriginal, strDecoded);
}
代码示例来源:origin: commons-codec/commons-codec
@Test
public void testNonBase64Test() throws Exception {
final byte[] bArray = { '%' };
assertFalse("Invalid Base64 array was incorrectly validated as " + "an array of Base64 encoded data",
Base64.isBase64(bArray));
try {
final Base64 b64 = new Base64();
final byte[] result = b64.decode(bArray);
assertEquals("The result should be empty as the test encoded content did "
+ "not contain any valid base 64 characters", 0, result.length);
} catch (final Exception e) {
fail("Exception was thrown when trying to decode "
+ "invalid base64 encoded data - RFC 2045 requires that all "
+ "non base64 character be discarded, an exception should not" + " have been thrown");
}
}
代码示例来源:origin: commons-codec/commons-codec
/**
* Test the Base64 implementation
*/
@Test
public void testBase64() {
final String content = "Hello World";
String encodedContent;
byte[] encodedBytes = Base64.encodeBase64(StringUtils.getBytesUtf8(content));
encodedContent = StringUtils.newStringUtf8(encodedBytes);
assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
Base64 b64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, null); // null
// lineSeparator
// same as
// saying
// no-chunking
encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
encodedContent = StringUtils.newStringUtf8(encodedBytes);
assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
b64 = new Base64(0, null); // null lineSeparator same as saying
// no-chunking
encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
encodedContent = StringUtils.newStringUtf8(encodedBytes);
assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
// bogus characters to decode (to skip actually) {e-acute*6}
final byte[] decode = b64.decode("SGVsbG{\u00e9\u00e9\u00e9\u00e9\u00e9\u00e9}8gV29ybGQ=");
final String decodeString = StringUtils.newStringUtf8(decode);
assertEquals("decode hello world", "Hello World", decodeString);
}
代码示例来源:origin: line/armeria
))
.setK(ImmutableSet.of(true, false, false, false, true))
.setL(base64Encoder.decode("SGVsbG8gV29ybGQ="))
.setM("hello \"spherical\" world!")
.setN((short) 678)
.setV(Letter.BETA)
.setW(TestUnion.f2(4))
.setX(ImmutableList.of(TestUnion.f2(5), TestUnion.f1(base64Encoder.decode("SGVsbG8gV29ybGQ="))))
.setY(Letter.ALPHA);
内容来源于网络,如有侵权,请联系作者删除!