本文整理汇总了Java中org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher类的典型用法代码示例。如果您正苦于以下问题:Java PaddedBufferedBlockCipher类的具体用法?Java PaddedBufferedBlockCipher怎么用?Java PaddedBufferedBlockCipher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PaddedBufferedBlockCipher类属于org.bouncycastle.crypto.paddings包,在下文中一共展示了PaddedBufferedBlockCipher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decrypt
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; //导入依赖的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.paddings.PaddedBufferedBlockCipher; //导入依赖的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.paddings.PaddedBufferedBlockCipher; //导入依赖的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.paddings.PaddedBufferedBlockCipher; //导入依赖的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: 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);
}
}
示例6: 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();
}
示例7: 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+$", "");
}
示例8: 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);
}
}
示例9: 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);
}
}
示例10: 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");
}
}
示例11: 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);
}
示例12: 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;
}
示例13: 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;
}
示例14: 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);
}
示例15: 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);
}