Debian Codegraph @

crypto/RSAEngine.java

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