本文整理汇总了Java中org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal方法的典型用法代码示例。如果您正苦于以下问题:Java PaddedBufferedBlockCipher.doFinal方法的具体用法?Java PaddedBufferedBlockCipher.doFinal怎么用?Java PaddedBufferedBlockCipher.doFinal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher
的用法示例。
在下文中一共展示了PaddedBufferedBlockCipher.doFinal方法的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: decrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
/**
* A password-based data decryption using a constant salt value "<b>constantSalt</b>"
* @param cipher
* @param password
* @param salt
* @param iterationCount
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] cipher, String password) throws Exception
{
PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
char[] passwordChars = password.toCharArray();
final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
pGen.init(pkcs12PasswordBytes, constantSalt.getBytes(), iterations);
CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
aesCBC.init(false, aesCBCParams);
PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding());
byte[] plainTemp = new byte[aesCipher.getOutputSize(cipher.length)];
int offset = aesCipher.processBytes(cipher, 0, cipher.length, plainTemp, 0);
int last = aesCipher.doFinal(plainTemp, offset);
final byte[] plain = new byte[offset + last];
System.arraycopy(plainTemp, 0, plain, 0, plain.length);
return plain;
}
示例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: decryptAESCBC
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) {
// AES CBC PKCS7 decrypt
try {
CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv);
PaddedBufferedBlockCipher cipher
= new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding());
cipher.init(false, cipherParameters);
byte[] buffer = new byte[cipher.getOutputSize(data.length)];
int pos = cipher.processBytes(data, 0, data.length, buffer, 0);
pos += cipher.doFinal(buffer, pos);
return Arrays.copyOf(buffer, pos);
} catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) {
throw new IllegalArgumentException("decrypt failed", ex);
}
}
示例10: 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)));
}
示例11: 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);
}
示例12: processAESCipher
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
private static byte[] processAESCipher(boolean encrypt, byte[] data, SecretKey key, byte[] initVector)
throws DataLengthException, IllegalStateException, InvalidCipherTextException {
// seat up engine, block cipher mode and padding
AESEngine aesEngine = new AESEngine();
CBCBlockCipher cbc = new CBCBlockCipher(aesEngine);
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbc);
// apply parameters
CipherParameters parameters = new ParametersWithIV(new KeyParameter(key.getEncoded()), initVector);
cipher.init(encrypt, parameters);
// process ciphering
byte[] output = new byte[cipher.getOutputSize(data.length)];
int bytesProcessed1 = cipher.processBytes(data, 0, data.length, output, 0);
int bytesProcessed2 = cipher.doFinal(output, bytesProcessed1);
byte[] result = new byte[bytesProcessed1 + bytesProcessed2];
System.arraycopy(output, 0, result, 0, result.length);
return result;
}
示例13: aesDecrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
public static byte[] aesDecrypt(byte[] ivCiphertext, byte[] myPrivateKey, byte[] theirPublicKey, byte[] nonce) {
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);
for (int i = 0; i < 32; i++) {
dhSharedSecret[i] ^= nonce[i];
}
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);
}
}
示例14: blockCheck
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
private void blockCheck(
PaddedBufferedBlockCipher cipher,
BlockCipherPadding padding,
KeyParameter key,
byte[] data)
{
byte[] out = new byte[data.length + 8];
byte[] dec = new byte[data.length];
try
{
cipher.init(true, key);
int len = cipher.processBytes(data, 0, data.length, out, 0);
len += cipher.doFinal(out, len);
cipher.init(false, key);
int decLen = cipher.processBytes(out, 0, len, dec, 0);
decLen += cipher.doFinal(dec, decLen);
if (!areEqual(data, dec))
{
fail("failed to decrypt - i = " + data.length + ", padding = " + padding.getPaddingName());
}
}
catch (Exception e)
{
fail("Exception - " + e.toString(), e);
}
}
示例15: transformData
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入方法依赖的package包/类
/**
* the actual ciphering
*
* @param cipher
* the encyption object
* @param data
* the data to encrypt
* @return the transformed data
* @throws DataLengthException
* @throws IllegalStateException
* @throws InvalidCipherTextException
*/
private byte[] transformData(PaddedBufferedBlockCipher cipher, byte[] data)
throws DataLengthException, IllegalStateException, InvalidCipherTextException {
// allocate a buffer that's big enough
int minSize = cipher.getOutputSize(data.length);
byte[] outBuf = new byte[minSize];
// update the cipher
int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
int length2 = cipher.doFinal(outBuf, length1);
// copy the actual result into a array of the correct length
int actualLength = length1 + length2;
byte[] result = new byte[actualLength];
System.arraycopy(outBuf, 0, result, 0, result.length);
return result;
}