本文整理汇总了Java中org.spongycastle.crypto.StreamCipher.processBytes方法的典型用法代码示例。如果您正苦于以下问题:Java StreamCipher.processBytes方法的具体用法?Java StreamCipher.processBytes怎么用?Java StreamCipher.processBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.spongycastle.crypto.StreamCipher
的用法示例。
在下文中一共展示了StreamCipher.processBytes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enChacha
import org.spongycastle.crypto.StreamCipher; //导入方法依赖的package包/类
public static byte[] enChacha(byte[] raw, byte[] key, byte[] iv) {
boolean encrypt = true;
CipherParameters cp = new KeyParameter(key);
ParametersWithIV params = new ParametersWithIV(cp, iv);
StreamCipher engine = new ChaChaEngine();
//noinspection ConstantConditions
engine.init(encrypt, params);
byte[] ciphertext = new byte[raw.length];
engine.processBytes(raw, 0, raw.length, ciphertext, 0);
byte[] macKeyBytes = Arrays.copyOf(key, key.length);
Poly1305KeyGenerator.clamp(macKeyBytes);
KeyParameter macKey = new KeyParameter(macKeyBytes); //initRecord(engine, encrypt, 0, iv);
byte[] mac = calculateMAC(macKey, ciphertext, 0, ciphertext.length);
byte[] res = new byte[ciphertext.length + mac.length];
System.arraycopy(ciphertext, 0, res, 0, ciphertext.length);
System.arraycopy(mac, 0, res, ciphertext.length, mac.length);
KeyUtil.erase(ciphertext);
KeyUtil.erase(mac);
return res;
}
示例2: deChacha
import org.spongycastle.crypto.StreamCipher; //导入方法依赖的package包/类
public static byte[] deChacha(byte[] ciphertext, byte[] key, byte[] iv) throws MacMismatchException {
boolean encrypt = false;
CipherParameters cp = new KeyParameter(key);
ParametersWithIV params = new ParametersWithIV(cp, iv);
StreamCipher engine = new ChaChaEngine();
//noinspection ConstantConditions
engine.init(encrypt, params);
if (getPlaintextLimit(ciphertext.length) < 0) {
throw new IllegalArgumentException();
}
byte[] macKeyBytes = Arrays.copyOf(key, key.length);
Poly1305KeyGenerator.clamp(macKeyBytes);
KeyParameter macKey = new KeyParameter(macKeyBytes); //initRecord(engine, encrypt, 0, iv);
int plaintextLength = ciphertext.length - 16;
byte[] calculatedMAC = calculateMAC(macKey, ciphertext, 0, plaintextLength);
byte[] receivedMAC = Arrays.copyOfRange(ciphertext, ciphertext.length - 16, ciphertext.length);
if (!Arrays.constantTimeAreEqual(calculatedMAC, receivedMAC)) {
throw new MacMismatchException();
}
byte[] output = new byte[plaintextLength];
engine.processBytes(ciphertext, 0, plaintextLength, output, 0);
KeyUtil.erase(calculatedMAC);
KeyUtil.erase(receivedMAC);
return output;
}
示例3: generateRecordMACKey
import org.spongycastle.crypto.StreamCipher; //导入方法依赖的package包/类
private static KeyParameter generateRecordMACKey(StreamCipher cipher) {
byte[] firstBlock = new byte[64];
cipher.processBytes(firstBlock, 0, firstBlock.length, firstBlock, 0);
KeyParameter macKey = new KeyParameter(firstBlock, 0, 32);
Arrays.fill(firstBlock, (byte) 0);
return macKey;
}