本文整理汇总了Java中org.bouncycastle.crypto.engines.AESEngine类的典型用法代码示例。如果您正苦于以下问题:Java AESEngine类的具体用法?Java AESEngine怎么用?Java AESEngine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AESEngine类属于org.bouncycastle.crypto.engines包,在下文中一共展示了AESEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: EncryptAes256
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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);
}
}
示例2: decrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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;
}
示例3: scp03_kdf
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
private static byte[] scp03_kdf(byte[] key, byte constant, byte[] context, int blocklen_bits) {
// 11 bytes
byte[] label = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try {
bo.write(label); // 11 bytes of label
bo.write(constant); // constant for the last byte
bo.write(0x00); // separator
bo.write((blocklen_bits >> 8) & 0xFF); // block size in two bytes
bo.write(blocklen_bits & 0xFF);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
byte[] blocka = bo.toByteArray();
byte[] blockb = context;
BlockCipher cipher = new AESEngine();
CMac cmac = new CMac(cipher);
KDFCounterBytesGenerator kdf = new KDFCounterBytesGenerator(cmac);
kdf.init(new KDFCounterParameters(key, blocka, blockb, 8)); // counter size in bits
byte[] cgram = new byte[blocklen_bits / 8];
kdf.generateBytes(cgram, 0, cgram.length);
return cgram;
}
示例4: createRFC3211Wrapper
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
static Wrapper createRFC3211Wrapper(ASN1ObjectIdentifier algorithm)
throws CMSException
{
if (NISTObjectIdentifiers.id_aes128_CBC.equals(algorithm)
|| NISTObjectIdentifiers.id_aes192_CBC.equals(algorithm)
|| NISTObjectIdentifiers.id_aes256_CBC.equals(algorithm))
{
return new RFC3211WrapEngine(new AESEngine());
}
else if (PKCSObjectIdentifiers.des_EDE3_CBC.equals(algorithm))
{
return new RFC3211WrapEngine(new DESedeEngine());
}
else if (OIWObjectIdentifiers.desCBC.equals(algorithm))
{
return new RFC3211WrapEngine(new DESEngine());
}
else if (PKCSObjectIdentifiers.RC2_CBC.equals(algorithm))
{
return new RFC3211WrapEngine(new RC2Engine());
}
else
{
throw new CMSException("cannot recognise wrapper: " + algorithm);
}
}
示例5: aesEncrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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: compareModes
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
@Test
public void compareModes() {
BlockCipher engine = new AESEngine();
int blockSize = engine.getBlockSize();
BlockCipher ref = new SICBlockCipher(engine); // reference implementation
BlockCipher uut = new CtrBlockCipher(engine); // unit under test
PBEParametersGenerator gen = new PKCS5S2ParametersGenerator();
byte[] salt = new byte[blockSize]; // used as salt and cipher input
new SecureRandom().nextBytes(salt);
gen.init("top secret".getBytes(), salt, 1);
ParametersWithIV
param = (ParametersWithIV) gen.generateDerivedParameters(
blockSize * 8,
blockSize * 8);
ref.init(true, param);
uut.init(true, param);
assertModes(ref, uut);
ref.init(false, param);
uut.init(false, param);
assertModes(ref, uut);
}
示例7: aesEncrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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);
}
}
示例8: aesDecrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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);
}
}
示例9: initBlockCipherEngines
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
private static void initBlockCipherEngines() {
blockCipherEngines.put("MARS", MarsEngine.class);
blockCipherEngines.put("AES", AESEngine.class);
blockCipherEngines.put("Blowfish", BlowfishEngine.class);
blockCipherEngines.put("Camellia", CamelliaEngine.class);
blockCipherEngines.put("CAST5", CAST5Engine.class);
blockCipherEngines.put("CAST6", CAST6Engine.class);
blockCipherEngines.put("DESede", DESedeEngine.class);
blockCipherEngines.put("DES", DESEngine.class);
blockCipherEngines.put("GOST28147", GOST28147Engine.class);
blockCipherEngines.put("IDEA", IDEAEngine.class);
blockCipherEngines.put("Noekeon", NoekeonEngine.class);
blockCipherEngines.put("RC2", RC2Engine.class);
blockCipherEngines.put("RC5", RC532Engine.class);
blockCipherEngines.put("RC6", RC6Engine.class);
blockCipherEngines.put("SEED", SEEDEngine.class);
blockCipherEngines.put("Serpent", SerpentEngine.class);
blockCipherEngines.put("Shacal2", Shacal2Engine.class);
blockCipherEngines.put("Skipjack", SkipjackEngine.class);
blockCipherEngines.put("SM4", SM4Engine.class);
blockCipherEngines.put("TEA", TEAEngine.class);
blockCipherEngines.put("Twofish", TwofishEngine.class);
blockCipherEngines.put("XTEA", XTEAEngine.class);
blockCipherEngines.put("Threefish", ThreefishEngine.class);
}
示例10: InitCiphers
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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);
}
示例11: decrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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;
}
示例12: encrypt
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
private static final byte[] encrypt(byte[] key, byte[] data) throws Exception
{
PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
CipherParameters p = new KeyParameter(key);
try
{
SecretByteArrayOutputStream ba = new SecretByteArrayOutputStream();
try
{
XCipherOutputStream out = new XCipherOutputStream(c, p, ba);
out.write(data);
out.close();
byte[] b = ba.toByteArray();
return b;
}
finally
{
CKit.close(ba);
}
}
finally
{
Crypto.zero(p);
}
}
示例13: EAXEncryptStream
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
public EAXEncryptStream(byte[] key, byte[] nonce, byte[] associatedData, OutputStream os)
{
if(key.length != KEY_LENGTH_BYTES)
{
throw new IllegalArgumentException("key must be " + KEY_LENGTH_BYTES*8 + " bits");
}
this.cipher = new EAXBlockCipher(new AESEngine());
this.os = os;
keyParameter = new KeyParameter(key);
AEADParameters par = new AEADParameters(keyParameter, EAXDecryptStream.MAC_LEN_BITS, nonce, associatedData);
cipher.init(true, par);
out = new byte[BUFFER_SIZE];
}
示例14: EAXDecryptStream
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的package包/类
public EAXDecryptStream(byte[] key, byte[] nonce, byte[] associatedData, InputStream in)
{
if(key.length != KEY_LENGTH_BYTES)
{
throw new IllegalArgumentException("key must be " + KEY_LENGTH_BYTES*8 + " bits");
}
this.in = in;
this.cipher = new EAXBlockCipher(new AESEngine());
keyParameter = new KeyParameter(key);
AEADParameters par = new AEADParameters(keyParameter, MAC_LEN_BITS, nonce, associatedData);
cipher.init(false, par);
out = new byte[BUFFER_SIZE];
buf = new byte[BUFFER_SIZE];
}
示例15: encryptedOutputStream
import org.bouncycastle.crypto.engines.AESEngine; //导入依赖的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);
}
}