List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider BouncyCastleProvider
public BouncyCastleProvider()
From source file:crypto.util.hash.Hash.java
public String generateHash(File file, Boolean replace) throws FileNotFoundException, IOException { String hash = null;/* ww w.j a va2 s . c o m*/ //converting file to bytes FileInputStream fis = null; fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); //no doubt here is 0 } byte[] bytes = bos.toByteArray(); Security.addProvider(new BouncyCastleProvider()); Digest messageDigestObj = new SHA1Digest(); byte[] digest = new byte[messageDigestObj.getDigestSize()]; messageDigestObj.update(bytes, 0, bytes.length); messageDigestObj.doFinal(digest, 0); hash = new String(Base64.encode(digest)); if (replace) { hash = hash.replace("/", "").replace("=", ""); } return hash; }
From source file:crypto.util.hash.Hash.java
public String generateHash(String data, Boolean replace) { String hash = null;// w w w .jav a2 s. c om Security.addProvider(new BouncyCastleProvider()); Digest messageDigestObj = new SHA1Digest(); byte[] digest = new byte[messageDigestObj.getDigestSize()]; messageDigestObj.update(data.getBytes(), 0, data.getBytes().length); messageDigestObj.doFinal(digest, 0); hash = new String(Base64.encode(digest)); if (replace) { hash = hash.replace("/", "").replace("=", ""); } return hash; }
From source file:crypto.util.hash.Hash.java
public String generateHashHex(String data) { String hash = null;/*from w w w .j a v a2s .co m*/ Security.addProvider(new BouncyCastleProvider()); Digest messageDigestObj = new SHA1Digest(); byte[] digest = new byte[messageDigestObj.getDigestSize()]; messageDigestObj.update(data.getBytes(), 0, data.getBytes().length); messageDigestObj.doFinal(digest, 0); hash = new String(Hex.encode(digest)); return hash; }
From source file:cryptoexplorer.Services.java
License:Apache License
@POST @Path("/loadbc") public String loadBC() { addProvider(new BouncyCastleProvider()); return "ok"; }
From source file:cryptonite.Main.java
License:MIT License
/** * Initializes the Bouncy Castle Provider * @return true if the initialization suceeded *//*from ww w. j a v a 2 s . com*/ static boolean initializeProvider() { Security.addProvider(new BouncyCastleProvider()); return Security.getProvider(BOUNCY_CASTLE_PROVIDER) != null; }
From source file:crypttools.PGPCryptoBC.java
License:Open Source License
public String signData(String data, String passphrase) throws Exception { Security.addProvider(new BouncyCastleProvider()); InputStream keyInputStream = new ByteArrayInputStream(this.armoredSecretKey); PGPSecretKey pgpSecretKey = readSecretKey(keyInputStream); PGPPrivateKey pgpPrivateKey = pgpSecretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase.toCharArray())); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(pgpSecretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1) .setProvider("BC")); signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, pgpPrivateKey); @SuppressWarnings("unchecked") Iterator<String> it = pgpSecretKey.getPublicKey().getUserIDs(); if (it.hasNext()) { PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, it.next()); signatureGenerator.setHashedSubpackets(spGen.generate()); }// www . jav a2s.com ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); OutputStream outputStream = new ArmoredOutputStream(byteOutputStream); PGPCompressedDataGenerator compressDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZLIB); BCPGOutputStream bcOutputStream = new BCPGOutputStream(compressDataGenerator.open(outputStream)); signatureGenerator.generateOnePassVersion(false).encode(bcOutputStream); PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); File fileToSign = File.createTempFile("temp", ".scrap"); FileUtils.writeStringToFile(fileToSign, data); OutputStream literalDataGenOutputStream = literalDataGenerator.open(bcOutputStream, PGPLiteralData.BINARY, fileToSign); FileInputStream fis = new FileInputStream(fileToSign); int ch; while ((ch = fis.read()) >= 0) { literalDataGenOutputStream.write(ch); signatureGenerator.update((byte) ch); } literalDataGenerator.close(); fis.close(); signatureGenerator.generate().encode(bcOutputStream); compressDataGenerator.close(); outputStream.close(); fileToSign.delete(); return new String(byteOutputStream.toByteArray(), "UTF-8"); }
From source file:crypttools.PGPCryptoBC.java
License:Open Source License
public boolean validateData(String data, String publicKey) throws Exception { Security.addProvider(new BouncyCastleProvider()); File fileToVerify = File.createTempFile("temp", ".privateScrap"); FileUtils.writeStringToFile(fileToVerify, data); File publicKeyFile = File.createTempFile("temp", ".publicScrap"); // Creates an exception // System.out.println(this.armoredPublicKey); // String armoredKeyString = getPublicKey(); // System.out.println(armoredKeyString); FileUtils.writeStringToFile(publicKeyFile, publicKey); //FileUtils.writeStringToFile(publicKeyFile, new String(this.armoredPublicKey, "UTF-8")); try {// w ww . j a va 2 s. c om InputStream in = PGPUtil.getDecoderStream(new FileInputStream(fileToVerify)); PGPObjectFactory pgpObjFactory = new PGPObjectFactory(in); PGPCompressedData compressedData = (PGPCompressedData) pgpObjFactory.nextObject(); //Get the signature from the file pgpObjFactory = new PGPObjectFactory(compressedData.getDataStream()); PGPOnePassSignatureList onePassSignatureList = (PGPOnePassSignatureList) pgpObjFactory.nextObject(); PGPOnePassSignature onePassSignature = onePassSignatureList.get(0); //Get the literal data from the file PGPLiteralData pgpLiteralData = (PGPLiteralData) pgpObjFactory.nextObject(); InputStream literalDataStream = pgpLiteralData.getInputStream(); InputStream keyIn = new FileInputStream(publicKeyFile); PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); PGPPublicKey key = pgpRing.getPublicKey(onePassSignature.getKeyID()); FileOutputStream literalDataOutputStream = new FileOutputStream(pgpLiteralData.getFileName()); onePassSignature.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), key); int ch; while ((ch = literalDataStream.read()) >= 0) { onePassSignature.update((byte) ch); literalDataOutputStream.write(ch); } literalDataOutputStream.close(); //Get the signature from the written out file PGPSignatureList p3 = (PGPSignatureList) pgpObjFactory.nextObject(); PGPSignature signature = p3.get(0); //Verify the two signatures boolean valid = onePassSignature.verify(signature); return valid; } catch (Exception e) { System.out.println("Got an Exception: " + e.getMessage()); return false; //do something clever with the exception } finally { fileToVerify.delete(); publicKeyFile.delete(); } }
From source file:crypttools.PGPCryptoBC.java
License:Open Source License
public String signDataDetached(String data, String passphrase) throws Exception { Security.addProvider(new BouncyCastleProvider()); InputStream keyInputStream = new ByteArrayInputStream(this.armoredSecretKey); PGPSecretKey pgpSecretKey = readSecretKey(keyInputStream); PGPPrivateKey pgpPrivateKey = pgpSecretKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passphrase.toCharArray())); PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(pgpSecretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1) .setProvider("BC")); signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, pgpPrivateKey); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); OutputStream outputStream = new ArmoredOutputStream(byteOutputStream); BCPGOutputStream bOut = new BCPGOutputStream(outputStream); InputStream fIn = IOUtils.toInputStream(data, "UTF-8"); int ch;/* w ww .jav a 2s .co m*/ while ((ch = fIn.read()) >= 0) { signatureGenerator.update((byte) ch); } fIn.close(); signatureGenerator.generate().encode(bOut); outputStream.close(); keyInputStream.close(); return new String(byteOutputStream.toByteArray(), "UTF-8"); }
From source file:crypttools.PGPCryptoBC.java
License:Open Source License
public boolean verifyFileDetached(String data, String signature, String publicKey) throws Exception { Security.addProvider(new BouncyCastleProvider()); InputStream keyInputStream = new BufferedInputStream(IOUtils.toInputStream(publicKey, "UTF-8")); InputStream sigInputStream = PGPUtil .getDecoderStream(new BufferedInputStream(IOUtils.toInputStream(signature, "UTF-8"))); PGPObjectFactory pgpObjFactory = new PGPObjectFactory(sigInputStream); PGPSignatureList pgpSigList = null;/*from w ww .j ava 2 s. c o m*/ Object obj = pgpObjFactory.nextObject(); if (obj instanceof PGPCompressedData) { PGPCompressedData c1 = (PGPCompressedData) obj; pgpObjFactory = new PGPObjectFactory(c1.getDataStream()); pgpSigList = (PGPSignatureList) pgpObjFactory.nextObject(); } else { pgpSigList = (PGPSignatureList) obj; } PGPPublicKeyRingCollection pgpPubRingCollection = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(keyInputStream)); InputStream fileInputStream = new BufferedInputStream(IOUtils.toInputStream(data, "UTF-8")); PGPSignature sig = pgpSigList.get(0); PGPPublicKey pubKey = pgpPubRingCollection.getPublicKey(sig.getKeyID()); sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider("BC"), pubKey); int ch; while ((ch = fileInputStream.read()) >= 0) { sig.update((byte) ch); } fileInputStream.close(); keyInputStream.close(); sigInputStream.close(); if (sig.verify()) { return true; } else { return false; } }
From source file:cvc.TestCVCertificate.java
License:Open Source License
protected void setUp() throws Exception { // Install Bouncy Castle as security provider Security.addProvider(new BouncyCastleProvider()); }