use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class ElGamalTest method testRoundTrip.
public void testRoundTrip() {
Object[] keys = KeyGenerator.getInstance().generatePKIKeypair();
PublicKey pubKey = (PublicKey) keys[0];
PrivateKey privKey = (PrivateKey) keys[1];
String msg = "Hello world";
Set toBeDelivered = new HashSet();
SessionKey key = _context.sessionKeyManager().getCurrentKey(pubKey);
if (key == null)
key = _context.sessionKeyManager().createSession(pubKey);
byte[] encrypted = _context.elGamalAESEngine().encrypt(DataHelper.getASCII(msg), pubKey, key, null, null, 64);
byte[] decrypted = null;
try {
decrypted = _context.elGamalAESEngine().decrypt(encrypted, privKey, _context.sessionKeyManager());
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
}
assertNotNull(decrypted);
String read = new String(decrypted);
assertEquals(msg, read);
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class ElGamalTest method testVerifyCompatability.
public void testVerifyCompatability() {
PublicKey pub = new PublicKey();
PrivateKey priv = new PrivateKey();
try {
pub.fromBase64(PUBLIC_KEY);
priv.fromBase64(PRIVATE_KEY);
} catch (DataFormatException dfe) {
dfe.printStackTrace();
fail();
}
for (int i = 0; i < ENCRYPTED.length; i++) {
byte[] enc = Base64.decode(ENCRYPTED[i]);
byte[] decrypted = _context.elGamalEngine().decrypt(enc, priv);
assertTrue(DataHelper.eq(decrypted, DataHelper.getASCII(UNENCRYPTED[i])));
}
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class TrustedUpdate method verify.
/**
* Verifies the DSA signature of a signed update file.
*
* @param signedFile The signed update file to check.
* @param publicKeyFile A file containing the public key to use for
* verification.
*
* @return <code>true</code> if the file has a valid signature, otherwise
* <code>false</code>.
*/
public boolean verify(String signedFile, String publicKeyFile) {
SigningPublicKey signingPublicKey = new SigningPublicKey();
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(signedFile);
signingPublicKey.readBytes(fileInputStream);
} catch (IOException ioe) {
if (_log.shouldLog(Log.WARN))
_log.warn("Unable to load the signature", ioe);
return false;
} catch (DataFormatException dfe) {
if (_log.shouldLog(Log.WARN))
_log.warn("Unable to load the signature", dfe);
return false;
} finally {
if (fileInputStream != null)
try {
fileInputStream.close();
} catch (IOException ioe) {
}
}
return verify(new File(signedFile), signingPublicKey);
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class TrustedUpdate method sign.
/**
* Uses the given private key to sign the given input file along with its
* version string using DSA. The output will be a signed update file where
* the first 40 bytes are the resulting DSA signature, the next 16 bytes are
* the input file's version string encoded in UTF-8 (padded with trailing
* <code>0h</code> characters if necessary), and the remaining bytes are the
* raw bytes of the input file.
*
* @param inputFile The file to be signed.
* @param signedFile The signed update file to write.
* @param privateKeyFile The name of the file containing the private key to
* sign <code>inputFile</code> with.
* @param version The version string of the input file. If this is
* longer than 16 characters it will be truncated.
*
* @return An instance of {@link net.i2p.data.Signature}, or
* <code>null</code> if there was an error.
*/
public Signature sign(String inputFile, String signedFile, String privateKeyFile, String version) {
FileInputStream fileInputStream = null;
SigningPrivateKey signingPrivateKey = new SigningPrivateKey();
try {
fileInputStream = new FileInputStream(privateKeyFile);
signingPrivateKey.readBytes(fileInputStream);
} catch (IOException ioe) {
if (_log.shouldLog(Log.WARN))
_log.warn("Unable to load the signing key", ioe);
return null;
} catch (DataFormatException dfe) {
if (_log.shouldLog(Log.WARN))
_log.warn("Unable to load the signing key", dfe);
return null;
} finally {
if (fileInputStream != null)
try {
fileInputStream.close();
} catch (IOException ioe) {
}
}
return sign(inputFile, signedFile, signingPrivateKey, version);
}
use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.
the class TrustedUpdate method addKey.
/**
* Duplicate keys or names rejected,
* except that duplicate empty names are allowed
* @param key 172 character base64 string
* @param name non-null but "" ok
* @since 0.7.12
* @return true if successful
*/
public boolean addKey(String key, String name) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Adding " + name + ": " + key);
SigningPublicKey signingPublicKey = new SigningPublicKey();
try {
// fromBase64() will throw a DFE if length is not right
signingPublicKey.fromBase64(key);
} catch (DataFormatException dfe) {
_log.error("Invalid signing key for " + name + " : " + key, dfe);
return false;
}
String oldName = _trustedKeys.get(signingPublicKey);
// already there?
if (name.equals(oldName))
return true;
if (oldName != null && !oldName.equals("")) {
_log.error("Key for " + name + " already stored for different name " + oldName + " : " + key);
return false;
}
if ((!name.equals("")) && _trustedKeys.containsValue(name)) {
_log.error("Key mismatch for " + name + ", spoof attempt? : " + key);
return false;
}
_trustedKeys.put(signingPublicKey, name);
return true;
}
Aggregations