use of org.mozilla.jss.crypto.SymmetricKey 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.SymmetricKey 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