List of usage examples for org.bouncycastle.util.encoders Base64 encode
public static byte[] encode(byte[] data)
From source file:org.wso2.carbon.identity.certificateauthority.data.CRLDataHolder.java
License:Open Source License
/** * set x509CRL/* w w w . j a v a2 s . c om*/ * * @param incrl */ public void setCRL(X509CRL incrl) { try { String b64Crl = new String(Base64.encode((incrl).getEncoded())); setBase64Crl(b64Crl); } catch (CRLException ce) { log.error("Can't extract DER encoded CRL.", ce); } }
From source file:org.xwiki.crypto.internal.Convert.java
License:Open Source License
/** * Encode given data and return the base64 encoded result as a byte array. * /*from ww w . j av a 2s. c o m*/ * @param data the data to encode * @return base64 encoded data array */ public static byte[] toBase64(byte[] data) { if (data == null) { return new byte[0]; } return Base64.encode(data); }
From source file:org.xwiki.crypto.internal.FindEntropyForSecureRandomProvider.java
License:Open Source License
@Test public void testFindGoodEntropySource() throws Exception { FixedSecureRandomProvider rndprov = mocker .getInstance(new DefaultParameterizedType(null, Provider.class, SecureRandom.class)); RecordingSecureRandom rnd = new RecordingSecureRandom(); boolean succeed = false; byte[] rndSource = null; long maxsize = 4096; while (!succeed) { rndprov.setRandomSource(rnd);// w w w . j a v a 2 s. c o m rsaGenerator.generate(); rsaGenerator.generate(); while (rnd.counter > maxsize) { int rejectCount = 0; while (rejectCount < 10 && rnd.counter > maxsize) { rejectCount++; System.out.println(String.format("Rejected %d > %d", rnd.counter, maxsize)); rnd = new RecordingSecureRandom(); rndprov.setRandomSource(rnd); rsaGenerator.generate(); rsaGenerator.generate(); } if (rejectCount >= 10) { maxsize <<= 1; } } rndSource = rnd.baos.toByteArray(); System.out.print(String.format("Testing %d candidate", rndSource.length)); succeed = true; try { System.out.print(" - RSAstrength"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); rsaGenerator.generate(new RSAKeyGenerationParameters(64)); rsaGenerator.generate(new RSAKeyGenerationParameters(128)); System.out.print(" - RSAexp"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); rsaGenerator.generate(new RSAKeyGenerationParameters(64, BigInteger.valueOf(0x11))); System.out.print(" - RSAcertainty"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); rsaGenerator.generate(new RSAKeyGenerationParameters(64, 1)); System.out.print(" - DSA"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); dsaGenerator.generate(); dsaGenerator.generate(); System.out.print(" - FIPS186_2"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); dsaGenerator.generate(dsaParameterGenerator.generate()); System.out.print(" - FIPS186_3"); rndprov.setRandomSource(new FixedSecureRandom(rndSource)); dsaGenerator.generate( dsaParameterGenerator.generate(new DSAKeyParametersGenerationParameters(256, 28, 1))); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(" - Failed"); succeed = false; } } System.out.println(" - Succeed"); OutputStream os = new LineWrapperOutputStream(System.out, 100); os.write(Base64.encode(rndSource)); os.write("\n".getBytes()); }
From source file:org.xwiki.crypto.passwd.PBKDF2KeyDerivationFunctionTest.java
License:Open Source License
@Test public void deserializationTest() throws Exception { final KeyDerivationFunction serialFunction = (KeyDerivationFunction) SerializationUtils .deserialize(Base64.decode(this.serializedPBKDF2FunctionBase64.getBytes("US-ASCII"))); byte[] serialHash = serialFunction.deriveKey("password".getBytes("US-ASCII")); Assert.assertEquals(serializedPBKDF2FunctionHashOfPassword, new String(Base64.encode(serialHash))); }
From source file:org.xwiki.crypto.passwd.ScryptMemoryHardKeyDerivationFunctionTest.java
License:Open Source License
@Test public void scryptPBKDF2Test() throws Exception { PBKDF2KeyDerivationFunction sha256Pbkdf2 = new PBKDF2KeyDerivationFunction(new SHA256Digest()); byte[] out = sha256Pbkdf2.generateDerivedKey(scryptPBKDF2InputPassword.getBytes("US-ASCII"), Base64.decode(scryptPBKDF2InputSaltBase64.getBytes("US-ASCII")), 1, scryptPBKDF2DerivedKeyLength); Digest d = new SHA256Digest(); d.update(out, 0, out.length);/* w w w.j av a 2 s . com*/ byte[] hash = new byte[32]; d.doFinal(hash, 0); String outStr = new String(Base64.encode(hash), "US-ASCII"); Assert.assertTrue("Mismatch:\nExpecting: " + scryptPBKDF2OutputSha256Base64 + "\n Got: " + outStr, scryptPBKDF2OutputSha256Base64.equals(outStr)); }
From source file:org.xwiki.crypto.passwd.ScryptMemoryHardKeyDerivationFunctionTest.java
License:Open Source License
@Test public void blockMixTest() throws Exception { byte[] out = Base64.decode(this.blockMixInputBase64.getBytes("US-ASCII")); // Blockmix requires that memory be allocated. this.init(new byte[0], 2, 8, 1, 1); this.allocateMemory(true); this.blockMix(out); Digest d = new SHA256Digest(); d.update(out, 0, out.length);// ww w .j a v a 2s. c om byte[] hash = new byte[32]; d.doFinal(hash, 0); String outStr = new String(Base64.encode(hash), "US-ASCII"); Assert.assertEquals(this.blockMixOutputSha256Base64, outStr); }
From source file:org.xwiki.crypto.passwd.ScryptMemoryHardKeyDerivationFunctionTest.java
License:Open Source License
@Test public void salsa8Test() throws Exception { byte[] out = Base64.decode(this.salsa8InputBase64.getBytes("US-ASCII")); this.scryptSalsa8(out); String outStr = new String(Base64.encode(out), "US-ASCII"); Assert.assertEquals(this.salsa8OutputBase64, outStr); }
From source file:org.xwiki.crypto.passwd.ScryptMemoryHardKeyDerivationFunctionTest.java
License:Open Source License
@Test public void deserializationTest() throws Exception { final KeyDerivationFunction serialFunction = (KeyDerivationFunction) SerializationUtils .deserialize(Base64.decode(this.serializedScryptFunctionBase64.getBytes("US-ASCII"))); byte[] serialHash = serialFunction.deriveKey("password".getBytes()); Assert.assertEquals(serializedScryptFunctionHashOfPassword, new String(Base64.encode(serialHash))); }
From source file:phex.chat.ChatEngine.java
License:Open Source License
/** * Sends a chat message to the connected servent * * @param message the message to send/* ww w .j ava2s . co m*/ */ public void sendChatMessage(String message) { if (connection == null) { chatService.fireChatConnectionFailed(ChatEngine.this); } try { if (useEncodedStr) { String base64Str = new String(Base64.encode(message.getBytes())); connection.write(ByteBuffer.wrap((base64Str + '\n').getBytes())); } else { connection.write(ByteBuffer.wrap((message + '\n').getBytes())); } } catch (IOException exp) { NLogger.warn(ChatEngine.class, exp, exp); stopChat(); } }
From source file:piecework.security.concrete.ExampleBouncyCastleEncryptionService.java
License:Educational Community License
@Override public Secret encrypt(String text) throws InvalidCipherTextException, UnsupportedEncodingException, GeneralSecurityException { BufferedBlockCipher cipher = cipher(); SecretKeyRing secretKeyRing = keyProvider.getEncryptionKeyRing(null, null); byte[] key = secretKeyRing.getSecretKey().getEncoded(); byte[] iv = new byte[cipher.getBlockSize()]; // Generate a random initialization vector for this encryption random.nextBytes(iv);/*from w w w . j a v a 2s . c o m*/ cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] clear = text.getBytes("UTF-8"); int outputSize = cipher.getOutputSize(clear.length); byte[] hidden = new byte[outputSize]; int bytesProcessed = cipher.processBytes(clear, 0, clear.length, hidden, 0); bytesProcessed += cipher.doFinal(hidden, bytesProcessed); if (bytesProcessed != hidden.length) throw new GeneralSecurityException("Unable to correctly encrypt input data"); return new Secret.Builder().id(uuidGenerator.getNextId()).name(secretKeyRing.getKeyName()).date(new Date()) .ciphertext(Base64.encode(hidden)).iv(Base64.encode(iv)).build(); }