/** Only accepts <code>ElGamalPublicKey</code>s. */ @Override public byte[] encrypt(byte[] data, PublicKey key) throws GeneralSecurityException { ElGamalPublicKey elGamalKey = castToElGamal(key); net.i2p.data.PublicKey i2pPublicKey = elGamalKey.getI2PKey(); return Util.encrypt(data, i2pPublicKey); }
@Override public PrivateKeyPair createPrivateKeyPair(byte[] bytes) throws GeneralSecurityException { return createPrivateKeyPair(Base64.encode(bytes)); }
@Override public PublicKeyPair createPublicKeyPair(byte[] bytes) throws GeneralSecurityException { return createPublicKeyPair(Base64.encode(bytes)); }
private static void init() { instances = Collections.synchronizedList(new ArrayList<CryptoImplementation>()); Log log = new Log(CryptoFactory.class); try { instances.add(new ElGamal2048_DSA1024()); } catch (GeneralSecurityException e) { log.error("Error creating ElGamal2048_DSA1024.", e); } try { instances.add(new ECDH256_ECDSA256()); instances.add(new ECDH521_ECDSA521()); } catch (GeneralSecurityException e) { log.error("Error creating ECDH256_ECDSA256 or ECDH521_ECDSA521.", e); } try { instances.add(new NTRUEncrypt1087_GMSS512()); } catch (GeneralSecurityException e) { log.error("Error creating NTRUEncrypt1087_GMSS512.", e); } } }
/** Only accepts <code>DSAPublicKey</code>s. */ @Override public boolean verify(byte[] data, byte[] signature, PublicKey key) throws GeneralSecurityException { DSAPublicKey dsaKey = castToDSA(key); Signature signatureObj = new Signature(signature); boolean valid = DSAEngine.getInstance().verifySignature(signatureObj, data, dsaKey.getI2PKey()); return valid; }
@Override public KeyPair generateSigningKeyPair() throws KeyException { I2PSession i2pSession = createI2PSession(); net.i2p.data.SigningPublicKey i2pPublicKey = i2pSession.getMyDestination().getSigningPublicKey(); net.i2p.data.SigningPrivateKey i2pPrivateKey = i2pSession.getPrivateKey(); PublicKey publicKey = new DSAPublicKey(i2pPublicKey); PrivateKey privateKey = new DSAPrivateKey(i2pPrivateKey); return new KeyPair(publicKey, privateKey); }
@Override public String toBase64(PrivateKeyPair keyPair) throws GeneralSecurityException { return Base64.encode(toByteArray(keyPair)); }
TestIdentity elGamal2048 = new TestIdentity(elGamal2048PublicKeyPair, elGamal2048PrivateKeyPair, new ElGamal2048_DSA1024()); identities.add(elGamal2048);
/** Only accepts <code>DSAPrivateKey</code>s. */ @Override public byte[] sign(byte[] data, PrivateKey privateKey, KeyUpdateHandler keyupdateHandler) throws GeneralSecurityException { DSAPrivateKey dsaKey = castToDSA(privateKey); Signature signature = DSAEngine.getInstance().sign(data, dsaKey.getI2PKey()); return signature.toByteArray(); }
@Override public KeyPair generateEncryptionKeyPair() throws KeyException { I2PSession i2pSession = createI2PSession(); net.i2p.data.PublicKey i2pPublicKey = i2pSession.getMyDestination().getPublicKey(); net.i2p.data.PrivateKey i2pPrivateKey = i2pSession.getDecryptionKey(); PublicKey publicKey = new ElGamalPublicKey(i2pPublicKey); PrivateKey privateKey = new ElGamalPrivateKey(i2pPrivateKey); return new KeyPair(publicKey, privateKey); }
@Override public String toBase64(PublicKeyPair keyPair) throws GeneralSecurityException { return Base64.encode(toByteArray(keyPair)); }
/** Only accepts <code>ElGamalPrivateKey</code>s. The public key is not used. */ @Override public byte[] decrypt(byte[] data, PublicKey publicKey, PrivateKey privateKey) throws GeneralSecurityException { if (data == null) return null; ElGamalPrivateKey elGamalKey = castToElGamal(privateKey); try { net.i2p.data.PrivateKey i2pPrivateKey = elGamalKey.getI2PKey(); return Util.decrypt(data, i2pPrivateKey); } catch (DataFormatException e) { byte[] shortenedData = data.length>10?Arrays.copyOf(data, 10):data; throw new KeyException("Can't decrypt data: " + Arrays.toString(shortenedData) + " (only the first 10 elements are shown).", e); } }