int base64Length = cryptoImpl.getBase64PublicKeyPairLength(); // length of an email destination with this CryptoImplementation
/** * @param address A string containing a valid base64-encoded Email Destination * @throws GeneralSecurityException If <code>address</code> doesn't contain a valid Email Destination */ public EmailDestination(String address) throws GeneralSecurityException { String base64Key = extractBase64Dest(address); if (base64Key == null) throw new GeneralSecurityException("No Email Destination found in string: <" + address + ">"); // find the crypto implementation for this key length for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int base64Length = cryptoImpl.getBase64PublicKeyPairLength(); // length of an email destination that uses this CryptoImplementation if (base64Key.length() == base64Length) this.cryptoImpl = cryptoImpl; } if (cryptoImpl == null) throw new InvalidKeyException("Not a valid Email Destination: <" + base64Key + ">"); PublicKeyPair keyPair = cryptoImpl.createPublicKeyPair(base64Key); publicEncryptionKey = keyPair.encryptionKey; publicSigningKey = keyPair.signingKey; }
/** * Creates a <code>EmailIdentity</code> from a Base64-encoded string. * The format can be any format supported by one of the {@link CryptoImplementation}s; * the length of the string must match {@link CryptoImplementation#getBase64CompleteKeySetLength()}. * @param base64Key * @throws GeneralSecurityException */ public EmailIdentity(String base64Key) throws GeneralSecurityException { // find the crypto implementation for this key length for (CryptoImplementation cryptoImpl: CryptoFactory.getInstances()) { int base64Length = cryptoImpl.getBase64CompleteKeySetLength(); // length of an email identity with this CryptoImplementation if (base64Key.length() == base64Length) { this.cryptoImpl = cryptoImpl; break; } } if (cryptoImpl == null) throw new InvalidKeyException("Not a valid Email Identity, no CryptoImplementation (out of " + CryptoFactory.getInstances().size() + ") matches length " + base64Key.length() + ": <" + base64Key + ">"); String base64PublicKeys = base64Key.substring(0, cryptoImpl.getBase64PublicKeyPairLength()); // the two private keys start after the two public keys PublicKeyPair publicKeys = cryptoImpl.createPublicKeyPair(base64PublicKeys); String base64PrivateKeys = base64Key.substring(cryptoImpl.getBase64PublicKeyPairLength()); // the two private keys start after the two public keys PrivateKeyPair privateKeys = cryptoImpl.createPrivateKeyPair(base64PrivateKeys); publicEncryptionKey = publicKeys.encryptionKey; privateEncryptionKey = privateKeys.encryptionKey; publicSigningKey = publicKeys.signingKey; privateSigningKey = privateKeys.signingKey; identityConfig = new IdentityConfigImpl(); }
@Test public void encodeDecodeBase64() throws GeneralSecurityException { for (TestIdentity testData: testIdentities) { CryptoImplementation cryptoImpl = testData.cryptoImpl; // test a public key pair PublicKeyPair publicKeyPair = cryptoImpl.createPublicKeyPair(testData.base64PublicKeyPair); String reencodedPublic = cryptoImpl.toBase64(publicKeyPair); assertEquals(testData.base64PublicKeyPair, reencodedPublic); assertEquals("Wrong public key length!", cryptoImpl.getBase64PublicKeyPairLength(), testData.base64PublicKeyPair.length()); // test a private key pair PrivateKeyPair privateKeyPair = cryptoImpl.createPrivateKeyPair(testData.base64PrivateKeyPair); String reencodedPrivate = cryptoImpl.toBase64(privateKeyPair); assertEquals(testData.base64PrivateKeyPair, reencodedPrivate); assertEquals("Wrong private key length!", cryptoImpl.getBase64CompleteKeySetLength()-cryptoImpl.getBase64PublicKeyPairLength(), testData.base64PrivateKeyPair.length()); } }