Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:buttonsFrame.java

private void validateDesafioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_validateDesafioMouseClicked
    try {/*  w w w.ja  va  2 s .  co  m*/

        //INSTANCIA A CLASSE PARA ASSINATURA PKCS7
        assinaBc signer = new assinaBc();
        KeyStore keyStore = signer.loadKeyStore();
        CMSSignedDataGenerator signatureGenerator = signer.setUpProvider(keyStore);

        Conexao conexao = new Conexao();
        byte[] signedBytes = signer.signPkcs7(Conexao.convertStringToByteArray(), signatureGenerator);//ASSINA O BYTE ARRAY CONVERTIDO DE separaJson
        String recebeCrip = new String(Base64.encode(signedBytes));//Atribui a uma string o byte array assinado

        String assinaturaFormat = recebeCrip.replaceAll("(.{64})", "$1\n");//quebra automatica a cada 64 caracteres
        String resposta = ("-----BEGIN PKCS7-----\n" + assinaturaFormat + "\n-----END PKCS7-----");//Add o cabealho PKCS7 na assinatura
        //System.out.println(resposta);

        TransformaJson<Resposta> tJson = new TransformaJson<Resposta>();
        Resposta resp = new Resposta(resposta);
        org.json.JSONObject json = tJson.converteJSON(resp);

        String reposta = conexao.requisicaoPost("https://api-prova.lab.ca.inf.br:9445/desafio", json);
        textResposta.setText(conexao.imprimeResultado());

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

}

From source file:CryptoTests.java

@Test
public void TestString() throws Exception {
    ByteArrayOutputStream target = new ByteArrayOutputStream();
    KeyPair pair = CryptoUtils.generateRSAKeyPair(512);
    String name = "Aleksandar";
    target.write(Base64.encode(name.getBytes()));
    String keyToString = new String(Base64.encode(pair.getPublic().getEncoded()), "UTF-8");
    //keyToString = new String(keyToString.getBytes(), "UTF-8");
    assert Arrays.equals(Base64.decode(keyToString), pair.getPublic().getEncoded()) : "Not equal";
}

From source file:Util.java

License:Apache License

/**
 * @param text - an unencrypted text to be encrypted (Base64 logic)
 * @return an encrypted string - using Base64 logic)
 *///  w  ww . j av a  2  s  .  c o  m
public static String encrypt(String text) throws NoSuchAlgorithmException {
    String result = new String(Base64.encode(text.getBytes()));
    return result;
}

From source file:ael.com.loterias.Library.c_PGP_KeyGenerator.java

License:GNU General Public License

public void generateKeys() {

    try {//from w  w w. j  ava  2  s. co m
        KeyPairGenerator generator;
        generator = KeyPairGenerator.getInstance("RSA", "BC");
        generator.initialize(SizeAlgorithm, new SecureRandom());
        KeyPair pairKeys = generator.generateKeyPair();

        pubKey = pairKeys.getPublic();
        privKey = pairKeys.getPrivate();

        byte[] publicKeyBytes = pubKey.getEncoded();
        this.pubKeyString = new String(Base64.encode(publicKeyBytes));

        byte[] privKeyBytes = privKey.getEncoded();
        this.privKeyString = new String(Base64.encode(privKeyBytes));

        c_logging.getInstance().log(c_logging.LOG_INFO, "GEN KEY PAIR OK (" + SizeAlgorithm + ")!!!");
        c_logging.getInstance().log(c_logging.LOG_INFO, "PGP Private Key: " + this.privKeyString);
        c_logging.getInstance().log(c_logging.LOG_INFO, "PGP Public Key :  " + this.pubKeyString);

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
}

From source file:ael.com.loterias.Library.c_PGP_KeyGenerator.java

License:GNU General Public License

public String encrypt(Key publicKey, String PlainText) {
    String encryptedData = null;//from w  ww .j a  v  a 2 s .com

    try {
        byte[] BytePlainText = PlainText.getBytes();
        Cipher cipher = Cipher.getInstance("RSA", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(BytePlainText);
        byte[] encodedBytes = Base64.encode(encryptedBytes); //used library encode decode
        encryptedData = new String(encodedBytes);
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return encryptedData;
}

From source file:aff4.commonobjects.WarrantWriter.java

License:Open Source License

public static String calculateSignature(String canonicalGraph, PrivateKey key)
        throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException

{
    Security.addProvider(new BouncyCastleProvider());
    String signature = null;//from w  w  w .j av a2 s  .  com
    Signature sig = Signature.getInstance("SHA256withRSA", new BouncyCastleProvider());
    sig.initSign(key);
    sig.update(canonicalGraph.getBytes("UTF-8"));
    signature = new String(Base64.encode(sig.sign()), "UTF-8");
    return signature;
}

From source file:android.webkit.CertTool.java

License:Apache License

static String getSignedPublicKey(Context context, int index, String challenge) {
    try {/*from ww  w  .j a  va2 s  . c om*/
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize((index == 0) ? 2048 : 1024);
        KeyPair pair = generator.genKeyPair();

        NetscapeCertRequest request = new NetscapeCertRequest(challenge, MD5_WITH_RSA, pair.getPublic());
        request.sign(pair.getPrivate());
        byte[] signed = request.toASN1Object().getDEREncoded();

        Credentials.getInstance().install(context, pair);
        return new String(Base64.encode(signed));
    } catch (Exception e) {
        Log.w(LOGTAG, e);
    }
    return null;
}

From source file:Applet.utiles.Utiles.java

public static String convertPKCS10ToBase64(PKCS10CertificationRequest pkcs10) {
    byte[] pk = Base64.encode(pkcs10.getEncoded());
    String baseString = "";
    for (int i = 0; i < pk.length; i++) {
        baseString = baseString + (char) pk[i];
    }/*from  ww  w . java  2  s  .  c o  m*/
    return baseString;
}

From source file:Applet.utiles.Utiles.java

public static String convertX509ToBase64(X509Certificate cert) throws CertificateEncodingException {

    byte[] pk = Base64.encode(cert.getEncoded());
    String certString = "";
    for (int i = 0; i < pk.length; i++) {
        certString = certString + (char) pk[i];
    }/* w w w  .jav a 2 s  . c om*/
    return certString;
}

From source file:Applet.utiles.Utiles.java

/**
 * Mtodo que devuelve un archivo en base 64. Se pasa por parmetro la
 * ruta del archivo.// w  w  w.  ja v a  2s  . c om
 * @param fileName
 * @return 
 * @throws java.io.IOException
 */
public static String encodeFileToBase64Binary(String fileName) throws IOException {
    String encodedString = "";
    if (!isNullOrEmpty(fileName)) {
        File file = new File(fileName);
        byte[] bytes = loadFile(file);
        byte[] encoded = Base64.encode(bytes);
        encodedString = new String(encoded);
    }
    return encodedString;
}