use of org.mozilla.jss.crypto.IVParameterSpec in project OpenAM by OpenRock.
the class JSSEncryption method encode.
/**
* <p>Encrypt a String.</p>
* @param clearText The string to be encoded.
* @return The encoded string.
*/
private byte[] encode(byte[] clearText) {
if (clearText == null || clearText.length == 0)
return null;
try {
byte[] type = new byte[2];
String encAlgString = DEFAULT_ENCYPTION_ALG;
EncryptionAlgorithm encAlg = getEncryptionAlg(encAlgString);
int i = getEncryptionByte(encAlgString);
type[1] = (byte) i;
Cipher cipher = mToken.getCipherContext(encAlg);
String keyA = DEFAULT_KEYGEN_ALG;
i = getKeyGenByte(keyA);
type[0] = (byte) i;
SymmetricKey sk = getSymmetricKey(i);
// bug in JSS: msg in stdout.
//secureRandom.nextBytes(iv);
IVParameterSpec ivSpec = getIVParameterSpec(i);
byte[] iv = ivSpec.getIV();
cipher.initEncrypt(sk, ivSpec);
byte[] enc = cipher.doFinal(clearText);
enc = addPrefix(type, iv, enc);
return (enc);
} catch (Throwable e) {
if (debug != null) {
debug.error("in encode string " + e);
}
return null;
}
}
use of org.mozilla.jss.crypto.IVParameterSpec in project OpenAM by OpenRock.
the class JSSEncryption method initSymmetricKeysAndInitializationVectors.
private void initSymmetricKeysAndInitializationVectors(String password) {
sKeys = new SymmetricKey[NUM_KEYGEN_ALG];
ivParamSpecs = new IVParameterSpec[NUM_KEYGEN_ALG];
byte[] salt = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
Password pass = new Password(password.toCharArray());
for (int i = 0; i < NUM_KEYGEN_ALG; i++) {
try {
PBEAlgorithm keyAlg = getKeyGenAlg(KEYGEN_ALGS[i]);
KeyGenerator kg = mToken.getKeyGenerator(keyAlg);
PBEKeyGenParams kgp = new PBEKeyGenParams(pass, salt, 5);
kg.initialize(kgp);
sKeys[i] = kg.generate();
ivParamSpecs[i] = new IVParameterSpec(kg.generatePBE_IV());
if (debug.messageEnabled()) {
debug.message("Created symKey successfully : " + KEYGEN_ALGS[i]);
}
} catch (Exception e) {
debug.error("Failed creating symKey : " + KEYGEN_ALGS[i], e);
}
}
pass.clear();
}
use of org.mozilla.jss.crypto.IVParameterSpec in project OpenAM by OpenRock.
the class JSSEncryption method decode.
/**
* Decode an encoded string
*
* @param encoded The encoded string.
* @return The decoded string.
**/
private byte[] decode(byte[] encoded) {
if (encoded == null || encoded.length == 0) {
return null;
}
try {
byte[] share = encoded;
if (share[0] != VERSION) {
if (debug != null) {
debug.error("In decode string: unsupported version:" + share[0]);
}
return null;
}
// get the alg from the string
byte[] type = getType(share);
// get the encrypted data
share = getRaw(share);
if ((int) type[1] < 0 && (int) type[1] >= NUM_ENCRYPTION_ALG) {
if (debug != null) {
debug.error("In decode string: unsupported encryption bit:" + (int) type[1]);
}
return null;
}
EncryptionAlgorithm encAlg = getEncryptionAlg(ENCRYPTION_ALGS[(int) type[1]]);
Cipher cipher = mToken.getCipherContext(encAlg);
if ((int) type[0] < 0 && (int) type[0] >= NUM_KEYGEN_ALG) {
if (debug != null) {
debug.error("In decode string: unsupported keygen bit:" + (int) type[0]);
}
return null;
}
SymmetricKey sk = getSymmetricKey((int) type[0]);
IVParameterSpec ivSpec = getIVParameterSpec((int) type[0]);
cipher.initDecrypt(sk, ivSpec);
byte[] dec = cipher.doFinal(share);
if (dec == null) {
debug.error("Failed to decode " + encoded);
return null;
}
return (dec);
} catch (Throwable e) {
if (debug != null) {
debug.error("in decoding string " + encoded, e);
}
return null;
}
}
Aggregations