public PemBlockList(String string) { while(true) { try { int p = string.indexOf("-----BEGIN "); if (p < 0) break; PemBlockModel next = new PemBlockModel().parse(string); add(next); string = next.getRest(); } catch (ParseException e) { MLogUtil.log().t(e); break; } } }
public static void prepareSymmetricCipherOut(PemPub key, PemBlockModel out, String name, String stringEncoding) throws MException { out.set(PemBlock.METHOD, name); out.set(PemBlock.SYMMETRIC, true); if (stringEncoding != null) out.set(PemBlock.STRING_ENCODING, stringEncoding); if (key.isProperty(PemBlock.IDENT)) out.set(PemBlock.KEY_ID, key.getString(PemBlock.IDENT)); out.set(PemBlock.CREATED, new Date()); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("-----BEGIN ").append(getName()).append("-----\n"); for (java.util.Map.Entry<String, Object> item : entrySet()) { String key = item.getKey().trim(); sb.append(key).append(": "); sb.append(getEncodedBlock()); sb.append("\n\n"); sb.append("-----END ").append(getName()).append("-----\n"); return sb.toString();
public static PemBlock parse(String entry) throws ParseException { return new PemBlockModel().parse(entry); }
public static PemPriv cipherPrivFromString(String str) throws ParseException, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPriv key = MVaultUtil.adaptTo(entry, PemPriv.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':').toUpperCase().trim(); String key = MString.afterIndex(str, ':').trim(); return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, name).setBlock(key); }
PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_CIPHER, encryptedData); CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding); out.setInt("AesLength", aesLength); out.setString("AesKey", Base64.encode(aesKeyEncoded)); return out;
if (n.contains("\n") || n.contains("\r")) throw new ParseException("name contains line break",n); setName(n); block = block.substring(p+5); String endMark = "-----END " + getName()+"-----"; p = block.indexOf(endMark); if (p < 0) throw new ParseException("end of block not found",getName()); } else if (line.startsWith(" ") && lastKey != null) { setString(lastKey, getString(lastKey,"") + line.substring(1)); } else { int pp = line.indexOf(':'); lastKey = line.substring(0,pp).trim(); String value = line.substring(pp+1).trim(); setString(lastKey, value);
@Override public PemBlock encrypt(PemPub key, String content) throws MException { try { byte[] xkey = key.getBytesBlock(); String stringEncoding = "utf-8"; byte[] dataToSend = content.getBytes(stringEncoding); Cipher c = Cipher.getInstance("AES"); SecretKeySpec k = new SecretKeySpec(xkey, "AES"); c.init(Cipher.ENCRYPT_MODE, k); byte[] encryptedData = c.doFinal(dataToSend); PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_CIPHER, encryptedData); CryptUtil.prepareSymmetricCipherOut(key, out, getName(), stringEncoding); return out; } catch (Throwable t) { throw new MException(t); } }
@SuppressWarnings("unchecked") public <T extends PemBlockModel> T set(String key, Object value) { put(key, value); return (T) this; }
public static PemPriv signPrivFromString(String str) throws Exception, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPriv key = MVaultUtil.adaptTo(entry, PemPriv.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_SIGN).set(PemBlock.METHOD, name).setBlock(key); }
PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_CIPHER, encryptedData); CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding); out.setInt("AesLength", aesLength); out.setString("AesKey", Base64.encode(aesKeyEncoded)); return out;
@Override public PemBlock sign(PemPriv key, String text, String passphrase) throws MException { try { byte[] encKey = key.getBytesBlock(); if (MString.isSet(passphrase)) encKey = Blowfish.decrypt(encKey, passphrase); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN"); PrivateKey privKey = keyFactory.generatePrivate(privKeySpec); Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initSign(privKey); byte[] buffer = text.getBytes(); dsa.update(buffer, 0, buffer.length); byte[] realSig = dsa.sign(); PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_SIGN, realSig); CryptUtil.prepareSignOut(key, out, getName()); return out; } catch (Exception e) { throw new MException(e); } }
public static PemKey toKey(String key) throws ParseException { return new PemKey(new PemBlockModel().parse(key)); }
public static PemPub signPubFromString(String str) throws NotSupportedException, IOException, ParseException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPub key = MVaultUtil.adaptTo(entry, PemPub.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_SIGN).set(PemBlock.METHOD, name).setBlock(key); }
public static void prepareCipherOut(PemPub key, PemBlockModel out, String name, String stringEncoding) throws MException { out.set(PemBlock.METHOD, name); if (stringEncoding != null) out.set(PemBlock.STRING_ENCODING, stringEncoding); if (key.isProperty(PemBlock.IDENT)) out.set(PemBlock.PUB_ID, key.getString(PemBlock.IDENT)); if (key.isProperty(PemBlock.PRIV_ID)) out.set(PemBlock.PRIV_ID, key.getString(PemBlock.PRIV_ID)); out.set(PemBlock.CREATED, new Date()); }
PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_CIPHER, os.toByteArray()); CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding);
public static PemPub cipherPubFromString(String str) throws ParseException, NotSupportedException, IOException { if (MValidator.isUUID(str)) { MVault vault = MVaultUtil.loadDefault(); VaultEntry entry = vault.getEntry(UUID.fromString(str)); PemPub key = MVaultUtil.adaptTo(entry, PemPub.class); return key; } if (isPemBlock(str)) { PemBlockModel block = new PemBlockModel().parse(str); // return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, block.getString(PemBlock.METHOD,"")).setBlock(block.getEncodedBlock()); return new PemKey(block); } String name = MString.beforeIndex(str, ':'); String key = MString.afterIndex(str, ':'); return new PemKey(PemBlock.BLOCK_CIPHER).set(PemBlock.METHOD, name).setBlock(key); }
public static void prepareSignOut(PemPriv key, PemBlockModel out, String name) { out.set(PemBlock.METHOD,name); if (key.isProperty(PemBlock.IDENT)) out.set(PemBlock.PRIV_ID, key.getProperty(PemBlock.IDENT)); if (key.isProperty(PemBlock.PUB_ID)) out.set(PemBlock.PUB_ID, key.getProperty(PemBlock.PUB_ID)); out.set(PemBlock.CREATED, new Date()); }
PemBlockModel out = new PemBlockModel(PemBlock.BLOCK_CIPHER, os.toByteArray()); CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding);