|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--iaik.me.security.Cipher | +--iaik.me.security.rsa.RSA
This class implements the RSA algorithm through the Cipher interface.
An application can use this class to encrypt or decrypt data with RSA public and private keys.
The RSA (Rivest Shamir Adleman) algorithm is one of the most famous public-key
algortihms used for data encryption or digital signing based on modulo
multiplications. For data encryption, messages are encrypted using the public
key (modulus n
, public exponent e
) of some entity.
Since only this entity holds the corresponding private key (private exponent d),
nobody else would be able to decrypt the encrypted message. The public key
(n,e)
is derived by first chosing two random large primes,
p
and q
, from which the modulus is calculated by doing
n=pq
.
The public exponent e
must be chosen to be relatively prime to
(p-1)(q-1)
. The corresponding private exponent d
yields from the prediction that ed
has to be congruent to
1 mod(p-1)(q-1)
. Encrypting some message m
(of size less than n
) is done by the continued modulo multiplication
c =me(mod n)
, decrypting uses the formula
m =cd(mod n)
. (see "Applied Cryptography", Bruce Schneier,
ISBN 0-471-59756-2). For digital signing, the RSA algorithm may be featured by a
certain hash algorithm for first building the message digest of the data to be
signed and subsequently signing it (encrypting it with an entity´s RSA private key).
Signature verifying uses the corresponding RSA public key
(see iaik.security.rsa.RSASignature).
PKCS#1 defines a data encryption method (rsaEncryption) based on the RSA
public-key algorithm to be used for generating digital signatures and digital
envelopes, as described in PKCS#7. Furthermore this standard describes a syntax
for RSA public keys (to be used in certificates) and RSA private keys (for use in
PKCS#8 private-key information). For effeciency reasons, in PKCS#1 the RSA private key
is treated not only to consist of modulus n
and private exponent
d
. Rather it is extended by a certain set of parameter values
(see iaik.security.rsa.RSAPrivateKey). PKCS#1 supports compatibility to X.509
and PEM (see
http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/).
This class follows the method described in PKCS#1 (Version 1.5) for RSA en/decrypting some data. The encryption process encrypts a given octet string to an encrypted octet string using two integer values as parameters, denoting the modulus (n) and the exponent (c), which either will represent the public exponent (e) or the private exponent (d), depending on whether to perform a public-key or a private-key operation. The decryption process decrypts a given encrypted octet string to an octet string, again using two integer values as parameters, denoting the modulus (n) and the exponent (c), which either will represent the public exponent (e) or the private exponent (d), depending on whether to perform a public-key or a private-key operation. Both encryption and decryption process first convert the given octet-string data input to an integer, which is transformed back to give the octet string output after doing the RSA computation. Before (respectively after) doing initial octet-string-to-integer (respectively final integer-to-octet-string) conversation, padding (unpadding) may be performed according to PKCS#1.
To en/decrypt data without any padding (encryption block formatting)
use Cipher.getInstance("RSA/ECB/NoPadding")
, however, this is not recommended.
You should use PKCS#1 padding, (Cipher.getInstance("RSA")
), which is equivalent
to RSA/ECB/PKCS1Padding
).
The padding block type will automatically be selected as 2 for public key encryption/
private key decryption and 1 for private key encryption/ public key decryption.
You can also explicitly specify the desiged block type using
Cipher.getInstance("RSA/n/PKCS1Padding")
where n is the padding type
(0, 1, or 2).
Code example:
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding"); rsa.init(Cipher.ENCRYPT_MODE, RSAPrivateKey); // auto selects block type 1or
rsa.init(Cipher.ENCRYPT_MODE, RSAPublicKey); // auto selects block type 2 crypted = rsa.doFinal(data);
Cipher
,
iaik.me.security.CipherSpi
,
CryptoBag
Fields inherited from class iaik.me.security.Cipher |
chainingMode, DECRYPT_MODE, ENCRYPT_MODE, iv, mode, MODE_CBC, MODE_ECB |
Constructor Summary | |
RSA()
Default Constructor for the RSA cipher. |
Method Summary | |
byte[] |
doFinal(byte[] message)
Performs a modulo exponentiation. |
int |
doFinal(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Encrypt the given data performing final padding operations. |
int[] |
getKeyLength()
Return the valid key lengths for this cipher. |
void |
init(int mode,
CryptoBag key,
Object params,
SecureRandom random)
Initializes this RSA cipher with the given key. |
protected String |
setMode(String mode)
Sets the block mode of the encryption block according to PKCS#1. |
protected String |
setPadding(String padding)
Sets the padding scheme of this cipher, which only can be "PKCS1Padding" or "NoPadding". |
int |
update(byte[] input,
int inputOffset,
int inputLen,
byte[] output,
int outputOffset)
Encrypt the input data storing it in the output array. |
Methods inherited from class iaik.me.security.Cipher |
cryptBlock, extractIV, getBlockSize, getInstance, getIV, init, register, toString, updateInternal |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public RSA()
This constructor only internally is used for initializing a RSA Cipher.
Applications should not call this constructor to get a RSA Cipher;
they should call one of the Cipher.getInstance
factory methods instead.
Cipher.getInstance(java.lang.String)
Method Detail |
public int[] getKeyLength()
Cipher
getKeyLength
in class Cipher
public int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws CryptoException
Cipher
update
in class Cipher
public int doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws CryptoException
Cipher
doFinal
in class Cipher
public void init(int mode, CryptoBag key, Object params, SecureRandom random) throws CryptoException
Before a cipher object is ready for data processing, it has to be initialized
according to the desired cryptographic operation, which is specified by the
opmode
parameter (either ENCRYPT_MODE or DECRYPT_MODE).
The key either will be a RSAPrivateKey or a RSAPublicKey, depending on
the specific cryptographic operation to be performed.
This class supports keys represented by IAIK specific classes as well as
java.security.spec.RSA*KeySpec
,
java.security.interfaces.RSA*Key
,
as well as java.security.PublicKey
and java.security.PrivateKey
that ASN.1 code themselves as RSA keys.
Applications shall use the corresponding init
method of
iaik.me.security.Cipher
for provider independently initializing
a RSA cipher.
init
in class Cipher
opmode
- Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODEkey
- an instance of a RSA PublicKey or RSA PrivateKeyrandom
- source of randomnessInvalidKeyException
- if the RSA key cannot be createdpublic byte[] doFinal(byte[] message) throws CryptoException
Applications shall use the corresponding doFinal
method of
iaik.me.security.Cipher
for provider independently
doing the data en/decryption.
The data to be processed is given in an input byte array. Beginning at
inputOffset
, only the first inputLen
bytes are
en/decrypted. The result is returned as an output byte array.
doFinal
in class Cipher
in
- the byte array holding the data to be processedinOff
- the offset indicating the start position within the input byte arrayinLen
- the number of bytes to be processedCryptoException
- if the decrypted data is not bounded by the proper padding bytes after data
decryption including (un)paddingCipher.doFinal(byte[], int, int, byte[], int)
,
iaik.me.security.CipherSpi#engineDoFinal
protected String setPadding(String padding) throws CryptoException
setPadding
in class Cipher
padding
- the padding scheme for this RSA cipherNoSuchPaddingException
- if padding algorithm is not "PKCS1Padding"protected String setMode(String mode) throws CryptoException
setMode
in class Cipher
the
- block type (0,1 or 2)NoSuchAlgorithmException
- if the block type is not 0,1 or 2
|
This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |