@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); } }
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()); }
@Override public PemBlock encrypt(PemPub key, String content) throws MException { try { byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); ByteArrayOutputStream os = new ByteArrayOutputStream(); int length = key.getInt(PemBlock.LENGTH, 1024); int blockSize = length == 512 ? 53 : 117;
@Override public PemBlock encrypt(PemPub key, String content) throws MException { try { byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); ByteArrayOutputStream os = new ByteArrayOutputStream(); int length = key.getInt(PemBlock.LENGTH, 1024); int blockSize = length == 512 ? 53 : 117;
try { int aesLength = key.getInt("AesLength", 128); if (aesLength != 128 && aesLength != 256) { throw new MException("AES length not valid, use 128 or 256",aesLength); byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA");
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 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); } }
try { int aesLength = key.getInt("AesLength", 128); if (aesLength != 128 && aesLength != 256) { throw new MException("AES length not valid, use 128 or 256",aesLength); byte[] encKey = key.getBytesBlock(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
@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); } }