Example usage for org.apache.commons.codec.binary Base64 encodeBase64String

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64String

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64String.

Prototype

public static String encodeBase64String(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided/*from w w w .j  a  v  a  2s  .c  om*/
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:net.bryansaunders.jee6divelog.util.HashUtilsTest.java

/**
 * Tests Encode/Decode of Base64./*from  w  w  w . j av  a  2 s.  c o m*/
 */
@Test
public void testBase64() {
    final String original = "Test String";
    final String expectedBase64 = Base64.encodeBase64String(original.getBytes());

    final String encoded = HashUtils.base64Encode(original);
    assertEquals(expectedBase64, encoded);
    assertEquals(original, HashUtils.base64Decode(encoded));
}

From source file:com.cloud.utils.EncryptionUtil.java

public static String generateSignature(String data, String key) {
    try {/*  w  w  w .j a va2 s .  c  o m*/
        final Mac mac = Mac.getInstance("HmacSHA1");
        final SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
        mac.init(keySpec);
        mac.update(data.getBytes("UTF-8"));
        final byte[] encryptedBytes = mac.doFinal();
        return Base64.encodeBase64String(encryptedBytes);
    } catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
        s_logger.error("exception occurred which encoding the data." + e.getMessage());
        throw new CloudRuntimeException("unable to generate signature", e);
    }
}

From source file:eu.scidipes.toolkits.pawebapp.util.Base64MultipartFileEditor.java

@Override
public void setValue(final Object value) {
    if (value instanceof MultipartFile) {
        final MultipartFile multipartFile = (MultipartFile) value;
        try {//from   ww  w .j a  v a2  s.  co  m
            super.setValue(Base64.encodeBase64String(multipartFile.getBytes()));
        } catch (final IOException ex) {
            throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
        }
    } else {
        super.setValue(value);
    }
}

From source file:com.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {/*from   w w w . j a v  a2 s .c  om*/
        // 1.?
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(256);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();

        // 2.??
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());

        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("SHA256withECDSA");
        signature.initSign(privateKey);
        signature.update(src.getBytes());
        byte[] res = signature.sign();
        System.out.println("??" + Base64.encodeBase64String(res));

        // 3.???
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("EC");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        signature = Signature.getInstance("SHA256withECDSA");
        signature.initVerify(publicKey);
        signature.update(src.getBytes());
        boolean bool = signature.verify(res);
        System.out.println("?" + bool);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:javafx1.Testpics.java

private void bildSchleife() {
    File f = new File("F:/NetBeansProjekte/pictures");
    File[] fileArray = f.listFiles();
    java.util.Arrays.sort(fileArray);
    for (File file : fileArray) {
        try {//w w w  .java 2  s .c o  m
            System.out.println("file: " + file.getCanonicalPath());
            File outputfile = new File(file.getCanonicalPath() + ".jpg");
            BufferedImage bi = ImageIO.read(file);
            System.out.println(" enc" + encodeToString(bi, "jpg"));
            ImageIO.write(bi, "jpg", outputfile);
            bi = ImageIO.read(outputfile);
            System.out.println(" w" + bi.getWidth() + " h" + bi.getHeight());
            byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
            // System.out.println(new String(data));
            byte[] x = Base64.encodeBase64URLSafe(data);
            System.out.println("x" + new String(x));
            String b64 = Base64.encodeBase64String(data);
            System.out.println("64" + b64);

            //                byte[] backToBytes = Base64.decodeBase64(base64String);
            //                InputStream in = new ByteArrayInputStream(backToBytes);
            //                BufferedImage bi;
            //                bi = ImageIO.read(in);
            //byte[] xx = base64String.getBytes(StandardCharsets.UTF_8);
            // bild, wie es von http kommt
            //                RunPic rp = new RunPic(this, data);
            //                Platform.runLater(rp);
            try {
                Thread.sleep(50);
                break;
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (IOException ex) {
            Logger.getLogger(JavaFX1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:io.apicurio.hub.api.github.GitHubCreateBlob.java

/**
 * @param content the content to set//  ww w  .  jav a 2  s  .  co m
 */
public void setContent(InputStream content) throws IOException {
    this.content = Base64.encodeBase64String(IOUtils.toByteArray(content));
    this.encoding = "base64";
}

From source file:com.consol.citrus.validation.text.BinaryBase64MessageValidator.java

@Override
public void validateMessagePayload(Message receivedMessage, Message controlMessage,
        ValidationContext validationContext, TestContext context) throws ValidationException {

    if (receivedMessage.getPayload() instanceof byte[]) {
        receivedMessage.setPayload(Base64.encodeBase64String(receivedMessage.getPayload(byte[].class)));
    }//from  w w  w. jav a  2 s. c o  m

    super.validateMessagePayload(receivedMessage, controlMessage, validationContext, context);
}

From source file:com.spectralogic.ds3client.utils.Signature.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * * @param data// w  w w .  java 2s.c  o m
 * The data to be signed.
 * @param key
 * The signing key.
 * @return
 * The Base64-encoded RFC 2104-compliant HMAC signature.
 * @throws
 * java.security.SignatureException when signature generation fails
 */
public static String calculateRFC2104HMAC(final String data, final String key)
        throws java.security.SignatureException {
    LOG.debug("String to sign: {}", data.replace("\n", "\\n"));
    final String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")),
                HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        final byte[] rawHmac = mac.doFinal(data.getBytes(Charset.forName("UTF-8")));
        result = Base64.encodeBase64String(rawHmac);
    } catch (final Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result.trim();
}

From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String encrypt(String secretkey, String iv, String toEncrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return Base64.encodeBase64String(cipher.doFinal(toEncrypt.getBytes()));
}