本文整理汇总了Java中javax.crypto.Cipher.ENCRYPT_MODE属性的典型用法代码示例。如果您正苦于以下问题:Java Cipher.ENCRYPT_MODE属性的具体用法?Java Cipher.ENCRYPT_MODE怎么用?Java Cipher.ENCRYPT_MODE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类javax.crypto.Cipher
的用法示例。
在下文中一共展示了Cipher.ENCRYPT_MODE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: GCMCipherLite
GCMCipherLite(Cipher cipher, SecretKey secreteKey, int cipherMode) {
super(cipher, ContentCryptoScheme.AES_GCM, secreteKey, cipherMode);
tagLen = cipherMode == Cipher.ENCRYPT_MODE ? TAG_LENGTH : 0;
if (cipherMode != Cipher.ENCRYPT_MODE
&& cipherMode != Cipher.DECRYPT_MODE) {
throw new IllegalArgumentException();
}
}
示例2: getTag
/**
* For testing purposes.
* Applicable only during encryption: returns the tag that has been
* produced; or null otherwise.
*/
byte[] getTag() {
return getCipherMode() != Cipher.ENCRYPT_MODE || finalBytes == null
? null
: Arrays.copyOfRange(finalBytes,
finalBytes.length - tagLen, finalBytes.length)
;
}
示例3: execute
/**
* Perform encryption/decryption operation (depending on the specified
* edMode) on the same byte buffer. Compare result with the result at an
* allocated buffer. If both results are equal - return true, otherwise
* return false.
*
* @param edMode specified mode
* @param inputText text to decrypt
* @param offset offset in the text
* @param len input length
* @return ture - test passed; false - test failed
*/
@Override
public boolean execute(int edMode, byte[] inputText, int offset, int len) {
boolean isUnlimited;
try {
isUnlimited =
(Cipher.getMaxAllowedKeyLength(this.algo) == Integer.MAX_VALUE);
} catch (NoSuchAlgorithmException nsae) {
out.println("Got unexpected exception for " + this.algo);
nsae.printStackTrace(out);
return false;
}
try {
// init Cipher
if (Cipher.ENCRYPT_MODE == edMode) {
ci.init(Cipher.ENCRYPT_MODE, this.key);
pbeParams = ci.getParameters();
} else {
ci.init(Cipher.DECRYPT_MODE, this.key, pbeParams);
}
if (this.algo.endsWith("AES_256") && !isUnlimited) {
out.print("Expected exception not thrown for " + this.algo);
return false;
}
// First, generate the cipherText at an allocated buffer
byte[] outputText = ci.doFinal(inputText, offset, len);
// Second, generate cipherText again at the same buffer of plainText
int myoff = offset / 2;
int off = ci.update(inputText, offset, len, inputText, myoff);
ci.doFinal(inputText, myoff + off);
// Compare to see whether the two results are the same or not
return equalsBlock(inputText, myoff, outputText, 0,
outputText.length);
} catch (Exception ex) {
if ((ex instanceof InvalidKeyException)
&& this.algo.endsWith("AES_256") && !isUnlimited) {
out.println("Expected InvalidKeyException thrown");
return true;
} else {
out.println("Got unexpected exception for " + algo);
ex.printStackTrace(out);
return false;
}
}
}
示例4: engineInit
/**
* Initialises this cipher with key and a source of randomness
*/
protected final void engineInit(int mode, Key key, SecureRandom random)
throws InvalidKeyException {
if (mode == Cipher.ENCRYPT_MODE)
if (!(key instanceof PaillierPublicKey))
throw new InvalidKeyException(
"I didn't get a PaillierPublicKey. ");
else if (mode == Cipher.DECRYPT_MODE)
if (!(key instanceof PaillierPrivateKey))
throw new InvalidKeyException(
"I didn't get a PaillierPrivateKey. ");
else
throw new IllegalArgumentException("Bad mode: " + mode);
stateMode = mode;
keyPaillier = key;
SECURE_RANDOM = random;
int modulusLength = ((PaillierKey) key).getN().bitLength();
calculateBlockSizes(modulusLength);
}
示例5: wolfCryptSetDirection
private void wolfCryptSetDirection(int opmode)
throws InvalidKeyException {
/* we don't currently support AES key wrap in JCE yet,
* so don't allow WRAP_MODE or UNWRAP_MODE */
switch (opmode) {
case Cipher.ENCRYPT_MODE:
this.direction = OpMode.WC_ENCRYPT;
break;
case Cipher.DECRYPT_MODE:
this.direction = OpMode.WC_DECRYPT;
break;
default:
throw new InvalidKeyException(
"Cipher opmode must be ENCRYPT_MODE or DECRPYT_MODE");
}
}
示例6: initCipher
/**
* Initiate the Cipher object using given "mode".
* @return a cipher object.
* @throws GeneralSecurityException all security exceptions are thrown.
*/
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
Provider provider = Security.getProvider("SunJCE");
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
}
// get Cipher instance
Cipher ci = Cipher.getInstance(transformation, provider);
if (Cipher.ENCRYPT_MODE == mode) {
ci.init(Cipher.ENCRYPT_MODE, key);
pbeParams = ci.getParameters();
} else {
ci.init(Cipher.DECRYPT_MODE, key, pbeParams);
}
return ci;
}
示例7: createInverse
/**
* Returns the inverse of the current {@link CipherLite}.
*/
CipherLite createInverse() throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, InvalidAlgorithmParameterException {
int inversedMode;
if (cipherMode == Cipher.DECRYPT_MODE)
inversedMode = Cipher.ENCRYPT_MODE;
else if (cipherMode == Cipher.ENCRYPT_MODE)
inversedMode = Cipher.DECRYPT_MODE;
else
throw new UnsupportedOperationException();
return scheme.createCipherLite(secreteKey, cipher.getIV(),
inversedMode, cipher.getProvider());
}
示例8: cryptData
protected byte[] cryptData(
boolean forEncryption,
AlgorithmIdentifier algId,
char[] password,
boolean wrongPKCS12Zero,
byte[] data)
throws IOException
{
String algorithm = algId.getAlgorithm().getId();
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try
{
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(
pbeParams.getIV(),
pbeParams.getIterations().intValue());
BCPBEKey key = (BCPBEKey)keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
}
catch (Exception e)
{
throw new IOException("exception decrypting data - " + e.toString());
}
}
示例9: createEncryptor
@Override
public Encryptor createEncryptor() throws GeneralSecurityException {
return new JceAesCtrCipher(Cipher.ENCRYPT_MODE, provider);
}
示例10: init
private void init(int opmode, Key key, IvParameterSpec params) {
if ( mIsInited ) {
// Do not allow multiple inits
assert(true);
throw new RuntimeException("Don't allow multiple inits");
} else {
NativeLib.init();
mIsInited = true;
}
mIV = params.getIV();
mEncrypting = opmode == Cipher.ENCRYPT_MODE;
mCtxPtr = nInit(mEncrypting, key.getEncoded(), mIV);
addToCleanupQueue(this, mCtxPtr);
}
示例11: createCipher
/**
* Create a Cipher object for the requested encryption/decryption mode.
*
* @param mode encryption or decryption mode
* @return Cipher object initiated to perform requested mode operation
*/
private Cipher createCipher(int mode, AlgorithmParameters params)
throws Exception {
Cipher ci;
if (Cipher.ENCRYPT_MODE == mode) {
// create a new Cipher object for encryption
ci = Cipher.getInstance(transformation, provider);
// initiate it with the saved parameters
if (params != null) {
ci.init(Cipher.ENCRYPT_MODE, key, params);
} else {
// initiate the cipher without parameters
ci.init(Cipher.ENCRYPT_MODE, key);
}
} else {
// it is expected that parameters already generated
// before decryption
ci = Cipher.getInstance(transformation, provider);
ci.init(Cipher.DECRYPT_MODE, key, params);
}
return ci;
}
示例12: open
@Override
public void open(DataSpec dataSpec) throws IOException {
wrappedDataSink.open(dataSpec);
long nonce = CryptoUtil.getFNV64Hash(dataSpec.key);
cipher = new AesFlushingCipher(Cipher.ENCRYPT_MODE, secretKey, nonce,
dataSpec.absoluteStreamPosition);
}
示例13: engineGetOutputSize
public int engineGetOutputSize(int inputLen)
{
int len1, len2, len3;
len1 = engine.getMac().getMacSize();
if (key != null)
{
len2 = 1 + 2 * (((ECKey)key).getParameters().getCurve().getFieldSize() + 7) / 8;
}
else
{
throw new IllegalStateException("cipher not initialised");
}
if (engine.getCipher() == null)
{
len3 = inputLen;
}
else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
{
len3 = engine.getCipher().getOutputSize(inputLen);
}
else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
{
len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
}
else
{
throw new IllegalStateException("cipher not initialised");
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
{
return buffer.size() + len1 + len2 + len3;
}
else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
{
return buffer.size() - len1 - len2 + len3;
}
else
{
throw new IllegalStateException("cipher not initialised");
}
}
示例14: engineGetOutputSize
protected int engineGetOutputSize(
int inputLen)
{
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
{
return buffer.size() + inputLen + 20; /* SHA1 MAC size */
}
else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
{
return buffer.size() + inputLen - 20;
}
else
{
throw new IllegalStateException("cipher not initialised");
}
}
示例15: engineInit
protected void engineInit(
int opmode,
Key key,
SecureRandom random)
throws InvalidKeyException
{
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE)
{
try
{
engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
return;
}
catch (InvalidAlgorithmParameterException e)
{
// fall through...
}
}
throw new IllegalArgumentException("can't handle null parameter spec in IES");
}