public static String getPrivateKeyStr(PrivateKey privateKey) throws Exception { return new String(base64.encode(privateKey.getEncoded())); }
/** * 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); }
public static String getPublicKeyStr(PublicKey publicKey) throws Exception { return new String(base64.encode(publicKey.getEncoded())); }
private static String toBase64(String rawString) { Base64 base64decoder = new Base64(); return new String(base64decoder.encode(rawString.getBytes())); }
public static String pemEncodePublicKey(PublicKey publicKey) { String begin = "-----BEGIN PUBLIC KEY-----\n"; String end = "\n-----END PUBLIC KEY-----"; byte[] data = publicKey.getEncoded(); String base64encoded = new String(new Base64(false).encode(data)); return begin + base64encoded + end; }
public static synchronized String encrypt( String secret, String plainText ) { secret = substring( rightPad( secret, 16 ), 0, 16 ); SecretKey key = new SecretKeySpec( secret.getBytes(), "AES" ); try { cipher.init( Cipher.ENCRYPT_MODE, key ); byte[] cipherText = cipher.doFinal( plainText.getBytes() ); return new String( coder.encode( cipherText ) ); } catch ( Exception e ) { logger.error( "Encryption error", e ); } return null; }
/** * Encodes an Object using the base64 algorithm. This method * is provided in order to satisfy the requirements of the * Encoder interface, and will throw an EncoderException if the * supplied object is not of type byte[]. * * @param pObject Object to encode * @return An object (of type byte[]) containing the * base64 encoded data which corresponds to the byte[] supplied. * @throws EncoderException if the parameter supplied is not * of type byte[] */ public Object encode(Object pObject) throws EncoderException { if (!(pObject instanceof byte[])) { throw new EncoderException( "Parameter supplied to Base64 encode is not a byte[]"); } return encode((byte[]) pObject); }
return b64.encode(binaryData);
URLConnection urlConnection = url.openConnection(); String userPass = user + ":" + password; String basicAuth = "Basic " + new String(new Base64().encode(userPass.getBytes())); urlConnection.setRequestProperty("Authorization", basicAuth);
URLConnection urlConnection = link.openConnection(); if (authenticate) { String basicAuth = "Basic " + new String(new Base64().encode(userPass.getBytes())); urlConnection.setRequestProperty("Authorization", basicAuth);
@Override public void write(Writable w) throws IOException { // Get input data byte[] input; int inputLength; if (w instanceof Text) { input = ((Text) w).getBytes(); inputLength = ((Text) w).getLength(); } else { assert (w instanceof BytesWritable); input = ((BytesWritable) w).getBytes(); inputLength = ((BytesWritable) w).getLength(); } // Add signature byte[] wrapped = new byte[signature.length + inputLength]; for (int i = 0; i < signature.length; i++) { wrapped[i] = signature[i]; } for (int i = 0; i < inputLength; i++) { wrapped[i + signature.length] = input[i]; } // Encode byte[] output = base64.encode(wrapped); bytesWritable.set(output, 0, output.length); writer.write(bytesWritable); }
@Test public void testObjectEncodeWithValidParameter() throws Exception { final String original = "Hello World!"; final Object origObj = original.getBytes(CHARSET_UTF8); final Base64 b64 = new Base64(); final Object oEncoded = b64.encode(origObj); final byte[] bArray = Base64.decodeBase64((byte[]) oEncoded); final String dest = new String(bArray); assertEquals("dest string does not equal original", original, dest); }
@Test public void testObjectEncode() throws Exception { final Base64 b64 = new Base64(); assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(CHARSET_UTF8)))); }
@Test public void testObjectEncodeWithInvalidParameter() throws Exception { final Base64 b64 = new Base64(); try { b64.encode("Yadayadayada"); fail("encode(Object) didn't throw an exception when passed a String object"); } catch (final EncoderException e) { // Expected } }
private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final String content = "Hello World"; String encodedContent; final byte[] bytesUtf8 = StringUtils.getBytesUtf8(content); byte[] buffer = ArrayUtils.addAll(bytesUtf8, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); final byte[] encodedBytes = new Base64().encode(buffer, startPasSize, bytesUtf8.length); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); }
@Test public void testConstructor_Int_ByteArray_Boolean_UrlSafe() { // url-safe variation final Base64 base64 = new Base64(64, new byte[] { '\t' }, true); final byte[] encoded = base64.encode(Base64TestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no // == padding. expectedResult = expectedResult.replace('\n', '\t'); expectedResult = expectedResult.replace('+', '-'); expectedResult = expectedResult.replace('/', '_'); final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base64(64, \\t, true)", result, expectedResult); }
@Test public void testConstructor_Int_ByteArray_Boolean() { final Base64 base64 = new Base64(65, new byte[] { '\t' }, false); final byte[] encoded = base64.encode(Base64TestData.DECODED); String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE; expectedResult = expectedResult.replace('\n', '\t'); final String result = StringUtils.newStringUtf8(encoded); assertEquals("new Base64(65, \\t, false)", expectedResult, result); }
return new String(BASE_64_CODEC.encode(outToken), StandardCharsets.US_ASCII); } catch (GSSException | IllegalAccessException | NoSuchFieldException | ClassNotFoundException e) { throw new AuthenticationException(e);
@Override public String run() throws Exception { // This Oid for Kerberos GSS-API mechanism. Oid mechOid = new Oid("1.2.840.113554.1.2.2"); // Oid for kerberos principal name Oid krb5PrincipalOid = new Oid("1.2.840.113554.1.2.2.1"); GSSManager manager = GSSManager.getInstance(); // GSS name for server GSSName serverName = manager.createName(serverPrincipal, krb5PrincipalOid); // Create a GSSContext for authentication with the service. // We're passing client credentials as null since we want them to be read from the Subject. GSSContext gssContext = manager.createContext(serverName, mechOid, null, GSSContext.DEFAULT_LIFETIME); gssContext.requestMutualAuth(false); // Establish context byte[] inToken = new byte[0]; byte[] outToken = gssContext.initSecContext(inToken, 0, inToken.length); gssContext.dispose(); // Base64 encoded and stringified token for server return new String(base64codec.encode(outToken)); } }
/** * 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); }