/** * decrypt the passed in message stream * * @param encrypted The message to be decrypted. * @param password Pass phrase (key) * @return Clear text as a byte array. I18N considerations are not handled * by this routine * @throws IOException * @throws PGPException * @throws NoSuchProviderException */ public static byte[] decrypt(byte[] encrypted, String keyIn, char[] password) throws IOException, PGPException, NoSuchProviderException { return decrypt (encrypted, new FileInputStream(keyIn), password); }
/** * Simple PGP encryptor between byte[]. * * @param clearData The test to be encrypted * @param keyRing public key ring input stream * @param fileName File name. This is used in the Literal Data Packet (tag 11) * which is really only important if the data is to be related to * a file to be recovered later. Because this routine does not * know the source of the information, the caller can set * something here for file name use that will be carried. If this * routine is being used to encrypt SOAP MIME bodies, for * example, use the file name from the MIME type, if applicable. * Or anything else appropriate. * @param withIntegrityCheck true if an integrity packet is to be included * @param armor true for ascii armor * @param ids destination ids * @return encrypted data. * @throws IOException * @throws PGPException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException */ public static byte[] encrypt(byte[] clearData, String keyRing, String fileName, boolean withIntegrityCheck, boolean armor, String... ids) throws IOException, PGPException, NoSuchProviderException, NoSuchAlgorithmException { return encrypt (clearData, new FileInputStream(keyRing), fileName, withIntegrityCheck, armor, ids); }
private static String getLicensee() { String s = null; try { s = PGPHelper.getLicensee(); } catch (IOException ignored) { // NOPMD: ignore } return s; } private void parseCmdLine (String[] args) {
public static boolean checkSignature() { boolean ok = false; try (InputStream is = getLicenseeStream()) { InputStream ks = Q2.class.getClassLoader().getResourceAsStream(PUBRING); PGPPublicKey pk = PGPHelper.readPublicKey(ks, SIGNER); ok = verifySignature(is, pk); } catch (Exception ignored) { // NOPMD: signature isn't good } return ok; }
@Test public void testEncryptDecrypt() throws Exception { String s = "The quick brown fox jumps over the lazy dog 0123456789"; byte[] cypertext = PGPHelper.encrypt( s.getBytes(StandardCharsets.UTF_8), "src/dist/cfg/demo.pub", "abc.txt", true, true, "demo@jpos.org"); byte[] clearText = PGPHelper.decrypt(cypertext, "src/dist/cfg/demo.priv", "demo".toCharArray()); assertEquals(s, new String(clearText, StandardCharsets.UTF_8)); } }
int ch; try (InputStream in = getLicenseeStream()){ InputStream ks = Q2.class.getClassLoader().getResourceAsStream(PUBRING); PGPPublicKey pk = readPublicKey(ks, SIGNER); ArmoredInputStream ain = new ArmoredInputStream(in, true); ByteArrayOutputStream out = new ByteArrayOutputStream();
private void logVersion () { long now = System.currentTimeMillis(); if (now - lastVersionLog > 3600000L) { LogEvent evt = getLog().createLogEvent("version"); evt.addMessage(getVersionString()); Logger.log(evt); lastVersionLog = now; while ((PGPHelper.checkLicense() & 0xE0000) != 0) relax(); } } private void setExit (boolean exit) {
public static String getLicensee() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (InputStream is = getLicenseeStream()) { if (is != null) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintStream p = new PrintStream(baos); p.println(); p.println(); while (br.ready()) p.println(br.readLine()); } } return baos.toString(); }
fileName = PGPLiteralData.CONSOLE; PGPPublicKey[] encKeys = readPublicKeys(keyRing, ids); ByteArrayOutputStream encOut = new ByteArrayOutputStream(); OutputStream out = encOut;
sKey = findSecretKey(pgpSec, pbe.getKeyID(), password);
public static String getVersionString() { String appVersionString = getAppVersionString(); int l = PGPHelper.checkLicense(); String sl = l > 0 ? " " + Integer.toString(l,16) : ""; String vs = null; if (appVersionString != null) { vs = String.format ("jPOS %s %s/%s%s (%s)%n%s%s", getVersion(), getBranch(), getRevision(), sl, getBuildTimestamp(), appVersionString, getLicensee() ); } else { vs = String.format("jPOS %s %s/%s%s (%s) %s", getVersion(), getBranch(), getRevision(), sl, getBuildTimestamp(), getLicensee() ); } // if ((l & 0xE0000) > 0) // throw new IllegalAccessError(vs); return vs; }
private SecretKey getKey (UUID keyId, char[] passPhrase) throws Exception { if (passPhrase == null && unlock == null) throw new SecurityException("Passphrase not available"); passPhrase = passPhrase != null ? passPhrase : unlock.get().toCharArray(); String v = DB.execWithTransaction(db -> { SysConfigManager mgr = new SysConfigManager(db, "key."); return mgr.get(keyId.toString(), null); }); if (v == null) { throw new SecurityException("Invalid key"); } byte[] key = PGPHelper.decrypt( v.getBytes(), privKeyRing, passPhrase ); return new SecretKeySpec(key, 0, key.length, "AES"); }
private byte[] pgpEncrypt(String id, byte[] clearText) throws NoSuchAlgorithmException, PGPException, NoSuchProviderException, IOException { return PGPHelper.encrypt(clearText, pubKeyRing, id, true, true, cfg.getAll("custodian")); }
/** * Unlock the CryptoService */ public boolean unlock (Supplier<String> passwordSupplier) { try { if (isLocked()) { // attempt encrypt/decrypt UUID id = UUID.randomUUID(); SecretKey sk = generateKey(); byte[] b = pgpEncrypt(id.toString(), sk.getEncoded()); PGPHelper.decrypt(b, privKeyRing, passwordSupplier.get().toCharArray()); sem.acquire(); this.unlock = passwordSupplier; sem.release(); } return true; } catch (Exception e) { getLog().warn(e); return false; } }