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);
}
}
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));
}
}
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;
}
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);
}
}
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");
}
Aggregations