本文整理汇总了Java中org.bouncycastle.crypto.BufferedBlockCipher.reset方法的典型用法代码示例。如果您正苦于以下问题:Java BufferedBlockCipher.reset方法的具体用法?Java BufferedBlockCipher.reset怎么用?Java BufferedBlockCipher.reset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.BufferedBlockCipher
的用法示例。
在下文中一共展示了BufferedBlockCipher.reset方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: encrypt
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
public static byte[] encrypt(byte[] input, BufferedBlockCipher cipher) {
synchronized(cipher) {
cipher.reset();
byte[] cipherText = new byte[cipher.getOutputSize(input.length + pad.length)];
//Write out the pad
int outputLen = cipher.processBytes(pad, 0, pad.length, cipherText, 0);
outputLen += cipher.processBytes(input, 0, input.length, cipherText, outputLen);
try {
cipher.doFinal(cipherText, outputLen);
} catch(CryptoException e) {
Logger.die("process", e);
}
return cipherText;
}
}
示例4: getHashedPassword
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
/**
* @return AES(BCrypt(clear_password, 10), SHA256(master_password_key))
*/
public byte[] getHashedPassword(String clear_password) throws SecurityException {
String tested_password = testIfPasswordIsStrong(clear_password);
try {
byte[] hashed = (BCrypt.hashpw(tested_password, BCrypt.gensalt(10))).getBytes("UTF-8");
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(true, params);
byte[] buf = new byte[cipher.getOutputSize(hashed.length)];
int len = cipher.processBytes(hashed, 0, hashed.length, buf, 0);
len += cipher.doFinal(buf, len);
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
return out;
} catch (Exception e) {
Loggers.Auth.error("Can't prepare password", e);
}
return null;
}
示例5: 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;
}
示例6: transform
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
private static String transform(byte[] key, byte[] iv, String encString, boolean encrypt)
throws Exception {
// setup cipher parameters with key and IV
KeyParameter keyParam = new KeyParameter(key);
CipherParameters params = new ParametersWithIV(keyParam, iv);
// setup AES cipher in CBC mode with PKCS7 padding
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher =
new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(encrypt, params);
// create a temporary buffer to decode into (it'll include padding)
byte[] encData = ByteUtilities.toByteArray(encString);
byte[] buf = new byte[cipher.getOutputSize(encData.length)];
int len = cipher.processBytes(encData, 0, encData.length, buf, 0);
len += cipher.doFinal(buf, len);
// remove padding
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// return string representation of decoded bytes
return ByteUtilities.toHexString(out);
}
示例7: deserialize
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
public static Curve25519PrivateKey deserialize(InputStream in, char[] password) throws IOException {
try {
// check magic number
byte[] mn = new byte[MagicNumbers.PRIVATE_KEY.length];
IOUtils.readFully(in, mn, 0, mn.length);
if (!Arrays.areEqual(mn, MagicNumbers.PRIVATE_KEY))
throw new IllegalArgumentException("Wrong key file format");
// read initial vector
byte[] iv = new byte[16];
IOUtils.readFully(in, iv, 0, iv.length);
// read salt
byte[] salt = new byte[64];
IOUtils.readFully(in, salt, 0, salt.length);
// initialize cipher
CipherParameters params = new ParametersWithIV(new KeyParameter(deriveKey(password, salt)), iv);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.reset();
cipher.init(false, params);
// decrypt key
CipherInputStream cin = new CipherInputStream(in, cipher);
byte[] key = new byte[Curve25519.KEY_SIZE];
IOUtils.readFully(cin, key, 0, key.length);
// return key instance
return new Curve25519PrivateKey(key);
} catch (UnsupportedEncodingException ex) {
throw new UnsupportedOperationException(ex.getMessage(), ex);
}
}
示例8: encryptBouncyCastle
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
private static String encryptBouncyCastle(SecretKey secret, String plainText) {
try {
// prepending with md5 hash allows us to do an integrity check on decrypt to prevent returning garbage if the decrypt key is incorrect
String md5 = HashUtils.md5(plainText);
plainText = md5 + plainText;
// the iv acts as a per use salt, this ensures things encrypted with the same key always have a unique salt
// 128 bit iv because NIST AES is standardized with 128 bit blocks and iv needs to match block size, even when using 256 bit key
byte[] iv = new byte[16];
SECURE_RANDOM.nextBytes(iv);
// setup cipher parameters with key and IV
byte[] key = secret.getEncoded();
// setup AES cipher in CBC mode with PKCS7 padding
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.reset();
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
byte[] plainTextBuf = plainText.getBytes(StandardCharsets.UTF_8);
byte[] buf = new byte[cipher.getOutputSize(plainTextBuf.length)];
int len = cipher.processBytes(plainTextBuf, 0, plainTextBuf.length, buf, 0);
len += cipher.doFinal(buf, len);
// copy the encrypted part of the buffer to out
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// iv$encrypted
return byteArrayToHexString(iv) + "$" + new String(Base64.encodeBase64URLSafe(out), StandardCharsets.UTF_8);
} catch (DataLengthException | InvalidCipherTextException e) {
throw new IllegalStateException("cannot encrypt", e);
}
}
示例9: dec
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
public static String dec(String password, String salt, String encString)
throws Exception {
byte[] ivData = toByte(encString.substring(0, 32));
byte[] encData = toByte(encString.substring(32));
// get raw key from password and salt
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
toByte(salt), 50, 256);
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("PBEWithSHA256And256BitAES-CBC-BC");
SecretKeySpec secretKey = new SecretKeySpec(keyFactory.generateSecret(
pbeKeySpec).getEncoded(), "AES");
byte[] key = secretKey.getEncoded();
// setup cipher parameters with key and IV
KeyParameter keyParam = new KeyParameter(key);
CipherParameters params = new ParametersWithIV(keyParam, ivData);
// setup AES cipher in CBC mode with PKCS7 padding
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new CBCBlockCipher(new AESEngine()), padding);
cipher.reset();
cipher.init(false, params);
// create a temporary buffer to decode into (it'll include padding)
byte[] buf = new byte[cipher.getOutputSize(encData.length)];
int len = cipher.processBytes(encData, 0, encData.length, buf, 0);
len += cipher.doFinal(buf, len);
// remove padding
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// return string representation of decoded bytes
return new String(out, "UTF-8");
}
示例10: encryptDecrypt
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
private byte[] encryptDecrypt(byte[] datas, int pos, int len, int mode) throws GeneralSecurityException, DataLengthException, IllegalStateException, InvalidCipherTextException {
if (log.isTraceEnabled()) {
if (mode == Cipher.ENCRYPT_MODE) {
log.trace("Raw data input (no crypted)" + Hexview.LINESEPARATOR + Hexview.tracelog(datas, pos, len));
} else {
log.trace("Raw data input (crypted)" + Hexview.LINESEPARATOR + Hexview.tracelog(datas, pos, len));
}
}
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher2 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);
cipher2.reset();
cipher2.init(mode == Cipher.ENCRYPT_MODE, params);
byte[] buf = new byte[cipher2.getOutputSize(len)];
int len2 = cipher2.processBytes(datas, pos, len, buf, 0);
len2 += cipher2.doFinal(buf, len2);
byte[] result = new byte[len2];
System.arraycopy(buf, 0, result, 0, len2);
if (log.isTraceEnabled()) {
if (mode == Cipher.ENCRYPT_MODE) {
log.trace("Raw data input (crypted)" + Hexview.LINESEPARATOR + Hexview.tracelog(result, 0, result.length));
} else {
log.trace("Raw data input (decrypted)" + Hexview.LINESEPARATOR + Hexview.tracelog(result, 0, result.length));
}
}
return result;
}
示例11: serialize
import org.bouncycastle.crypto.BufferedBlockCipher; //导入方法依赖的package包/类
public void serialize(OutputStream out, char[] password) throws IOException {
try {
// generate initial vector
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
byte[] iv = new byte[16];
random.nextBytes(iv);
// generate salt
byte[] salt = new byte[64];
random.nextBytes(salt);
// initialize cipher
CipherParameters params = new ParametersWithIV(new KeyParameter(deriveKey(password, salt)), iv);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
cipher.reset();
cipher.init(true, params);
// write magic number
out.write(MagicNumbers.PRIVATE_KEY);
out.flush();
// write initial vector and salt
out.write(iv);
out.write(salt);
out.flush();
// write encrypted key to output stream
ByteArrayOutputStream buf = new ByteArrayOutputStream();
CipherOutputStream cout = new CipherOutputStream(buf, cipher);
cout.write(key);
cout.close();
out.write(buf.toByteArray());
out.flush();
} catch (UnsupportedEncodingException | NoSuchAlgorithmException ex) {
throw new UnsupportedOperationException(ex.getMessage(), ex);
}
}