本文整理匯總了Java中org.bouncycastle.crypto.BufferedBlockCipher.init方法的典型用法代碼示例。如果您正苦於以下問題:Java BufferedBlockCipher.init方法的具體用法?Java BufferedBlockCipher.init怎麽用?Java BufferedBlockCipher.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.bouncycastle.crypto.BufferedBlockCipher
的用法示例。
在下文中一共展示了BufferedBlockCipher.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decrypt
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
@Override
public String decrypt(byte[] encrypted) {
// Cipher cipher = null;
String plain;
try {
// Security.addProvider(new BouncyCastlePQCProvider());
// cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastlePQCProvider());
// cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new IvParameterSpec(iv));
// plain = new String(cipher.doFinal(encrypted), "UTF-8");
KeyParameter keyParam = new KeyParameter(encryptionKey);
CipherParameters params = new ParametersWithIV(keyParam, iv);
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(false, params);
byte[] buffer = new byte[cipher.getOutputSize(encrypted.length)];
int len = cipher.processBytes(encrypted, 0, encrypted.length, buffer, 0);
len += cipher.doFinal(buffer, len);
byte[] out = Arrays.copyOfRange(buffer, 0, len);
plain = new String(out, "UTF-8");
} catch (Exception e) {
throw new RuntimeException("decrypt error in SimpleAesManaged", e);
}
return plain;
}
示例2: EncryptAes256
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private static byte[] EncryptAes256(byte[] data, byte[] encryptionKey)
{
try {
KeyParameter keyParam = new KeyParameter(encryptionKey);
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(true, keyParam);
byte[] buffer = new byte[cipher.getOutputSize(data.length)];
int len = cipher.processBytes(data, 0, data.length, buffer, 0);
len += cipher.doFinal(buffer, len);
return Arrays.copyOfRange(buffer, 0, len);
} catch (Exception e) {
throw new RuntimeException("decrypt error in SimpleAesManaged", e);
}
}
示例3: testEncryptRijndael
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
public String testEncryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException {
BlockCipher engine = new RijndaelEngine(256);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding());
byte[] keyBytes = key.getBytes();
cipher.init(true, new KeyParameter(keyBytes));
byte[] input = value.getBytes();
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
cipher.doFinal(cipherText, cipherLength);
String result = new String(Base64.encode(cipherText));
//Log.e("testEncryptRijndael : " , result);
return result;
}
示例4: encryptDESFile
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private byte[] encryptDESFile(String keys, byte[] plainText) {
BlockCipher engine = new DESEngine();
byte[] key = keys.getBytes();
byte[] ptBytes = plainText;
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
cipher.init(true, new KeyParameter(key));
byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];
int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
try {
cipher.doFinal(rv, tam);
} catch (Exception ce) {
ce.printStackTrace();
}
return rv;
}
示例5: basicTrial
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private void basicTrial(BufferedBlockCipher cipher, KeyParameter param)
throws InvalidCipherTextException
{
cipher.init(true, param);
byte[] out = new byte[input.length];
int len1 = cipher.processBytes(input, 0, input.length, out, 0);
cipher.doFinal(out, len1);
if (!areEqual(out, output))
{
fail("failed - " + "expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out)));
}
}
示例6: processECB
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
public static byte[] processECB(byte[] key, boolean forEncryption, byte[] input) {
final BufferedBlockCipher cipher = new BufferedBlockCipher(new TwofishEngine());
final KeyParameter kp = new KeyParameter(key);
cipher.init(forEncryption, kp);
final byte[] out = new byte[input.length];
final int len1 = cipher.processBytes(input, 0, input.length, out, 0);
try {
cipher.doFinal(out, len1);
} catch (final CryptoException e) {
throw new RuntimeException(e);
}
return out;
}
示例7: CreateInputStream
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private static InputStream CreateInputStream(InputStream s, boolean bEncrypt, byte[] pbKey, byte[] pbIV)
{
byte[] pbLocalIV = new byte[16];
System.arraycopy(pbIV, 0, pbLocalIV, 0, 16);
byte[] pbLocalKey = new byte[32];
System.arraycopy(pbKey, 0, pbLocalKey, 0, 32);
try {
// Cipher r = Cipher.getInstance("AES/CBC/PKCS5Padding");
// IvParameterSpec ivspec = new IvParameterSpec(pbLocalIV);
// SecretKeySpec keyspec = new SecretKeySpec(pbLocalKey, "AES");
// r.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
BlockCipher aes = AesEngines.createAesEngine();
KeyParameter key = new KeyParameter(pbLocalKey);
ParametersWithIV iv = new ParametersWithIV(key, pbLocalIV);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes));
cipher.init(false, iv);
return new CipherInputStream(s, cipher);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
示例8: CreateOutputStream
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private static OutputStream CreateOutputStream(OutputStream s, boolean bEncrypt, byte[] pbKey, byte[] pbIV)
{
byte[] pbLocalIV = new byte[16];
System.arraycopy(pbIV, 0, pbLocalIV, 0, 16);
byte[] pbLocalKey = new byte[32];
System.arraycopy(pbKey, 0, pbLocalKey, 0, 32);
try {
BlockCipher aes = AesEngines.createAesEngine();
KeyParameter key = new KeyParameter(pbLocalKey);
ParametersWithIV iv = new ParametersWithIV(key, pbLocalIV);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(aes));
cipher.init(true, iv);
// Cipher r = Cipher.getInstance("AES/CBC/PKCS5Padding");
// IvParameterSpec ivspec = new IvParameterSpec(pbLocalIV);
// SecretKeySpec keyspec = new SecretKeySpec(pbLocalKey, "AES");
// r.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
return new CipherOutputStream(s, cipher);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
示例9: getPrivKey
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
/**
* Returns the decrypted private key
*
* @param keyPhrase Key phrase used to derive the encryption key
* @return Private key
* @throws ECException Unable to complete a cryptographic function
*/
public BigInteger getPrivKey(String keyPhrase) throws ECException {
KeyParameter aesKey = deriveKey(keyPhrase, salt);
//
// Decrypt the private key using the generated AES key
//
BigInteger privKey;
try {
ParametersWithIV keyWithIV = new ParametersWithIV(aesKey, iv);
CBCBlockCipher blockCipher = new CBCBlockCipher(new AESEngine());
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
cipher.init(false, keyWithIV);
int bufferLength = cipher.getOutputSize(encKeyBytes.length);
byte[] outputBytes = new byte[bufferLength];
int length1 = cipher.processBytes(encKeyBytes, 0, encKeyBytes.length, outputBytes, 0);
int length2 = cipher.doFinal(outputBytes, length1);
int actualLength = length1 + length2;
byte[] privKeyBytes = new byte[actualLength];
System.arraycopy(outputBytes, 0, privKeyBytes, 0, actualLength);
privKey = new BigInteger(privKeyBytes);
} catch (Exception exc) {
throw new ECException("Unable to decrypt the private key", exc);
}
return privKey;
}
示例10: process
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private byte[] process(byte[] data, boolean encryption) throws DataLengthException {
BlockCipher cipher = new AESEngine();
BlockCipherPadding padding = new ZeroBytePadding();
BufferedBlockCipher bufferedCipher = new PaddedBufferedBlockCipher(cipher, padding);
bufferedCipher.init(encryption, key);
byte[] output = new byte[bufferedCipher.getOutputSize(data.length)];
int bytesProcessed = bufferedCipher.processBytes(data, 0, data.length, output, 0);
try {
bufferedCipher.doFinal(output, bytesProcessed);
return output;
} catch (IllegalStateException
| InvalidCipherTextException e) {
e.printStackTrace();
}
return null;
}
示例11: crypt
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private static byte[] crypt(final boolean encrypt, final byte[] bytes, final String password, final byte[] salt) throws InvalidCipherTextException {
final PBEParametersGenerator keyGenerator = new PKCS12ParametersGenerator(new SHA256Digest());
keyGenerator.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password.toCharArray()), salt, 20);
final CipherParameters keyParams = keyGenerator.generateDerivedParameters(256, 128);
final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.init(encrypt, keyParams);
final byte[] processed = new byte[cipher.getOutputSize(bytes.length)];
int outputLength = cipher.processBytes(bytes, 0, bytes.length, processed, 0);
outputLength += cipher.doFinal(processed, outputLength);
final byte[] results = new byte[outputLength];
System.arraycopy(processed, 0, results, 0, outputLength);
return results;
}
示例12: checkPassword
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
public boolean checkPassword(String candidate_password, byte[] raw_password) {
try {
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(false, params);
byte[] buf = new byte[cipher.getOutputSize(raw_password.length)];
int len = cipher.processBytes(raw_password, 0, raw_password.length, buf, 0);
len += cipher.doFinal(buf, len);
return BCrypt.checkpw(candidate_password, new String(buf, 0, len));
} catch (Exception e) {
Loggers.Auth.error("Can't extract hashed password", e);
}
return false;
}
示例13: decryptDESFile
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
public byte[] decryptDESFile(String key, byte[] cipherText) {
BlockCipher engine = new DESEngine();
byte[] bytes = key.getBytes();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
cipher.init(false, new KeyParameter(bytes));
byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];
int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
try {
cipher.doFinal(rv, tam);
} catch (Exception ce) {
ce.printStackTrace();
}
return rv;
}
示例14: createSymmetricKeyWrapper
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
public static BufferedBlockCipher createSymmetricKeyWrapper(boolean forEncryption, BlockCipher engine, byte[] key, byte[] iv)
{
BufferedBlockCipher c = new BufferedBlockCipher(new CFBBlockCipher(engine, engine.getBlockSize() * 8));
c.init(forEncryption, new ParametersWithIV(new KeyParameter(key), iv));
return c;
}
示例15: testCTS
import org.bouncycastle.crypto.BufferedBlockCipher; //導入方法依賴的package包/類
private void testCTS(
int id,
int type,
BlockCipher cipher,
CipherParameters params,
byte[] input,
byte[] output)
throws Exception
{
byte[] out = new byte[input.length];
BufferedBlockCipher engine = new NISTCTSBlockCipher(type, cipher);
engine.init(true, params);
int len = engine.processBytes(input, 0, input.length, out, 0);
engine.doFinal(out, len);
if (!areEqual(output, out))
{
fail(id + " failed encryption expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out)));
}
engine.init(false, params);
len = engine.processBytes(output, 0, output.length, out, 0);
engine.doFinal(out, len);
if (!areEqual(input, out))
{
fail(id + " failed decryption expected " + new String(Hex.encode(input)) + " got " + new String(Hex.encode(out)));
}
}