use of org.xwiki.crypto.cipher.Cipher in project xwiki-commons by xwiki.
the class BcRsaOaepCipherFactoryTest method testRSAEncryptionDecryptionProgressive.
@Test
public void testRSAEncryptionDecryptionProgressive() throws Exception {
Cipher cipher = factory.getInstance(true, publicKey);
cipher.update(input, 0, 17);
cipher.update(input, 17, 1);
cipher.update(input, 18, input.length - 18);
byte[] encrypted = cipher.doFinal();
cipher = factory.getInstance(false, privateKey);
cipher.update(encrypted, 0, 65);
cipher.update(encrypted, 65, 1);
cipher.update(encrypted, 66, encrypted.length - 66);
assertThat(cipher.doFinal(), equalTo(input));
cipher = factory.getInstance(true, privateKey);
cipher.update(input, 0, 15);
cipher.update(input, 15, 1);
encrypted = cipher.doFinal(input, 16, input.length - 16);
cipher = factory.getInstance(false, publicKey);
cipher.update(encrypted);
assertThat(cipher.doFinal(), equalTo(input));
}
use of org.xwiki.crypto.cipher.Cipher in project xwiki-commons by xwiki.
the class AbstractSymmetricCipherFactoryTest method getProgressive.
private byte[] getProgressive(boolean forEncryption, byte[] bytes, int size) throws Exception {
Cipher cipher = getCipher(forEncryption);
byte[] result = new byte[size];
byte[] tmp;
int len = 0;
tmp = cipher.update(bytes, 0, BLOCK_SIZE + 1);
assertThat(tmp, not(nullValue()));
System.arraycopy(tmp, 0, result, 0, len = tmp.length);
assertThat(cipher.update(bytes, BLOCK_SIZE + 1, BLOCK_SIZE - 1), nullValue());
tmp = cipher.update(bytes, BLOCK_SIZE * 2, 1);
assertThat(tmp, not(nullValue()));
System.arraycopy(tmp, 0, result, len, tmp.length);
len += tmp.length;
tmp = cipher.update(bytes, ((BLOCK_SIZE * 2) + 1), bytes.length - ((BLOCK_SIZE * 2) + 1));
assertThat(tmp, not(nullValue()));
System.arraycopy(tmp, 0, result, len, tmp.length);
len += tmp.length;
tmp = cipher.doFinal();
if (forEncryption || tmp != null) {
assertThat(tmp, not(nullValue()));
System.arraycopy(tmp, 0, result, len, tmp.length);
len += tmp.length;
}
return result;
}
use of org.xwiki.crypto.cipher.Cipher in project xwiki-commons by xwiki.
the class AbstractSymmetricCipherFactoryTest method cipherOneShotDecryption.
@Test
public void cipherOneShotDecryption() throws Exception {
Cipher cipher = getCipher(false);
byte[] result = cipher.doFinal(getEncrypted());
assertThat(result.length, equalTo(BYTES.length));
assertThat(result, equalTo(BYTES));
result = cipher.doFinal(getAnotherEncrypted());
assertThat(result.length, equalTo(ANOTHER_BYTES.length));
assertThat(result, equalTo(ANOTHER_BYTES));
}
use of org.xwiki.crypto.cipher.Cipher in project xwiki-commons by xwiki.
the class AbstractSymmetricCipherFactoryTest method fetCipherProperties.
@Test
public void fetCipherProperties() throws Exception {
Cipher cipher = getCipher(true);
assertThat(cipher.getAlgorithmName(), equalTo(CIPHER_ALGO));
assertThat(cipher.getOutputBlockSize(), equalTo(BLOCK_SIZE));
assertThat(cipher.isForEncryption(), is(true));
cipher = getCipher(false);
assertThat(cipher.getAlgorithmName(), equalTo(CIPHER_ALGO));
assertThat(cipher.getOutputBlockSize(), equalTo(BLOCK_SIZE));
assertThat(cipher.isForEncryption(), is(false));
}
use of org.xwiki.crypto.cipher.Cipher in project xwiki-commons by xwiki.
the class AbstractSymmetricCipherFactoryTest method cipherOneShotEncryption.
@Test
public void cipherOneShotEncryption() throws Exception {
Cipher cipher = getCipher(true);
assertThat(getEncrypted().length, equalTo(BYTES_ENCRYPTED_SIZE));
assertThat(cipher.doFinal(BYTES), equalTo(getEncrypted()));
assertThat(getAnotherEncrypted().length, equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE));
assertThat(cipher.doFinal(ANOTHER_BYTES), equalTo(getAnotherEncrypted()));
}
Aggregations