01: package org.bouncycastle.crypto.engines;
02:
03: import org.bouncycastle.crypto.AsymmetricBlockCipher;
04: import org.bouncycastle.crypto.CipherParameters;
05: import org.bouncycastle.crypto.DataLengthException;
06:
07: /**
08: * this does your basic RSA algorithm.
09: */
10: public class RSAEngine implements AsymmetricBlockCipher {
11: private RSACoreEngine core;
12:
13: /**
14: * initialise the RSA engine.
15: *
16: * @param forEncryption true if we are encrypting, false otherwise.
17: * @param param the necessary RSA key parameters.
18: */
19: public void init(boolean forEncryption, CipherParameters param) {
20: if (core == null) {
21: core = new RSACoreEngine();
22: }
23:
24: core.init(forEncryption, param);
25: }
26:
27: /**
28: * Return the maximum size for an input block to this engine.
29: * For RSA this is always one byte less than the key size on
30: * encryption, and the same length as the key size on decryption.
31: *
32: * @return maximum size for an input block.
33: */
34: public int getInputBlockSize() {
35: return core.getInputBlockSize();
36: }
37:
38: /**
39: * Return the maximum size for an output block to this engine.
40: * For RSA this is always one byte less than the key size on
41: * decryption, and the same length as the key size on encryption.
42: *
43: * @return maximum size for an output block.
44: */
45: public int getOutputBlockSize() {
46: return core.getOutputBlockSize();
47: }
48:
49: /**
50: * Process a single block using the basic RSA algorithm.
51: *
52: * @param in the input array.
53: * @param inOff the offset into the input buffer where the data starts.
54: * @param inLen the length of the data to be processed.
55: * @return the result of the RSA process.
56: * @exception DataLengthException the input block is too large.
57: */
58: public byte[] processBlock(byte[] in, int inOff, int inLen) {
59: if (core == null) {
60: throw new IllegalStateException(
61: "RSA engine not initialised");
62: }
63:
64: return core.convertOutput(core.processBlock(core.convertInput(
65: in, inOff, inLen)));
66: }
67: }
|