|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
|
+--javax.crypto.CipherSpi
|
+--iaik.security.cipher.BufferedCipher
|
+--iaik.security.cipher.RC2
|
+--iaik.security.cipher.PbeWithSHAAnd40BitRC2_CBC
This class implements from the Personal Information Exchange Standard (PKCS#12) the algorithm pbeWithSHAAnd40BitRC2_CBC (object identifier: 1.2.840.113549.1.12.1.6).
The PbeWithSHAAnd40BitRC2_CBC key-encryption algorithm is used to encrypt a given message with the RC2 algorithm in CBC mode using a 40Bit secret key which is derived from a password with the SHA hash algorithm as described in PKCS#12.
PKCS#12 breaks with the PKCS#5 recommendation suggesting passwords to consist of printable ASCII characters. PKCS #12 creates passwords from BMPStrings with a NULL terminator by encoding every character of the original BMPString in 2 bytes in big-endian format (most-significant byte first).
As an alternative to the PKCS#5 pbeWithMD5AndDES-CBC and
pbeWithMD2AndDES-CBC algorithms, the
pbeWithSHAAnd40BitRC2_CBC algorithm maybe used for encrypting
private keys, as described in PKCS#8.
Suppose you have created a RSAPrivateKey rsa_priv_key and are
going to protect it with a password according to PKCS#5, (PKCS#12) and
PKCS#8. You therefore will encode a value of type
PrivateKeyInfo
according to PKCS#8 to represent the private key in an algorithm-independent
manner, which subsequently will be encrypted using the PbeWithSHAAnd40BitRC2_CBC
algorithm and encoded as PKCS#8 EncryptedPrivateKeyInfo:
import iaik.pkcs.pkcs8.*;
...
EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(rsa_priv_key);
epki.encrypt("password", AlgorithmID.pbeWithSHAAnd40BitRC2_CBC, null);
Decrypting goes the reverse way obtaining a PrivateKeyInfo from the
EncryptedPrivateKeyInfo and "extracting" the RSAPrivateKey:
RSAPrivateKey rsa_priv_key = (RSAPrivateKey)epki.decrypt("password");
You also may use the PbeWithSHAAnd40BitRC2_CBC algorithm for password based
encrypting some message in the common way by directly using the
Cipher.getInstance method when not intending to deal with PKCS#8
EncryptedPrivateKeyInfo.
When doing so, you will use
PBEKeyBMP
(created from some password, which is treated as BMPString according to PKCS#12)
and PBEParameterSpec
(created from salt and iteration count) for properly
initializing the cipher; for instance (do not forget to include exception
handling!):
Random random = new Random();
// salt
byte[] salt = new byte[16];
random.nextBytes(salt);
//iteration count
int count = 1;
// PBE paramters
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
// PBEKeyBMP from password
PBEKeyBMP pbeKey = new PBEKeyBMP("password");
Cipher pbeCipher = Cipher.getInstance("PbeWithSHAAnd40BitRC2_CBC");
// initialize for encryption
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// encrypt data
byte[] cipher_data = pbeCipher.doFinal(plain_data);
// initialize for decryption
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
// decrypt cipher data
byte[] decrypted_data = pbeCipher.doFinal(cipherdata);
PrivateKeyInfo,
EncryptedPrivateKeyInfo,
RC2,
PBEKeyBMP,
PBEParameterSpec,
PBEGenParameterSpec,
PBEParameterGenerator,
PBEParameters,
IaikPBEParameterSpec| Field Summary | |
protected AlgorithmParameters |
params
Contains the parameters the cipher is initialized with |
| Constructor Summary | |
PbeWithSHAAnd40BitRC2_CBC()
Creates a new PbeWithSHAAnd40BitRC2_CBC Cipher object. |
|
| Method Summary | |
AlgorithmParameters |
engineGetParameters()
This method returns the algorithm's parameters. |
void |
engineInit(int opmode,
Key key,
AlgorithmParameterSpec paramSpec,
SecureRandom random)
Initializes this cipher for encryption or decryption. |
void |
engineInit(int opmode,
Key key,
AlgorithmParameters params,
SecureRandom random)
Initializes this cipher for encryption or decryption. |
void |
engineInit(int opmode,
Key key,
SecureRandom random)
Initializes this cipher for encryption or decryption. |
void |
engineSetMode(java.lang.String mode)
This method only overwrites the corresponding method in its superclass and does nothing. |
void |
engineSetPadding(java.lang.String padding)
This method only overwrites the corresponding method in its superclass and does nothing. |
protected void |
initCipher(int opmode,
Key key,
SecureRandom random)
This method is used by the engineInit methods and initializes the cipher. |
| Methods inherited from class iaik.security.cipher.BufferedCipher |
engineDoFinal,
engineDoFinal,
engineGetBlockSize,
engineGetIV,
engineGetOutputSize,
engineUpdate,
engineUpdate,
toString |
| Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
| Field Detail |
protected AlgorithmParameters params
| Constructor Detail |
public PbeWithSHAAnd40BitRC2_CBC()
throws NoSuchAlgorithmException,
NoSuchPaddingException
Usually this constructor is not directly called for using the PbeWithSHAAnd40BitRC2_CBC algorithm for password-based encrypting some message. Rather
Cipher.getInstance("PbeWithSHAAnd40BitRC2_CBC")
is used to get a PbeWithSHAAnd40BitRC2_CBC Cipher object.
When dealing with PKCS#8 EncryptedPrivateKeyInfo this
algorithm is specified by its appertaining AlgorithmID, e.g.
epki.encrypt("password", AlgorithmID.pbeWithSHAAnd3_KeyTripleDES_CBC, null);
causing a call to the getInstance method of the
iaik.asn1.structures.AlgorithmID class which in its turn calls
Cipher.getInstance(algorithmID.getName()) for actually
getting an implementation of the the PbeWithSHAAnd40BitRC2_CBC algorithm,
finally leading to this constructor.
| Method Detail |
public AlgorithmParameters engineGetParameters()
null.
public void engineInit(int opmode,
Key key,
SecureRandom random)
throws InvalidKeyException
The key must be a PBEKeyBMP in "RAW_BMP" format; you may use a
PBEKeyBMP key.
This method initializes salt
(random value of 8 byte length) and iteration count with 1 as specified
in PKCS#12.
opmode - Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODEkey - the password as PBEKeyBMPrandom - not needed - shall be null
public void engineInit(int opmode,
Key key,
AlgorithmParameters params,
SecureRandom random)
throws InvalidKeyException,
InvalidAlgorithmParameterException
The key must be a PBEKeyBMP in "RAW_BMP" format; you may use a
PBEKeyBMP key.
params has to be of type
PBEParameterSpec,
created from salt and
iteration count as specified in PKCS#12.
opmode - Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODEkey - the password as PBEKeyBMPparams - the algorithm parameters of type PBEParametersrandom - not needed - shall be null
public void engineInit(int opmode,
Key key,
AlgorithmParameterSpec paramSpec,
SecureRandom random)
throws InvalidKeyException,
InvalidAlgorithmParameterException
The key must be a PBEKeyBMP in "RAW_BMP" format; you may use a
PBEKeyBMP key.
params has to be of type
PBEParameterSpec,
created from salt and
iteration count as specified in PKCS#12.
opmode - Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODEkey - the password as PBEKeyBMPparams - the algorithm parameters of type PBEParameterSpecrandom - not needed - shall be null
protected void initCipher(int opmode,
Key key,
SecureRandom random)
throws InvalidKeyException,
InvalidAlgorithmParameterException
public void engineSetPadding(java.lang.String padding)
throws NoSuchPaddingException
padding - the name of the padding scheme
public void engineSetMode(java.lang.String mode)
throws NoSuchAlgorithmException
mode - the cipher mode
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||