本文整理汇总了Java中javax.crypto.Cipher.getOutputSize方法的典型用法代码示例。如果您正苦于以下问题:Java Cipher.getOutputSize方法的具体用法?Java Cipher.getOutputSize怎么用?Java Cipher.getOutputSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.Cipher
的用法示例。
在下文中一共展示了Cipher.getOutputSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: combination_6
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_6(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
c.updateAAD(AAD, 0, AAD.length / 2);
c.updateAAD(AAD, AAD.length / 2, AAD.length - AAD.length / 2);
int t = 0;
int offset = 0;
if (plainText.length > ARRAY_OFFSET) {
t = plainText.length - ARRAY_OFFSET;
offset = ARRAY_OFFSET;
}
byte[] part61 = c.update(plainText, 0, t);
byte[] part62 = new byte[c.getOutputSize(plainText.length)];
int len = c.doFinal(plainText, t, offset, part62, 0);
int part61Length = part61 != null ? part61.length : 0;
byte[] outputText6 = new byte[part61Length + len];
if (part61 != null) {
System.arraycopy(part61, 0, outputText6, 0, part61Length);
}
System.arraycopy(part62, 0, outputText6, part61Length, len);
results.add(outputText6);
}
示例2: main
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws ShortBufferException,
IllegalBlockSizeException, BadPaddingException {
byte[] plainText = new byte[801];
// Initialization
RandomFactory.getRandom().nextBytes(plainText);
Cipher ci = new NullCipher();
// Encryption
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// Decryption
byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
// Comparison
if (len != plainText.length ||
!TestUtilities.equalsBlock(plainText, cipherText, len) ||
!TestUtilities.equalsBlock(plainText, recoveredText, len)) {
throw new RuntimeException(
"Test failed because plainText not equal to cipherText and revoveredText");
}
}
示例3: doTestWithSameBuffer
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void doTestWithSameBuffer(int offset, AlgorithmParameters params)
throws Exception {
// calculate output length
Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
int outputLength = c.getOutputSize(textLength);
// prepare byte buffer contained AAD and plain text
int bufSize = AADLength + offset + outputLength;
byte[] AAD_and_Text = Helper.generateBytes(bufSize);
ByteBuffer AAD_and_Text_Buf = ByteBuffer.allocate(bufSize);
AAD_and_Text_Buf.put(AAD_and_Text, 0, AAD_and_Text.length);
// do test
runGCMWithSameBuffer(Cipher.ENCRYPT_MODE, AAD_and_Text_Buf, offset,
textLength, params);
int tagLength = c.getParameters()
.getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
AAD_and_Text_Buf.limit(AADLength + offset + textLength + tagLength);
runGCMWithSameBuffer(Cipher.DECRYPT_MODE, AAD_and_Text_Buf, offset,
textLength + tagLength, params);
}
示例4: 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);
}
示例5: doTestWithSeparateArrays
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void doTestWithSeparateArrays(int offset,
AlgorithmParameters params) throws Exception {
// prepare buffers to test
Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
int outputLength = c.getOutputSize(textLength);
int outputBufSize = outputLength + offset * 2;
byte[] inputText = Helper.generateBytes(outputBufSize);
byte[] AAD = Helper.generateBytes(AADLength);
// do the test
runGCMWithSeparateArray(Cipher.ENCRYPT_MODE, AAD, inputText, offset * 2,
textLength, offset, params);
int tagLength = c.getParameters()
.getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
runGCMWithSeparateArray(Cipher.DECRYPT_MODE, AAD, inputText, offset,
textLength + tagLength, offset, params);
}
示例6: combination_3
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_3(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher ci = createCipher(mode, params);
ci.updateAAD(AAD);
byte[] part31 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len = ci.update(plainText, 0, plainText.length - offset, part31, 0);
byte[] part32 = ci.doFinal(plainText, plainText.length - offset,
offset);
byte[] outputText3 = new byte[len + part32.length];
System.arraycopy(part31, 0, outputText3, 0, len);
System.arraycopy(part32, 0, outputText3, len, part32.length);
results.add(outputText3);
}
示例7: encryptRaw
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Performs encryption using given key only; does not add
* confounder, padding, or checksum. Incoming data to be encrypted
* assumed to have the correct blocksize.
* Ignore key usage.
*/
public byte[] encryptRaw(byte[] baseKey, int usage,
byte[] ivec, byte[] plaintext, int start, int len)
throws GeneralSecurityException, KrbCryptoException {
if (debug) {
System.err.println("usage: " + usage);
if (ivec != null) {
traceOutput("old_state.ivec", ivec, 0, ivec.length);
}
traceOutput("plaintext", plaintext, start, Math.min(len, 32));
traceOutput("baseKey", baseKey, 0, baseKey.length);
}
// Encrypt
Cipher encCipher = getCipher(baseKey, ivec, Cipher.ENCRYPT_MODE);
int blockSize = encCipher.getBlockSize();
if ((len % blockSize) != 0) {
throw new GeneralSecurityException(
"length of data to be encrypted (" + len +
") is not a multiple of the blocksize (" + blockSize + ")");
}
int cipherSize = encCipher.getOutputSize(len);
byte[] ciphertext = new byte[cipherSize];
encCipher.doFinal(plaintext, 0, len, ciphertext, 0);
return ciphertext;
}
示例8: doTestWithSeparatedBuffer
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void doTestWithSeparatedBuffer(int offset,
AlgorithmParameters params) throws Exception {
// prepare AAD byte buffers to test
byte[] AAD = Helper.generateBytes(AADLength);
ByteBuffer AAD_Buf = ByteBuffer.allocate(AADLength);
AAD_Buf.put(AAD, 0, AAD.length);
AAD_Buf.flip();
// prepare text byte buffer to encrypt/decrypt
Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
int outputLength = c.getOutputSize(textLength);
int outputBufSize = outputLength + offset;
byte[] inputText = Helper.generateBytes(outputBufSize);
ByteBuffer plainTextBB = ByteBuffer.allocateDirect(inputText.length);
plainTextBB.put(inputText);
plainTextBB.position(offset);
plainTextBB.limit(offset + textLength);
// do test
runGCMWithSeparateBuffers(Cipher.ENCRYPT_MODE, AAD_Buf, plainTextBB, offset,
textLength, params);
int tagLength = c.getParameters()
.getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
plainTextBB.position(offset);
plainTextBB.limit(offset + textLength + tagLength);
runGCMWithSeparateBuffers(Cipher.DECRYPT_MODE, AAD_Buf, plainTextBB, offset,
textLength + tagLength, params);
}
示例9: AES128Encode
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static byte[] AES128Encode(String str, String str2) throws Throwable {
if (str == null || str2 == null) {
return null;
}
Object bytes = str.getBytes("UTF-8");
Object obj = new byte[16];
System.arraycopy(bytes, 0, obj, 0, Math.min(bytes.length, 16));
byte[] bytes2 = str2.getBytes("UTF-8");
Key secretKeySpec = new SecretKeySpec(obj, "AES");
Cipher instance = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
instance.init(1, secretKeySpec);
byte[] bArr = new byte[instance.getOutputSize(bytes2.length)];
instance.doFinal(bArr, instance.update(bytes2, 0, bytes2.length, bArr, 0));
return bArr;
}
示例10: combination_4
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_4(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher ci = createCipher(mode, params);
ci.updateAAD(AAD);
byte[] part41 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len = ci.update(plainText, 0, plainText.length - offset, part41, 0);
int rest4 = ci.doFinal(plainText, plainText.length - offset, offset,
part41, len);
byte[] outputText4 = new byte[len + rest4];
System.arraycopy(part41, 0, outputText4, 0, outputText4.length);
results.add(outputText4);
}
示例11: 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);
}
示例12: combination_8
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_8(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher ci = createCipher(mode, params);
ci.updateAAD(AAD, 0, 0);
ci.updateAAD(AAD, 0, AAD.length);
byte[] part81 = new byte[ci.getOutputSize(plainText.length)];
int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
int len = ci.update(plainText, 0, plainText.length - offset, part81, 0);
int rest = ci.doFinal(plainText, plainText.length - offset, offset,
part81, len);
byte[] outputText8 = new byte[len + rest];
System.arraycopy(part81, 0, outputText8, 0, outputText8.length);
results.add(outputText8);
}
示例13: runTest
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void runTest(Algorithm algo) throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidKeyException,
InvalidKeySpecException, NoSuchPaddingException,
InvalidAlgorithmParameterException, ShortBufferException,
IllegalBlockSizeException, BadPaddingException {
AlgorithmParameterSpec[] aps = new AlgorithmParameterSpec[1];
byte[] plainText = new byte[800];
SecretKey key1 = algo.intSecurityKey(aps);
Random random = new Random();
// Initialization
SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.toString(),
SUN_JCE);
random.nextBytes(plainText);
Cipher ci = Cipher.getInstance(algo.toString(), SUN_JCE);
// Encryption
ci.init(Cipher.ENCRYPT_MODE, key1, aps[0]);
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// translate key
SecretKey key2 = skf.translateKey(key1);
// Decryption
ci.init(Cipher.DECRYPT_MODE, key2, aps[0]);
byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];
ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
// Comparison
if (!Arrays.equals(plainText, recoveredText)) {
System.out.println("Key1:" + new String(key1.getEncoded())
+ " Key2:" + new String(key2.getEncoded()));
throw new RuntimeException("Testing translate key failed with "
+ algo);
}
}
示例14: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
private static byte[] encrypt(Cipher cipher, byte[] plainText) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
byte[] cipherText = new byte[cipher.getOutputSize(plainText.length)];
int ctLength = cipher.update(plainText, 0, plainText.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
return Arrays.copyOf(cipherText, ctLength);
}
示例15: main
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
boolean failedOnce = false;
Exception failedReason = null;
byte[] keyBytes = new byte[8];
random.nextBytes(keyBytes);
SecretKey key = new SecretKeySpec(keyBytes, "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding", "SunJCE");
cipher.init(Cipher.ENCRYPT_MODE, key);
/*
* Iterate through various sizes to make sure that the code does empty
* blocks, single partial blocks, 1 full block, full + partial blocks,
* multiple full blocks, etc. 5 blocks (using DES) is probably overkill
* but will feel more confident the fix isn't breaking anything.
*/
System.out.println("Output test results for every "
+ outputFrequency + " tests...");
for (int size = 0; size <= testSizes; size++) {
boolean output = (size % outputFrequency) == 0;
if (output) {
System.out.print("\nTesting buffer size: " + size + ":");
}
int outSize = cipher.getOutputSize(size);
try {
encrypt(cipher, size,
ByteBuffer.allocate(size),
ByteBuffer.allocate(outSize),
ByteBuffer.allocateDirect(size),
ByteBuffer.allocateDirect(outSize),
output);
} catch (Exception e) {
System.out.print("\n Failed with size " + size);
failedOnce = true;
failedReason = e;
// If we got an exception, let's be safe for future
// testing and reset the cipher to a known good state.
cipher.init(Cipher.ENCRYPT_MODE, key);
}
}
if (failedOnce) {
throw failedReason;
}
System.out.println("\nTest Passed...");
}