本文整理匯總了Java中org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.init方法的典型用法代碼示例。如果您正苦於以下問題:Java PaddedBufferedBlockCipher.init方法的具體用法?Java PaddedBufferedBlockCipher.init怎麽用?Java PaddedBufferedBlockCipher.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher
的用法示例。
在下文中一共展示了PaddedBufferedBlockCipher.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: aesEncrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
try {
byte[] dhSharedSecret = new byte[32];
Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
for (int i = 0; i < 32; i++) {
dhSharedSecret[i] ^= nonce[i];
}
byte[] key = sha256().digest(dhSharedSecret);
byte[] iv = new byte[16];
secureRandom.get().nextBytes(iv);
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(true, ivAndKey);
byte[] output = new byte[aes.getOutputSize(plaintext.length)];
int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
ciphertextLength += aes.doFinal(output, ciphertextLength);
byte[] result = new byte[iv.length + ciphertextLength];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(output, 0, result, iv.length, ciphertextLength);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例2: encrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
/**
* Encrypt.
*
* @param instr the instr
* @return the string
* @throws java.security.GeneralSecurityException the general security exception
*/
@Override
public String encrypt(String instr) throws GeneralSecurityException {
long t1 = System.currentTimeMillis();
byte[] in = instr.getBytes();
PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new DESedeEngine()));
encryptor.init(true, keyParameter);
byte[] cipherText = new byte[encryptor.getOutputSize(in.length)];
int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0);
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
encryptor.doFinal(cipherText, outputLen);
Hex.encode(cipherText, os);
} catch (Exception e) {
e.printStackTrace();
throw new GeneralSecurityException(e);
}
long t2 = System.currentTimeMillis();
logger.debug("Time taken to encrypt(millis) :" + (t2 - t1));
return ENC_PREFIX + os.toString();
}
示例3: decryptStr
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
private String decryptStr(String instr) throws GeneralSecurityException {
if(StringUtils.isEmpty(instr)){
return instr;
}
long t1 = System.currentTimeMillis();
PaddedBufferedBlockCipher decryptor = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new DESedeEngine()));
decryptor.init(false, keyParameter);
byte[] in = null;
byte[] cipherText = null;
try {
in = Hex.decode(instr);
cipherText = new byte[decryptor.getOutputSize(in.length)];
int outputLen = decryptor.processBytes(in, 0, in.length, cipherText, 0);
decryptor.doFinal(cipherText, outputLen);
} catch (Exception e) {
throw new GeneralSecurityException(e);
}
long t2 = System.currentTimeMillis();
logger.debug("Time taken to decrypt(millis) : " + (t2 - t1));
return (new String(cipherText)).replaceAll("\\u0000+$", "");
}
示例4: aesEncrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
public static byte[] aesEncrypt(byte[] plaintext, byte[] myPrivateKey, byte[] theirPublicKey) {
try {
byte[] dhSharedSecret = new byte[32];
Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
byte[] key = sha256().digest(dhSharedSecret);
byte[] iv = new byte[16];
secureRandom.get().nextBytes(iv);
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(true, ivAndKey);
byte[] output = new byte[aes.getOutputSize(plaintext.length)];
int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
ciphertextLength += aes.doFinal(output, ciphertextLength);
byte[] result = new byte[iv.length + ciphertextLength];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(output, 0, result, iv.length, ciphertextLength);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例5: aesDecrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte theirPublicKey[]) {
try {
if (ivCiphertext.length < 16 || ivCiphertext.length % 16 != 0) {
throw new InvalidCipherTextException("invalid ciphertext");
}
byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
byte[] dhSharedSecret = new byte[32];
Curve25519.curve(dhSharedSecret, myPrivateKey, theirPublicKey);
byte[] key = sha256().digest(dhSharedSecret);
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(false, ivAndKey);
byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
plaintextLength += aes.doFinal(output, plaintextLength);
byte[] result = new byte[plaintextLength];
System.arraycopy(output, 0, result, 0, result.length);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
示例6: doCbc
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
private void doCbc(byte[] key, byte[] iv, byte[] pt, byte[] expected)
throws Exception
{
PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(new SerpentEngine()), new PKCS7Padding());
byte[] ct = new byte[expected.length];
c.init(true, new ParametersWithIV(new KeyParameter(key), iv));
int l = c.processBytes(pt, 0, pt.length, ct, 0);
c.doFinal(ct, l);
if (!Arrays.areEqual(expected, ct))
{
fail("CBC test failed");
}
}
示例7: InitCiphers
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
public void InitCiphers() {
// create the ciphers
// AES block cipher in CBC mode with padding
encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESEngine()));
decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESEngine()));
// create the IV parameter
ParametersWithIV parameterIV = new ParametersWithIV(new KeyParameter(
key), IV);
encryptCipher.init(true, parameterIV);
decryptCipher.init(false, parameterIV);
}
示例8: operate
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
private static byte[] operate(Encryption encryptionAlgorithm, boolean doEncrypt, byte[] subject, byte[] key, byte[] iv) throws CryptoException
{
// set up padded buffered cipher
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
CipherBuilder.buildCipher(encryptionAlgorithm, doEncrypt, key, iv), new PKCS7Padding()
);
// init with key and if
cipher.init(doEncrypt, new ParametersWithIV(new KeyParameter(key), iv));
// construct output buffer
byte[] output = new byte[cipher.getOutputSize(subject.length)];
// process all da bytes
int cursor = cipher.processBytes(subject, 0, subject.length, output, 0);
// process the last bytes from the buffer
cipher.doFinal(output, cursor);
return output;
}
示例9: decryptDataAES256
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
/**
* Encrypt or decrypt data with AES256.
*
* @param data The data to encrypt.
* @param output The output to write the encrypted data to.
*
* @throws IOException If there is an error reading the data.
*/
private void decryptDataAES256(InputStream data, OutputStream output) throws IOException
{
byte[] iv = new byte[16];
// read IV from stream
int ivSize = data.read(iv);
if (ivSize == -1)
{
return;
}
if (ivSize != iv.length)
{
throw new IOException("AES initialization vector not fully read: only " + ivSize
+ " bytes read instead of " + iv.length);
}
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESFastEngine()));
cipher.init(false, new ParametersWithIV(new KeyParameter(encryptionKey), iv));
try (CipherInputStream cis = new CipherInputStream(data, cipher))
{
org.apache.commons.io.IOUtils.copy(cis, output);
}
}
示例10: initCiphers
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
private void initCiphers(byte[] key, byte[] iv) {
// get the keyBytes
keyBytes = new byte[key.length];
System.arraycopy(key, 0, keyBytes, 0, key.length);
keyP = new KeyParameter(keyBytes);
// get the IV
IV = new byte[blockSize];
System.arraycopy(iv, 0, IV, 0, IV.length);
// create the ciphers
// AES block cipher in CBC mode with ISO7816d4 padding
encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESFastEngine()), new ISO7816d4Padding());
decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESFastEngine()), new ISO7816d4Padding());
// create the IV parameter
ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);
encryptCipher.init(true, parameterIV);
decryptCipher.init(false, parameterIV);
}
示例11: initCiphers
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
private void initCiphers(byte[] key, byte[] iv) {
// get the keyBytes
keyBytes = new byte[key.length];
System.arraycopy(key, 0, keyBytes, 0, key.length);
// get the IV
IV = new byte[blockSize];
System.arraycopy(iv, 0, IV, 0, iv.length);
keyP = new KeyParameter(keyBytes);
encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new DESedeEngine()), new ISO7816d4Padding());
decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new DESedeEngine()), new ISO7816d4Padding());
// create the IV parameter
ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);
encryptCipher.init(true, parameterIV);
decryptCipher.init(false, parameterIV);
}
示例12: encryptedOutputStream
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
@Override
public OutputStream encryptedOutputStream(final Path path, final String password) throws IOException,
EncryptionFailedException {
try {
final byte[] salt = generateSalt();
final byte[] key = generateKey(password, salt);
final byte[] iv = generateIV();
final byte[] fileInitBlock = generateOutputInitBlock(salt, iv);
final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
final KeyParameter keyParam = new KeyParameter(key);
final CipherParameters params = new ParametersWithIV(keyParam, iv);
cipher.init(true, params);
final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
out.write(fileInitBlock);
return new CipherOutputStream(out, cipher);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new EncryptionFailedException(e);
}
}
示例13: decryptedInputStream
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
@Override
public InputStream decryptedInputStream(final Path path, final String password) throws IOException,
DecryptionFailedException {
try {
InputStream in = new BufferedInputStream(Files.newInputStream(path));
byte[] initBlock = readInitBlock(in);
byte[] salt = extractSalt(initBlock);
byte[] iv = extractIV(initBlock);
byte[] key = generateKey(password, salt);
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
new PKCS7Padding());
KeyParameter keyParam = new KeyParameter(key);
CipherParameters params = new ParametersWithIV(keyParam, iv);
cipher.init(false, params);
return new CipherInputStream(in, cipher);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new DecryptionFailedException(e);
}
}
示例14: test
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
@Test
public void test() throws DataLengthException, IllegalStateException, InvalidCipherTextException {
PaddedBufferedBlockCipher encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
PaddedBufferedBlockCipher decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
byte inBuff[] = "Hello Wd".getBytes();
byte[] outBuff = new byte[512];
byte[] keyBytes = "TestTestTestTest".getBytes();
byte[] uncipherData = new byte[8];
encryptCipher.init(true, new KeyParameter(keyBytes));
decryptCipher.init(false, new KeyParameter(keyBytes));
encryptCipher.processBytes(inBuff, 0, inBuff.length, outBuff, 0);
encryptCipher.doFinal(outBuff, 0);
decryptCipher.processBytes(outBuff, 0, 2*inBuff.length, uncipherData, 0);
decryptCipher.doFinal(uncipherData, 0);
log.debug("Uncipher Data: {}", uncipherData);
assertTrue("Hello Wd".equals(new String(uncipherData)));
}
示例15: decipher
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //導入方法依賴的package包/類
public String decipher(byte[] key, String cipherText) throws InvalidCipherTextException {
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
cipher.init(false, new KeyParameter(Hex.decode(key)));
byte[] cipherTextBytes = java.util.Base64.getDecoder().decode(cipherText);
byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)];
int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
cipher.doFinal(plainTextBytes, outputLength);
int paddingStarts = plainTextBytes.length - 1;
for (; paddingStarts >= 0 ; paddingStarts--) {
if (plainTextBytes[paddingStarts] != 0) {
break;
}
}
return new String(plainTextBytes, 0, paddingStarts + 1);
}