本文整理汇总了Java中javax.crypto.Cipher.updateAAD方法的典型用法代码示例。如果您正苦于以下问题:Java Cipher.updateAAD方法的具体用法?Java Cipher.updateAAD怎么用?Java Cipher.updateAAD使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.Cipher
的用法示例。
在下文中一共展示了Cipher.updateAAD方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: combination_2
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_2(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
c.updateAAD(AAD);
int t = 0;
int offset = 0;
if (plainText.length > ARRAY_OFFSET) {
t = plainText.length - ARRAY_OFFSET;
offset = ARRAY_OFFSET;
}
byte[] part21 = c.update(plainText, 0, t);
byte[] part22 = new byte[c.getOutputSize(plainText.length)];
int len2 = c.doFinal(plainText, t, offset, part22, 0);
int part21Length = part21 != null ? part21.length : 0;
byte[] outputText2 = new byte[part21Length + len2];
if (part21 != null) {
System.arraycopy(part21, 0, outputText2, 0, part21Length);
}
System.arraycopy(part22, 0, outputText2, part21Length, len2);
results.add(outputText2);
}
示例2: combination_12
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_12(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
buf.put(AAD);
buf.position(0);
buf.limit(AAD.length);
Cipher ci = createCipher(mode, params);
ci.updateAAD(buf);
// prepare an empty ByteBuffer
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
emptyBuf.put(new byte[0]);
ci.updateAAD(emptyBuf);
byte[] part12_1 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len12 = ci.update(plainText, 0, plainText.length - offset,
part12_1, 0);
int rest12 = ci.doFinal(plainText, plainText.length - offset, offset,
part12_1, len12);
byte[] outputText12 = new byte[len12 + rest12];
System.arraycopy(part12_1, 0, outputText12, 0, outputText12.length);
results.add(outputText12);
}
示例3: combination_9
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_9(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
buf.put(AAD);
buf.position(0);
buf.limit(AAD.length);
// Get Cipher object and do the combination
Cipher c = createCipher(mode, params);
c.updateAAD(buf);
byte[] part91 = c.update(plainText, 0, plainText.length);
int part91_length = part91 == null ? 0 : part91.length;
byte[] part92 = c.doFinal();
byte[] outputText9 = new byte[part91_length + part92.length];
// form result of the combination
if (part91 != null) {
System.arraycopy(part91, 0, outputText9, 0, part91_length);
}
System.arraycopy(part92, 0, outputText9, part91_length, part92.length);
results.add(outputText9);
}
示例4: getCipherTextBySpec
import javax.crypto.Cipher; //导入方法依赖的package包/类
private byte[] getCipherTextBySpec(GCMParameterSpec spec) throws Exception {
// init a cipher
Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, spec);
cipher.updateAAD(AAD);
byte[] cipherText = cipher.doFinal(data);
// check IVs
if (!Arrays.equals(cipher.getIV(), spec.getIV())) {
System.out.println("IV in parameters is incorrect");
return null;
}
if (spec.getTLen() != (cipherText.length - data.length) * 8) {
System.out.println("Tag length is incorrect");
return null;
}
return cipherText;
}
示例5: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
@Override
public byte[] encrypt(byte[] rawEncryptionKey, byte[] rawData, @Nullable byte[] associatedData) throws AuthenticatedEncryptionException {
if (rawEncryptionKey.length < 16) {
throw new IllegalArgumentException("key length must be longer than 16 byte");
}
try {
byte[] iv = new byte[IV_LENGTH_BYTE];
secureRandom.nextBytes(iv);
final Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(rawEncryptionKey, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
if (associatedData != null) {
cipher.updateAAD(associatedData);
}
byte[] encrypted = cipher.doFinal(rawData);
ByteBuffer byteBuffer = ByteBuffer.allocate(1 + iv.length + encrypted.length);
byteBuffer.put((byte) iv.length);
byteBuffer.put(iv);
byteBuffer.put(encrypted);
return byteBuffer.array();
} catch (Exception e) {
throw new AuthenticatedEncryptionException("could not encrypt", e);
}
}
示例6: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
@Override
public byte[] decrypt(byte[] rawEncryptionKey, byte[] encryptedData, @Nullable byte[] associatedData) throws AuthenticatedEncryptionException {
try {
ByteBuffer byteBuffer = ByteBuffer.wrap(encryptedData);
int ivLength = byteBuffer.get();
byte[] iv = new byte[ivLength];
byteBuffer.get(iv);
byte[] encrypted = new byte[byteBuffer.remaining()];
byteBuffer.get(encrypted);
final Cipher cipher = getCipher();
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(rawEncryptionKey, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));
if (associatedData != null) {
cipher.updateAAD(associatedData);
}
byte[] decrypted = cipher.doFinal(encrypted);
Bytes.wrap(iv).mutable().secureWipe();
Bytes.wrap(rawEncryptionKey).mutable().secureWipe();
Bytes.wrap(encrypted).mutable().secureWipe();
return decrypted;
} catch (Exception e) {
throw new AuthenticatedEncryptionException("could not decrypt", e);
}
}
示例7: execute
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Test AEAD combinations
*
* @param mode decryption or encryption
* @param AAD additional data for AEAD operations
* @param inputText plain text to decrypt/encrypt
* @return output text after encrypt/decrypt
*/
public byte[] execute(int mode, byte[] AAD, byte[] inputText,
AlgorithmParameters params) throws Exception {
Cipher cipher = createCipher(mode, params);
// results of each combination will be saved in the outputTexts
List<byte[]> outputTexts = new ArrayList<>();
// generate a standard outputText using a single-part en/de-cryption
cipher.updateAAD(AAD);
byte[] output = cipher.doFinal(inputText);
// execute multiple-part encryption/decryption combinations
combination_1(outputTexts, mode, AAD, inputText, params);
combination_2(outputTexts, mode, AAD, inputText, params);
combination_3(outputTexts, mode, AAD, inputText, params);
combination_4(outputTexts, mode, AAD, inputText, params);
combination_5(outputTexts, mode, AAD, inputText, params);
combination_6(outputTexts, mode, AAD, inputText, params);
combination_7(outputTexts, mode, AAD, inputText, params);
combination_8(outputTexts, mode, AAD, inputText, params);
combination_9(outputTexts, mode, AAD, inputText, params);
combination_10(outputTexts, mode, AAD, inputText, params);
combination_11(outputTexts, mode, AAD, inputText, params);
combination_12(outputTexts, mode, AAD, inputText, params);
combination_13(outputTexts, mode, AAD, inputText, params);
combination_14(outputTexts, mode, AAD, inputText, params);
combination_15(outputTexts, mode, AAD, inputText, params);
combination_16(outputTexts, mode, AAD, inputText, params);
for (int k = 0; k < outputTexts.size(); k++) {
if (!Arrays.equals(output, outputTexts.get(k))) {
throw new RuntimeException("Combination #" + k + " failed");
}
}
return output;
}
示例8: combination_1
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_1(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
c.updateAAD(AAD);
byte[] part11 = c.update(plainText, 0, plainText.length);
int part11_length = part11 == null ? 0 : part11.length;
byte[] part12 = c.doFinal();
byte[] outputText1 = new byte[part11_length + part12.length];
if (part11 != null) {
System.arraycopy(part11, 0, outputText1, 0, part11_length);
}
System.arraycopy(part12, 0, outputText1, part11_length, part12.length);
results.add(outputText1);
}
示例9: createCipher
import javax.crypto.Cipher; //导入方法依赖的package包/类
private Cipher createCipher(int mode) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(TRANSFORM, PROVIDER);
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, key);
} else {
if (encryptCipher != null) {
cipher.init(Cipher.DECRYPT_MODE, key,
encryptCipher.getParameters());
} else {
throw new RuntimeException("Can't create a cipher");
}
}
cipher.updateAAD(AAD);
return cipher;
}
示例10: combination_11
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_11(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
// prepare ByteBuffer1 to test
ByteBuffer buf1 = ByteBuffer.allocate(AAD.length / 2);
buf1.put(AAD, 0, AAD.length / 2);
buf1.position(0);
buf1.limit(AAD.length / 2);
// get a Cipher object and do combination
Cipher ci = createCipher(mode, params);
// process the first half of AAD data
ci.updateAAD(buf1);
// prepare ByteBuffer2 to test
ByteBuffer buf2 = ByteBuffer.allocate(AAD.length - AAD.length / 2);
buf2.put(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
buf2.position(0);
buf2.limit(AAD.length - AAD.length / 2);
// process the rest of AAD data
ci.updateAAD(buf2);
// encrypt plain text
byte[] part11_1 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len_11 = ci.update(plainText, 0, plainText.length - offset,
part11_1, 0);
byte[] part11_2 = ci.doFinal(plainText, plainText.length - offset,
offset);
byte[] outputText11 = new byte[len_11 + part11_2.length];
System.arraycopy(part11_1, 0, outputText11, 0, len_11);
System.arraycopy(part11_2, 0, outputText11, len_11, part11_2.length);
results.add(outputText11);
}
示例11: combination_5
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_5(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
c.updateAAD(AAD, 0, AAD.length);
byte[] part51 = c.update(plainText, 0, plainText.length);
byte[] part52 = c.doFinal();
int part51Length = part51 != null ? part51.length : 0;
byte[] outputText5 = new byte[part51Length + part52.length];
if (part51 != null) {
System.arraycopy(part51, 0, outputText5, 0, part51Length);
}
System.arraycopy(part52, 0, outputText5, part51Length, part52.length);
results.add(outputText5);
}
示例12: combination_7
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_7(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher ci = createCipher(mode, params);
ci.updateAAD(AAD, 0, AAD.length);
ci.updateAAD(AAD, AAD.length, 0);
byte[] part71 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len = ci.update(plainText, 0, plainText.length - offset, part71, 0);
byte[] part72 = ci.doFinal(plainText, plainText.length - offset, offset);
byte[] outputText7 = new byte[len + part72.length];
System.arraycopy(part71, 0, outputText7, 0, len);
System.arraycopy(part72, 0, outputText7, len, part72.length);
results.add(outputText7);
}
示例13: combination_14
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_14(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
buf.put(AAD);
// process the first half of AAD data
buf.position(0);
buf.limit(AAD.length / 2);
c.updateAAD(buf);
// process the rest of AAD data
buf.limit(AAD.length);
c.updateAAD(buf);
// prepare buffers to encrypt/decrypt
ByteBuffer in = ByteBuffer.allocate(plainText.length);
in.put(plainText);
in.position(0);
in.limit(plainText.length);
ByteBuffer out = ByteBuffer.allocate(c.getOutputSize(in.limit()));
out.position(0);
out.limit(c.getOutputSize(in.limit()));
// process input text
c.update(in, out);
c.doFinal(in, out);
int resultSize = out.position();
byte[] result14 = new byte[resultSize];
out.position(0);
out.limit(resultSize);
out.get(result14, 0, resultSize);
results.add(result14);
}
示例14: combination_16
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_16(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
buf.put(AAD);
buf.position(0);
buf.limit(AAD.length);
c.updateAAD(buf);
// prepare empty ByteBuffer
ByteBuffer emptyBuf = ByteBuffer.allocateDirect(0);
emptyBuf.put(new byte[0]);
c.updateAAD(emptyBuf);
// prepare buffers to encrypt/decrypt
ByteBuffer in = ByteBuffer.allocateDirect(plainText.length);
in.put(plainText);
in.position(0);
in.limit(plainText.length);
ByteBuffer output = ByteBuffer.allocateDirect(
c.getOutputSize(in.limit()));
output.position(0);
output.limit(c.getOutputSize(in.limit()));
// process input text with an empty buffer
c.update(in, output);
ByteBuffer emptyBuf2 = ByteBuffer.allocate(0);
emptyBuf2.put(new byte[0]);
c.doFinal(emptyBuf2, output);
int resultSize = output.position();
byte[] result16 = new byte[resultSize];
output.position(0);
output.limit(resultSize);
output.get(result16, 0, resultSize);
results.add(result16);
}
示例15: combination_10
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_10(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocate(AAD.length);
buf.put(AAD);
buf.position(0);
buf.limit(AAD.length / 2);
// get a Cipher object and do the combination
Cipher c = createCipher(mode, params);
// process the first half of AAD data
c.updateAAD(buf);
// process the rest of AAD data
buf.limit(AAD.length);
c.updateAAD(buf);
// prapare variables for the combination
int t = 0;
int offset = 0;
if (plainText.length > ARRAY_OFFSET) {
t = plainText.length - ARRAY_OFFSET;
offset = ARRAY_OFFSET;
}
// encrypt the text
byte[] part10_1 = c.update(plainText, 0, t);
int part10_1_Length = part10_1 != null ? part10_1.length : 0;
byte[] part10_2 = new byte[c.getOutputSize(plainText.length)];
int len2 = c.doFinal(plainText, t, offset, part10_2, 0);
// form the combination's result
byte[] outputText10 = new byte[part10_1_Length + len2];
if (part10_1 != null) {
System.arraycopy(part10_1, 0, outputText10, 0, part10_1_Length);
}
System.arraycopy(part10_2, 0, outputText10, part10_1_Length, len2);
results.add(outputText10);
}