@Override public PrivateKeyPair createPrivateKeyPair(String base64) throws GeneralSecurityException { // convert to byte[] first because the two keys end at byte boundaries, but not at base64 char boundaries byte[] bytes = Base64.decode(base64); byte[] encryptionKeyBytes = Arrays.copyOfRange(bytes, 0, net.i2p.data.PrivateKey.KEYSIZE_BYTES); net.i2p.data.PrivateKey i2pEncryptionKey = new net.i2p.data.PrivateKey(encryptionKeyBytes); int signingKeyStart = net.i2p.data.PrivateKey.KEYSIZE_BYTES; int signingKeyEnd = signingKeyStart + net.i2p.data.SigningPrivateKey.KEYSIZE_BYTES; byte[] signingKeyBytes = Arrays.copyOfRange(bytes, signingKeyStart, signingKeyEnd); net.i2p.data.SigningPrivateKey i2pSigningKey = new net.i2p.data.SigningPrivateKey(signingKeyBytes); PrivateKeyPair keyPair = new PrivateKeyPair(); keyPair.encryptionKey = new ElGamalPrivateKey(i2pEncryptionKey); keyPair.signingKey = new DSAPrivateKey(i2pSigningKey); return 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); } }
@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); }