@Override InternalBinaryStringEncoder getEncoder() { return new AbstractBouncyCastleInternalBinaryStringEncoder(new Base64Encoder(), BLOCK_SIZE, CHAR_SIZE) { @Override public boolean isValidEncoding(byte b) { return (b == 0x2b || b == 0x3d || (b >= 0x2f && b <= 0x39) || (b >= 0x41 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a)); } }; } }
private int nextI(byte[] data, int i, int finish) { while ((i < finish) && ignore((char)data[i])) { i++; } return i; }
public Base64Encoder() { initialiseDecodingTable(); }
if (!ignore((char)data[end - 1])) int finish = end - 4; i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); outLen += decodeLastBlock(out, (char)data[end - 4], (char)data[end - 3], (char)data[end - 2], (char)data[end - 1]);
public String encrypt(String str, String passPhrase) throws PlexusCipherException { try { byte[] salt = getSalt(SALT_SIZE); Cipher cipher = init(passPhrase, salt, true); // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes(STRING_ENCODING); // Encrypt it byte[] enc = cipher.doFinal(utf8); // Encode bytes to base64 to get a string Base64Encoder b64 = new Base64Encoder(); byte saltLen = (byte) (salt.length & 0x00ff); int encLen = enc.length; byte[] res = new byte[salt.length + encLen + 1]; res[0] = saltLen; System.arraycopy(salt, 0, res, 1, saltLen); System.arraycopy(enc, 0, res, saltLen + 1, encLen); ByteArrayOutputStream bout = new ByteArrayOutputStream(res.length * 2); b64.encode(res, 0, res.length, bout); return bout.toString(STRING_ENCODING); } catch (Exception e) { throw new PlexusCipherException(e); } }
Base64Encoder decoder = new Base64Encoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); decoder.decode( str, baos ); byte[] res = baos.toByteArray();
@Override public byte[] encrypt(final byte[] payload, final String passPhrase) { checkNotNull(payload); checkNotNull(passPhrase); try { byte[] salt = new byte[saltSize]; secureRandom.nextBytes(salt); byte[] enc = createCipher(passPhrase, salt, Cipher.ENCRYPT_MODE).doFinal(payload); byte saltLen = (byte) (salt.length & 0x00ff); int encLen = enc.length; byte[] res = new byte[salt.length + encLen + 1]; res[0] = saltLen; System.arraycopy(salt, 0, res, 1, saltLen); System.arraycopy(enc, 0, res, saltLen + 1, encLen); ByteArrayOutputStream bout = new ByteArrayOutputStream(res.length * 2); base64Encoder.encode(res, 0, res.length, bout); return bout.toByteArray(); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public byte[] decrypt(final byte[] payload, final String passPhrase) { checkNotNull(payload); checkNotNull(passPhrase); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); base64Encoder.decode(payload, 0, payload.length, baos); byte[] allEncryptedBytes = baos.toByteArray(); int totalLen = allEncryptedBytes.length; byte[] salt = new byte[SALT_SIZE]; System.arraycopy(allEncryptedBytes, 0, salt, 0, SALT_SIZE); byte padLen = allEncryptedBytes[SALT_SIZE]; byte[] encryptedBytes = new byte[totalLen - SALT_SIZE - 1 - padLen]; System.arraycopy(allEncryptedBytes, SALT_SIZE + 1, encryptedBytes, 0, encryptedBytes.length); Cipher cipher = createCipher(passPhrase, salt, Cipher.DECRYPT_MODE); return cipher.doFinal(encryptedBytes); } catch (IOException e) { throw new IllegalArgumentException("Invalid payload (base64 problem)", e); } catch (Exception e) { throw Throwables.propagate(e); } }
if (!ignore((char)data[end - 1])) if (!ignore((char)data[finish - 1])) i = nextI(data, off, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); int e0 = nextI(data, i, end); int e1 = nextI(data, e0 + 1, end); int e2 = nextI(data, e1 + 1, end); int e3 = nextI(data, e2 + 1, end); outLen += decodeLastBlock(out, (char)data[e0], (char)data[e1], (char)data[e2], (char)data[e3]);
public String encrypt( String str, String passPhrase ) throws PlexusCipherException { try { byte[] salt = getSalt( SALT_SIZE ); Cipher cipher = init( passPhrase, salt, true ); // Encode the string into bytes using utf-8 byte[] utf8 = str.getBytes( STRING_ENCODING ); // Encrypt it byte[] enc = cipher.doFinal( utf8 ); // Encode bytes to base64 to get a string Base64Encoder b64 = new Base64Encoder(); byte saltLen = (byte) ( salt.length & 0x00ff ); int encLen = enc.length; byte[] res = new byte[salt.length + encLen + 1]; res[0] = saltLen; System.arraycopy( salt, 0, res, 1, saltLen ); System.arraycopy( enc, 0, res, saltLen + 1, encLen ); ByteArrayOutputStream bout = new ByteArrayOutputStream( res.length * 2 ); b64.encode( res, 0, res.length, bout ); return bout.toString( STRING_ENCODING ); } catch ( Exception e ) { throw new PlexusCipherException( e ); } }
Base64Encoder decoder = new Base64Encoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); decoder.decode(str, baos); byte[] res = baos.toByteArray();
@Override public byte[] encrypt(final byte[] payload, final String passPhrase) { checkNotNull(payload); checkNotNull(passPhrase); try { byte[] salt = new byte[SALT_SIZE]; secureRandom.nextBytes(salt); Cipher cipher = createCipher(passPhrase, salt, Cipher.ENCRYPT_MODE); byte[] encryptedBytes = cipher.doFinal(payload); int len = encryptedBytes.length; byte padLen = (byte) (CHUNK_SIZE - (SALT_SIZE + len + 1) % CHUNK_SIZE); int totalLen = SALT_SIZE + len + padLen + 1; byte[] allEncryptedBytes = new byte[totalLen]; secureRandom.nextBytes(allEncryptedBytes); System.arraycopy(salt, 0, allEncryptedBytes, 0, SALT_SIZE); allEncryptedBytes[SALT_SIZE] = padLen; System.arraycopy(encryptedBytes, 0, allEncryptedBytes, SALT_SIZE + 1, len); ByteArrayOutputStream bout = new ByteArrayOutputStream(allEncryptedBytes.length * 2); base64Encoder.encode(allEncryptedBytes, 0, allEncryptedBytes.length, bout); return bout.toByteArray(); } catch (Exception e) { throw Throwables.propagate(e); } }
/** {@inheritDoc} */ protected void fillBuffer() throws IOException { position = 0; decodeBuffer.reset(); int count = 0; if (lineLength == 0) { count = in.read(byteBuffer); } else { int n; while (count + lineLength < CHUNK_SIZE) { n = in.read(byteBuffer, count, lineLength); if (n < 0) { break; } count += n; if (count % lineLength == 0) { // Skip line terminator character in.skip(1); } } } if (count > 0) { encoder.decode(byteBuffer, 0, count, decodeBuffer); } } }
if (!ignore(data.charAt(end - 1))) if (!ignore(data.charAt(finish - 1))) i = nextI(data, 0, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); int e0 = nextI(data, i, end); int e1 = nextI(data, e0 + 1, end); int e2 = nextI(data, e1 + 1, end); int e3 = nextI(data, e2 + 1, end); length += decodeLastBlock(out, data.charAt(e0), data.charAt(e1), data.charAt(e2), data.charAt(e3));
@Override InternalBinaryStringEncoder getEncoder() { return new AbstractBouncyCastleInternalBinaryStringEncoder(new Base64Encoder(), BLOCK_SIZE, CHAR_SIZE) { @Override public boolean isValidEncoding(byte b) { return (b == 0x2b || b == 0x3d || (b >= 0x2f && b <= 0x39) || (b >= 0x41 && b <= 0x5a) || (b >= 0x61 && b <= 0x7a)); } }; } }
private int nextI(byte[] data, int i, int finish) { while ((i < finish) && ignore((char)data[i])) { i++; } return i; }
public Base64Encoder() { initialiseDecodingTable(); }
@Override public byte[] decrypt(final byte[] payload, final String passPhrase) { checkNotNull(payload); checkNotNull(passPhrase); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); base64Encoder.decode(payload, 0, payload.length, baos); byte[] res = baos.toByteArray(); int saltLen = res[0] & 0x00ff; checkArgument(saltLen == saltSize, "Encrypted string corrupted (salt size)"); checkArgument(res.length >= (saltLen + 2), "Encrypted string corrupted (payload size)"); byte[] salt = new byte[saltLen]; System.arraycopy(res, 1, salt, 0, saltLen); int decLen = res.length - saltLen - 1; checkArgument(decLen >= 1, "Encrypted string corrupted (payload segment)"); byte[] dec = new byte[decLen]; System.arraycopy(res, saltLen + 1, dec, 0, decLen); Cipher cipher = createCipher(passPhrase, salt, Cipher.DECRYPT_MODE); return cipher.doFinal(dec); } catch (IOException e) { throw new IllegalArgumentException("Invalid payload (base64 problem)", e); } catch (Exception e) { throw Throwables.propagate(e); } }
if (!ignore(data.charAt(end - 1))) int finish = end - 4; i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); i = nextI(data, i, finish); length += decodeLastBlock(out, data.charAt(end - 4), data.charAt(end - 3), data.charAt(end - 2), data.charAt(end - 1));