@Override public void foundPublicKey(PemBlock block) { String id = block.getString(PemBlock.IDENT, null); if (id == null) return; keys.put(id, block); }
@Override public String decrypt(PemPriv key, PemBlock encoded, String passphrase) throws MException { try { byte[] xkey = key.getBytesBlock(); byte[] data = encoded.getBytesBlock(); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(xkey, "AES"); c.init(Cipher.DECRYPT_MODE, k); byte[] enc = c.doFinal(data); String stringEncoding = encoded.getString(PemBlock.STRING_ENCODING, "utf-8"); return new String(enc, stringEncoding); } catch (Throwable t) { throw new MException(t); } }
public PemBlock find(String name) { for (PemBlock block : this) { if (name.equals(block.getName())) { return block; } } return null; } public PemBlock find(String name, String filter ) throws MException {
@Override public boolean validate(PemPub key, String text, PemBlock sign) throws MException { try { byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN"); PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance("SHA1withDSA", "SUN"); sig.initVerify(pubKey); byte[] buffer = text.getBytes(); sig.update(buffer, 0, buffer.length); byte[] sigToVerify = sign.getBytesBlock(); return sig.verify(sigToVerify); } catch (Exception e) { throw new MException(e); } }
cipher.init(Cipher.DECRYPT_MODE, privKey); String aesEncKey = encoded.getString("AesKey"); byte[] b = Base64.decode(aesEncKey); byte[] data = encoded.getBytesBlock(); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(aesKey, "AES"); byte[] enc = c.doFinal(data); String stringEncoding = encoded.getString(PemBlock.STRING_ENCODING, "utf-8"); return new String(enc, stringEncoding);
public static boolean isSign(PemBlock block) { return PemBlock.BLOCK_SIGN.equals(block.getName()); }
@Override public boolean validate(PemPub key, String text, PemBlock sign) throws MException { try { byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC"); PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance("SHA512WITHECDSA", "BC"); sig.initVerify(pubKey); byte[] buffer = text.getBytes(); sig.update(buffer, 0, buffer.length); byte[] sigToVerify = sign.getBytesBlock(); return sig.verify(sigToVerify); } catch (Exception e) { throw new MException(e); } }
cipher.init(Cipher.DECRYPT_MODE, privKey); String aesEncKey = encoded.getString("AesKey"); byte[] b = Base64.decode(aesEncKey); byte[] data = encoded.getBytesBlock(); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(aesKey, "AES"); byte[] enc = c.doFinal(data); String stringEncoding = encoded.getString(PemBlock.STRING_ENCODING, "utf-8"); return new String(enc, stringEncoding);
@Override public void foundPrivateKey(PemBlock block) { String id = block.getString(PemBlock.IDENT, null); if (id == null) return; keys.put(id, block); }
public static boolean isHash(PemBlock block) { return PemBlock.BLOCK_HASH.equals(block.getName()); }
cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] b = encoded.getBytesBlock(); ByteArrayOutputStream os = new ByteArrayOutputStream(); String stringEncoding = encoded.getString(PemBlock.STRING_ENCODING, "utf-8"); return new String(os.toByteArray(), stringEncoding);
@Override public String getPrivateIdForPublicKeyId(String pubId) throws CryptException { PemBlock pub = keys.get(pubId); if (pub == null) return null; return pub.getString(PemBlock.PRIV_ID, null); }
public static boolean isContent(PemBlock block) { return PemBlock.BLOCK_CONTENT.equals(block.getName()); }
int blockSize = Math.max(length / 1024 * 128, 64); byte[] b = encoded.getBytesBlock(); ByteArrayOutputStream os = new ByteArrayOutputStream(); String stringEncoding = encoded.getString(PemBlock.STRING_ENCODING, "utf-8"); return new String(os.toByteArray(), stringEncoding);
public PemEntry(PemBlock block) { String str = block.getString(PemBlock.IDENT, null); if (str == null) id = UUID.randomUUID(); description = block.getString(PemBlock.DESCRIPTION, ""); String method = block.getString(PemBlock.METHOD, "").toUpperCase(); if (PemUtil.isPubKey(block)) { if (method.contains("RSA"))
public static boolean isCipher(PemBlock block) { return PemBlock.BLOCK_CIPHER.equals(block.getName()); }
public static boolean isPrivKey(PemBlock block) { return PemBlock.BLOCK_PRIV.equals(block.getName()); }
public static boolean isPubKey(PemBlock block) { return PemBlock.BLOCK_PUB.equals(block.getName()); }