/** * Get the key size of a key represented by key parameters. * * @param keyParams The key parameters * @return The key size, {@link #UNKNOWN_KEY_SIZE} if not known */ public static int getKeyLength(AsymmetricKeyParameter keyParams) { if (keyParams instanceof RSAKeyParameters) { return ((RSAKeyParameters) keyParams).getModulus().bitLength(); } else if (keyParams instanceof DSAKeyParameters) { return ((DSAKeyParameters) keyParams).getParameters().getP().bitLength(); } else if (keyParams instanceof DHKeyParameters) { return ((DHKeyParameters) keyParams).getParameters().getP().bitLength(); } else if (keyParams instanceof ECKeyParameters) { // TODO: how to get key length from these? return UNKNOWN_KEY_SIZE; } LOG.warning("Don't know how to get key size from parameters " + keyParams); return UNKNOWN_KEY_SIZE; }
/** * Initialise the ECIES-KEM. * * @param key the recipient's public (for encryption) or private (for decryption) key. */ public void init(CipherParameters key) throws IllegalArgumentException { if (!(key instanceof ECKeyParameters)) { throw new IllegalArgumentException("EC key required"); } else { this.key = (ECKeyParameters)key; } }
public int engineGetOutputSize(int inputLen) { int len1, len2, len3; if (key == null) { throw new IllegalStateException("cipher not initialised"); } len1 = engine.getMac().getMacSize(); if (otherKeyParameter == null) { len2 = 1 + 2 * (((ECKeyParameters)key).getParameters().getCurve().getFieldSize() + 7) / 8; } else { len2 = 0; } if (engine.getCipher() == null) { len3 = inputLen; } else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) { len3 = engine.getCipher().getOutputSize(inputLen); } else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) { len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2); } else { throw new IllegalStateException("cipher not initialised"); } if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) { return buffer.size() + len1 + len2 + len3; } else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) { return buffer.size() - len1 - len2 + len3; } else { throw new IllegalStateException("cipher not initialised"); } }