com.googlecode.gwt.crypto.bouncycastle.engines.RSAEngine Maven / Gradle / Ivy
package com.googlecode.gwt.crypto.bouncycastle.engines;
import com.googlecode.gwt.crypto.bouncycastle.AsymmetricBlockCipher;
import com.googlecode.gwt.crypto.bouncycastle.CipherParameters;
import com.googlecode.gwt.crypto.bouncycastle.DataLengthException;
/**
* this does your basic RSA algorithm.
*/
public class RSAEngine
implements AsymmetricBlockCipher
{
private RSACoreEngine core;
/**
* initialise the RSA engine.
*
* @param forEncryption true if we are encrypting, false otherwise.
* @param param the necessary RSA key parameters.
*/
@Override
public void init(
boolean forEncryption,
CipherParameters param)
{
if (core == null)
{
core = new RSACoreEngine();
}
core.init(forEncryption, param);
}
/**
* 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.
*/
@Override
public int getInputBlockSize()
{
return core.getInputBlockSize();
}
/**
* 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.
*/
@Override
public int getOutputBlockSize()
{
return core.getOutputBlockSize();
}
/**
* Process a single block using the basic RSA algorithm.
*
* @param in the input array.
* @param inOff the offset into the input buffer where the data starts.
* @param inLen the length of the data to be processed.
* @return the result of the RSA process.
* @exception DataLengthException the input block is too large.
*/
@Override
public byte[] processBlock(
byte[] in,
int inOff,
int inLen)
{
if (core == null)
{
throw new IllegalStateException("RSA engine not initialised");
}
return core.convertOutput(core.processBlock(core.convertInput(in, inOff, inLen)));
}
}