@Test public void testShortData() throws Exception{ PublicKey publicKey = new PublicKey(); byte data[] = new byte[56]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i); exception.expect(IllegalArgumentException.class); exception.expectMessage("Bad data length: 56; required: " + PublicKey.KEYSIZE_BYTES); publicKey.setData(data); publicKey.writeBytes(new ByteArrayOutputStream()); }
@Test public void testBase64Constructor() throws Exception{ PublicKey publicKey = new PublicKey(); byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%56); publicKey.setData(data); PublicKey key2 = new PublicKey(publicKey.toBase64()); assertEquals(publicKey, key2); }
public DataStructure createDataStructure() throws DataFormatException { PublicKey publicKey = new PublicKey(); byte data[] = new byte[PublicKey.KEYSIZE_BYTES]; for (int i = 0; i < data.length; i++) data[i] = (byte)(i%16); publicKey.setData(data); return publicKey; } public DataStructure createStructureToRead() { return new PublicKey(); }
@Test public void testNullData() throws Exception{ PublicKey publicKey = new PublicKey(); publicKey.toString(); exception.expect(DataFormatException.class); exception.expectMessage("No data to write out"); publicKey.writeBytes(new ByteArrayOutputStream()); }
private void syncPublicKey(File keyDir) { DataStructure ds; File keyFile = new File(keyDir, KEYFILE_PUBLIC_ENC); boolean exists = (_publicKey != null); if (exists) ds = _publicKey; else ds = new PublicKey(); DataStructure readin = syncKey(keyFile, ds, exists); if (readin != null && !exists) _publicKey = (PublicKey) readin; }
signingPrivKey = new SigningPrivateKey(); signingPrivKey.readBytes(fis); PublicKey pubkey = new PublicKey(); pubkey.readBytes(fis); SigningPublicKey signingPubKey = new SigningPublicKey(); signingPubKey.readBytes(fis); if (!pubkey.equals(KeyGenerator.getPublicKey(privkey))) throw new DataFormatException("Key pairs invalid"); if (!signingPubKey.equals(KeyGenerator.getSigningPublicKey(signingPrivKey)))
/** * Without sig. This does NOT validate the signature */ protected void writeBody(OutputStream out) throws DataFormatException, IOException { if (_options != null && !_options.isEmpty()) { DataHelper.writeProperties(out, _options); } else { DataHelper.writeLong(out, 2, 0); } List<PublicKey> keys = getEncryptionKeys(); out.write((byte) keys.size()); for (PublicKey key : keys) { EncType type = key.getType(); if (type != null) { DataHelper.writeLong(out, 2, type.getCode()); } else { DataHelper.writeLong(out, 2, key.getUnknownTypeCode()); } DataHelper.writeLong(out, 2, key.length()); key.writeBytes(out); } out.write((byte) _leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } }
/** without sig! */ protected byte[] getBytes() { if (_byteified != null) return _byteified; if ((_destination == null) || (_encryptionKey == null) || (_signingKey == null)) return null; int len = size(); ByteArrayOutputStream out = new ByteArrayOutputStream(len); try { _destination.writeBytes(out); _encryptionKey.writeBytes(out); _signingKey.writeBytes(out); out.write((byte) _leases.size()); for (Lease lease : _leases) { lease.writeBytes(out); } } catch (IOException ioe) { return null; } catch (DataFormatException dfe) { return null; } byte rv[] = out.toByteArray(); // if we are floodfill and this was published to us if (_receivedAsPublished) _byteified = rv; return rv; }
if (!dest.getPublicKey().equals(sp.dest.getPublicKey())) return SessionStatusMessage.STATUS_INVALID;
@Test public void testShortRead() throws Exception{ PublicKey publicKey = new PublicKey(); ByteArrayInputStream in = new ByteArrayInputStream(DataHelper.getASCII("six times nine equals forty-two")); exception.expect(EOFException.class); exception.expectMessage("EOF after reading 31 bytes of " + PublicKey.KEYSIZE_BYTES + " byte value"); publicKey.readBytes(in); } }
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]))); } }
/** * Deprecated, used only by Packet.java in streaming. * Broken for sig types P521 and RSA before 0.9.15 * @return the written length (NOT the new offset) */ public int writeBytes(byte target[], int offset) { int cur = offset; System.arraycopy(_publicKey.getData(), 0, target, cur, PublicKey.KEYSIZE_BYTES); cur += PublicKey.KEYSIZE_BYTES; if (_padding != null) { System.arraycopy(_padding, 0, target, cur, _padding.length); cur += _padding.length; } int spkTrunc = Math.min(SigningPublicKey.KEYSIZE_BYTES, _signingKey.length()); System.arraycopy(_signingKey.getData(), 0, target, cur, spkTrunc); cur += spkTrunc; cur += _certificate.writeBytes(target, cur); return cur - offset; }
/** * For debugging * @since 0.9 */ private static String toString(PublicKey target) { if (target == null) return "null"; return target.toBase64().substring(0, 20) + "..."; }
/** * @param publicKey non-null, same EncType as privateKey * @param privateKey non-null, same EncType as publicKey */ public KeyPair(PublicKey publicKey, PrivateKey privateKey) { pub = publicKey; priv = privateKey; if (pub.getType() != priv.getType()) throw new IllegalArgumentException(); }
/** * Constructs from base64. ElGamal only. * @param base64Data a string of base64 data (the output of .toBase64() called * on a prior instance of PublicKey */ public PublicKey(String base64Data) throws DataFormatException { this(DEF_TYPE); fromBase64(base64Data); }
/** * @param type if null, type is unknown * @param data must be non-null * @since 0.9.38 */ public PublicKey(EncType type, byte data[]) { this(type); if (data == null) throw new IllegalArgumentException("Data must be specified"); setData(data); }
public DataStructure createStructureToRead() { return new PublicKey(); }