List of usage examples for org.bouncycastle.util.encoders Base64 encode
public static byte[] encode(byte[] data)
From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java
License:Open Source License
public static String encrypt(String data, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); final Random r = new SecureRandom(); byte[] salt = new byte[SALT_SIZE]; r.nextBytes(salt);/*from w w w . ja v a 2 s . co m*/ SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC"); cipher.init(Cipher.ENCRYPT_MODE, fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE))); byte[] encVal = cipher.doFinal(data.getBytes()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // writing encrypted data along with the salt in the format readable by // open ssl api bos.write("Salted__".getBytes()); bos.write(salt); bos.write(encVal); String encryptedValue = new String(Base64.encode(bos.toByteArray())); bos.close(); return encryptedValue; }
From source file:com.centurylink.mdw.listener.email.MDWEmailListener.java
License:Apache License
protected String handleMimeMessages(Message message, Map<String, byte[]> attachmentMap) throws Exception { String emailBody = null;/*from w w w .j av a 2 s.co m*/ Multipart multipart = (Multipart) message.getContent(); for (int j = 0, m = multipart.getCount(); j < m; j++) { Part part = multipart.getBodyPart(j); String disposition = part.getDisposition(); boolean isAttachment = false; if (disposition != null && (disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE)))) { isAttachment = true; InputStream input = part.getInputStream(); String fileName = part.getFileName(); String contentType = part.getContentType(); ByteArrayOutputStream output = new ByteArrayOutputStream(); if (saveAttachment(input, output)) { byte[] encodedByte = Base64.encode(output.toByteArray()); if (encodedByte != null && fileName != null && contentType != null) { attachmentMap.put(fileName + "~" + contentType, encodedByte); } } } if (!isAttachment && part.getContentType().startsWith("text/")) { emailBody = (String) part.getContent(); } } return emailBody; }
From source file:com.cloud.server.auth.LDAPUserAuthenticator.java
License:Apache License
@Override public String encode(String password) { // Password is not used, so set to a random string try {/*from w w w.j a v a 2 s.co m*/ SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG"); byte bytes[] = new byte[20]; randomGen.nextBytes(bytes); return Base64.encode(bytes).toString(); } catch (NoSuchAlgorithmException e) { throw new CloudRuntimeException("Failed to generate random password", e); } }
From source file:com.cloud.server.auth.SHA256SaltedUserAuthenticator.java
License:Apache License
@Override public String encode(String password) { // 1. Generate the salt SecureRandom randomGen;/*from w w w. j av a 2 s .c o m*/ try { randomGen = SecureRandom.getInstance("SHA1PRNG"); byte salt[] = new byte[s_saltlen]; randomGen.nextBytes(salt); String saltString = new String(Base64.encode(salt)); String hashString = encode(password, salt); // 3. concatenate the two and return return saltString + ":" + hashString; } catch (NoSuchAlgorithmException e) { throw new CloudRuntimeException("Unable to hash password", e); } catch (UnsupportedEncodingException e) { throw new CloudRuntimeException("Unable to hash password", e); } }
From source file:com.cloud.server.auth.SHA256SaltedUserAuthenticator.java
License:Apache License
public String encode(String password, byte[] salt) throws UnsupportedEncodingException, NoSuchAlgorithmException { byte[] passwordBytes = password.getBytes("UTF-8"); byte[] hashSource = new byte[passwordBytes.length + s_saltlen]; System.arraycopy(passwordBytes, 0, hashSource, 0, passwordBytes.length); System.arraycopy(salt, 0, hashSource, passwordBytes.length, s_saltlen); // 2. Hash the password with the salt MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(hashSource);// w ww.j a v a 2s . c om byte[] digest = md.digest(); return new String(Base64.encode(digest)); }
From source file:com.coyotesong.security.pbk.PbkTest.java
License:Apache License
/** * Test encryption.// ww w. j a va2 s.com * * @throws Exception */ @Test public void testEncryption() throws Exception { String plaintext = BUNDLE.getString("plaintext"); Cipher cipher = Cipher.getInstance(BUNDLE.getString("algorithm"), bc); cipher.init(Cipher.ENCRYPT_MODE, cipherKey, ivSpec); byte[] actual = cipher.doFinal(plaintext.getBytes()); assertEquals(BUNDLE.getString("ciphertext"), new String(Base64.encode(actual), Charset.forName("UTF-8"))); }
From source file:com.dcsquare.fileauthplugin.utility.Commands.java
License:Apache License
/** * Encodes a salt with base64// w w w. jav a2 s . c o m * * @param salt raw salt * @return base64 encoded salt */ private String encodeSalt(String salt) { return new String(Base64.encode(salt.getBytes()), Charsets.UTF_8); }
From source file:com.entertailion.android.shapeways.api.Request.java
License:Apache License
/** * Generate HMAC-SHA1 for message/*from ww w . j a v a 2 s . com*/ * * @param key * @param message * @return * @throws Exception */ private static String generateHmac(String key, String message) throws Exception { Log.d(LOG_TAG, "generateHmac: " + key + "=" + message); byte[] keyBytes = key.getBytes(ShapewaysClient.ENCODING); byte[] data = message.getBytes(ShapewaysClient.ENCODING); HMac macProvider = new HMac(new SHA1Digest()); macProvider.init(new KeyParameter(keyBytes)); macProvider.reset(); macProvider.update(data, 0, data.length); byte[] output = new byte[macProvider.getMacSize()]; macProvider.doFinal(output, 0); byte[] hmac = Base64.encode(output); return new String(hmac).replaceAll("\r\n", ""); }
From source file:com.eucalyptus.auth.DatabaseAccountProxy.java
License:Open Source License
@Override public ServerCertificate addServerCertificate(String certName, String certBody, String certChain, String certPath, String pk) throws AuthException { if (!ServerCertificateEntity.isCertificateNameValid(certName)) throw new AuthException(AuthException.INVALID_SERVER_CERT_NAME); if (!ServerCertificateEntity.isCertificatePathValid(certPath)) throw new AuthException(AuthException.INVALID_SERVER_CERT_PATH); String encPk = null;//from w w w.j a v a 2s.co m String sessionKey = null; try { // generate symmetric key final MessageDigest digest = Digest.SHA256.get(); final byte[] salt = new byte[32]; Crypto.getSecureRandomSupplier().get().nextBytes(salt); digest.update(this.lookupAdmin().getPassword().getBytes(Charsets.UTF_8)); digest.update(salt); final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES"); // encrypt the server pk Cipher cipher = Ciphers.AES_GCM.get(); final byte[] iv = new byte[32]; Crypto.getSecureRandomSupplier().get().nextBytes(iv); cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv)); final byte[] cipherText = cipher.doFinal(pk.getBytes()); encPk = new String(Base64.encode(Arrays.concatenate(iv, cipherText))); final PublicKey euarePublicKey = SystemCredentials.lookup(Euare.class).getCertificate().getPublicKey(); cipher = Ciphers.RSA_PKCS1.get(); cipher.init(Cipher.WRAP_MODE, euarePublicKey); byte[] wrappedKeyBytes = cipher.wrap(symmKey); sessionKey = new String(Base64.encode(wrappedKeyBytes)); } catch (final Exception e) { LOG.error("Failed to encrypt key", e); throw Exceptions.toUndeclared(e); } try { final ServerCertificate found = lookupServerCertificate(certName); if (found != null) throw new AuthException(AuthException.SERVER_CERT_ALREADY_EXISTS); } catch (final NoSuchElementException | AuthException ex) { ; } catch (final Exception ex) { throw ex; } final String certId = Crypto.generateAlphanumericId(21, "ASC"); final EntityTransaction db = Entities.get(ServerCertificateEntity.class); ServerCertificateEntity entity = null; try { final UserFullName accountAdmin = UserFullName.getInstance(this.lookupAdmin()); entity = new ServerCertificateEntity(accountAdmin, certName); entity.setCertBody(certBody); entity.setCertChain(certChain); entity.setCertPath(certPath); entity.setPrivateKey(encPk); entity.setSessionKey(sessionKey); entity.setCertId(certId); Entities.persist(entity); db.commit(); } catch (final Exception ex) { LOG.error("Failed to persist server certificate entity", ex); throw Exceptions.toUndeclared(ex); } finally { if (db.isActive()) db.rollback(); } return ServerCertificates.ToServerCertificate.INSTANCE.apply(entity); }
From source file:com.eucalyptus.auth.euare.EuareServerCertificateUtil.java
License:Open Source License
public static String getEncryptedKey(final String certArn, final String certPem) throws AuthException { final ServerCertificate targetCert = lookupServerCertificate(certArn); // generate symmetric key final MessageDigest digest = Digest.SHA256.get(); final byte[] salt = new byte[32]; Crypto.getSecureRandomSupplier().get().nextBytes(salt); digest.update(salt);/* w w w . ja v a 2s .com*/ final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES"); try { // encrypt the server pk using symm key Cipher cipher = Ciphers.AES_CBC.get(); final byte[] iv = new byte[16]; Crypto.getSecureRandomSupplier().get().nextBytes(iv); cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv)); final byte[] cipherText = cipher.doFinal(Base64.encode(targetCert.getPrivateKey().getBytes())); final String encPrivKey = new String(Base64.encode(Arrays.concatenate(iv, cipherText))); // encrypt the symmetric key using the certPem X509Certificate x509Cert = PEMFiles.getCert(B64.standard.dec(certPem)); cipher = Ciphers.RSA_PKCS1.get(); cipher.init(Cipher.ENCRYPT_MODE, x509Cert.getPublicKey()); byte[] symmkey = cipher.doFinal(symmKey.getEncoded()); final String b64SymKey = new String(Base64.encode(symmkey)); return String.format("%s\n%s", b64SymKey, encPrivKey); } catch (final Exception ex) { throw Exceptions.toUndeclared(ex); } }