Search in sources :

Example 96 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class ElGamalTest method testElGamal.

public void testElGamal() {
    for (int i = 0; i < 2; i++) {
        Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
        PublicKey pubKey = (PublicKey) keys[0];
        PrivateKey privKey = (PrivateKey) keys[1];
        SessionKey key = KeyGenerator.getInstance().generateSessionKey();
        ByteArrayOutputStream elgSrc = new ByteArrayOutputStream(256);
        try {
            key.writeBytes(elgSrc);
        } catch (DataFormatException dfe) {
            dfe.printStackTrace();
            fail();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail();
        }
        byte[] preIV = new byte[32];
        RandomSource.getInstance().nextBytes(preIV);
        try {
            elgSrc.write(preIV);
            elgSrc.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail();
        }
        byte[] elgEncr = _context.elGamalEngine().encrypt(elgSrc.toByteArray(), pubKey);
        byte[] elgDecr = _context.elGamalEngine().decrypt(elgEncr, privKey);
        ByteArrayInputStream bais = new ByteArrayInputStream(elgDecr);
        SessionKey nk = new SessionKey();
        try {
            nk.readBytes(bais);
        } catch (DataFormatException dfe) {
            dfe.printStackTrace();
            fail();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail();
        }
        byte[] postpreIV = new byte[32];
        int read = 0;
        try {
            read = bais.read(postpreIV);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail();
        }
        assertEquals(read, postpreIV.length);
        assertTrue(DataHelper.eq(preIV, postpreIV));
        assertEquals(key, nk);
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 97 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class ElGamalTest method testLoop.

public void testLoop() {
    for (int i = 0; i < 5; i++) {
        Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
        PublicKey pubKey = (PublicKey) keys[0];
        PrivateKey privKey = (PrivateKey) keys[1];
        byte[] msg = new byte[400];
        RandomSource.getInstance().nextBytes(msg);
        SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
        if (key == null)
            key = _context.sessionKeyManager().createSession(pubKey);
        byte[] encrypted = _context.elGamalAESEngine().encrypt(msg, pubKey, key, null, null, 1024);
        byte[] decrypted = null;
        try {
            decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
        } catch (DataFormatException dfe) {
            dfe.printStackTrace();
            fail();
        }
        assertTrue(DataHelper.eq(msg, decrypted));
    }
}
Also used : PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) PublicKey(net.i2p.data.PublicKey) SessionKey(net.i2p.data.SessionKey)

Example 98 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class BlockfileNamingService method readProperties.

/**
 * Same as DataHelper.readProperties, UTF-8, unsorted,
 * except that values may up to 4K bytes.
 *
 * Throws DataFormatException on duplicate key
 *
 * @param in stream to read the mapping from
 * @throws DataFormatException if the format is invalid
 * @throws IOException if there is a problem reading the data
 * @return a Properties
 * @since 0.9.26
 */
public static Properties readProperties(ByteArrayInputStream in) throws DataFormatException, IOException {
    Properties props = new Properties();
    int size = (int) DataHelper.readLong(in, 2);
    // this doesn't prevent reading past the end on corruption
    int ignore = in.available() - size;
    while (in.available() > ignore) {
        String key = DataHelper.readString(in);
        int b = in.read();
        if (b != '=')
            throw new DataFormatException("Bad key " + b);
        String val = readLongString(in);
        b = in.read();
        if (b != ';')
            throw new DataFormatException("Bad value");
        Object old = props.put(key, val);
        if (old != null)
            throw new DataFormatException("Duplicate key " + key);
    }
    return props;
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Properties(java.util.Properties)

Example 99 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class BlockfileNamingService method writeProperties.

/**
 * Same as DataHelper.writeProperties, UTF-8, unsorted,
 * except that values may up to 4K bytes.
 *
 * @param props source may be null
 * @throws DataFormatException if any key string is over 255 bytes long,
 *                             if any value string is over 4096 bytes long, or if the total length
 *                             (not including the two length bytes) is greater than 65535 bytes.
 * @since 0.9.26
 */
private static void writeProperties(ByteArrayOutputStream rawStream, Properties p) throws DataFormatException, IOException {
    if (p != null && !p.isEmpty()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(p.size() * 32);
        for (Map.Entry<Object, Object> entry : p.entrySet()) {
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            DataHelper.writeStringUTF8(baos, key);
            baos.write('=');
            writeLongStringUTF8(baos, val);
            baos.write(';');
        }
        if (baos.size() > 65535)
            throw new DataFormatException("Properties too big (65535 max): " + baos.size());
        byte[] propBytes = baos.toByteArray();
        DataHelper.writeLong(rawStream, 2, propBytes.length);
        rawStream.write(propBytes);
    } else {
        DataHelper.writeLong(rawStream, 2, 0);
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 100 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class BlockfileNamingService method readLongString.

/**
 * Same as DataHelper.readString, except that
 * strings up to 4K bytes are allowed.
 * Format is: one-byte length + data, or 0xff + two-byte length + data
 *
 * @param in stream to read from
 * @throws DataFormatException if the stream doesn't contain a validly formatted string
 * @throws EOFException if there aren't enough bytes to read the string
 * @throws IOException if there is an IO error reading the string
 * @return UTF-8 string
 */
private static String readLongString(ByteArrayInputStream in) throws DataFormatException, IOException {
    int size = in.read();
    if (size < 0)
        throw new EOFException("EOF reading string");
    if (size == 0xff) {
        size = (int) DataHelper.readLong(in, 2);
        if (size > MAX_VALUE_LENGTH)
            throw new DataFormatException(MAX_VALUE_LENGTH + " max, but this is " + size);
    }
    if (size == 0)
        return "";
    byte[] raw = new byte[size];
    int read = DataHelper.read(in, raw);
    if (read != size)
        throw new EOFException("EOF reading string");
    return new String(raw, "UTF-8");
}
Also used : DataFormatException(net.i2p.data.DataFormatException) EOFException(java.io.EOFException)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8