/** * Return the maximum size for an input block to this engine. * For RSA this is always one byte less than the key size on * encryption, and the same length as the key size on decryption. * * @return maximum size for an input block. */ public int getInputBlockSize() { int bitSize = key.getModulus().bitLength(); if (forEncryption) { return (bitSize + 7) / 8 - 1; } else { return (bitSize + 7) / 8; } }
/** * {@inheritDoc} */ @Override public int getBitStrength() throws IllegalStateException { if (state == null) { throw new IllegalStateException(); } else { return state.keyParameters.getModulus().bitLength(); } }
/** * Return the maximum size for an output block to this engine. * For RSA this is always one byte less than the key size on * decryption, and the same length as the key size on encryption. * * @return maximum size for an output block. */ public int getOutputBlockSize() { int bitSize = key.getModulus().bitLength(); if (forEncryption) { return (bitSize + 7) / 8; } else { return (bitSize + 7) / 8 - 1; } }
BCRSAPublicKey( RSAKeyParameters key) { this.algorithmIdentifier = DEFAULT_ALGORITHM_IDENTIFIER; this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
BCRSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
JCERSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
JCERSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
JCERSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
BCRSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
private BigInteger unblindMessage( BigInteger blindedMsg) { BigInteger m = key.getModulus(); BigInteger msg = blindedMsg; BigInteger blindFactorInverse = blindingFactor.modInverse(m); msg = msg.multiply(blindFactorInverse); msg = msg.mod(m); return msg; } }
BCRSAPublicKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.publicExponent = key.getExponent(); }
private BigInteger unblindMessage( BigInteger blindedMsg) { BigInteger m = key.getModulus(); BigInteger msg = blindedMsg; BigInteger blindFactorInverse = blindingFactor.modInverse(m); msg = msg.multiply(blindFactorInverse); msg = msg.mod(m); return msg; } }
private BigInteger blindMessage( BigInteger msg) { BigInteger blindMsg = blindingFactor; blindMsg = msg.multiply(blindMsg.modPow(key.getExponent(), key.getModulus())); blindMsg = blindMsg.mod(key.getModulus()); return blindMsg; }
JCERSAPrivateKey( RSAKeyParameters key) { this.modulus = key.getModulus(); this.privateExponent = key.getExponent(); }
private BigInteger blindMessage( BigInteger msg) { BigInteger blindMsg = blindingFactor; blindMsg = msg.multiply(blindMsg.modPow(key.getExponent(), key.getModulus())); blindMsg = blindMsg.mod(key.getModulus()); return blindMsg; }
public void init( boolean forSigning, CipherParameters param) { kParam = (RSAKeyParameters)param; cipher.init(forSigning, kParam); keyBits = kParam.getModulus().bitLength(); block = new byte[(keyBits + 7) / 8]; reset(); }
/** * generate a signature for the loaded message using the key we were * initialised with. */ public byte[] generateSignature() throws CryptoException { createSignatureBlock(); BigInteger t = new BigInteger(1, cipher.processBlock(block, 0, block.length)); clearBlock(block); t = t.min(kParam.getModulus().subtract(t)); return BigIntegers.asUnsignedByteArray((kParam.getModulus().bitLength() + 7) / 8, t); }
public void init( boolean forSigning, CipherParameters param) { RSAKeyParameters kParam = (RSAKeyParameters)param; cipher.init(forSigning, kParam); keyBits = kParam.getModulus().bitLength(); block = new byte[(keyBits + 7) / 8]; if (trailer == TRAILER_IMPLICIT) { mBuf = new byte[block.length - digest.getDigestSize() - 2]; } else { mBuf = new byte[block.length - digest.getDigestSize() - 3]; } reset(); }
/** * {@inheritDoc} */ @NonNull @Override public Map<String, Object> toHash() throws IllegalStateException { if (state == null) { throw new IllegalStateException(); } else { return Collections.unmodifiableMap(new HashMap<String, Object>() {{ put("n", BigIntegers.asUnsignedByteArray(state.keyParameters.getModulus())); put("e", BigIntegers.asUnsignedByteArray(state.keyParameters.getExponent())); // Optional fields. if (!state.mgf1HashType.equals(DEFAULT_MGF1_HASH)) { put("mgf1Hash", state.mgf1HashType.getAlgorithmName()); } }}); } }
/** * Test {@link RSAOAEPPublicKey#toHash} where some data is default. */ @Test public void toHashWithDefaultData() throws Exception { // Test random public key. AbstractPublicKey goodPublicKey1 = new RSAOAEPPublicKey( BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()), HashType.SHA1, HashType.SHA1, new SecureRandom()); Map map1 = goodPublicKey1.toHash(); assertArrayEquals((byte[]) map1.get("n"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus())); assertArrayEquals((byte[]) map1.get("e"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent())); // With the default values (SHA-256 for hash, SHA-1 for MGF1), // hash and mgf1Hash fields should be missing from the hash. assertFalse(map1.containsKey("hash")); assertFalse(map1.containsKey("mgf1Hash")); }