Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniStreaming2Kmip.java
package com.p6r.kmip;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JniStreaming2Kmip {
private static final String _hostName = "kmiptest01.p6r.com";
@BeforeClass
public static void oneTimeSetUp() {
// NOOP
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// NOOP
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
// NOOP
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
// NOOP
System.out.println("@After - tearDown");
}
@Test
public void JNICall_streaming2KMIP() {
System.out.println("@Test - JNICall-streaming2KMIP");
String uid = null;
byte[] sampleData1 = new byte[20];
byte[] sampleData2 = new byte[20];
byte[] IV = new byte[16];
byte[] correlation = null;
P6KMIPClient kc = new P6KMIPClient();
try {
// [A] Simply show encrypting blocks of data, this could be one buffer we fill via reads to a file or network socket
for (int i = 0; i < sampleData1.length; i++) sampleData1[i] = (byte) (i + 22);
for (int i = 0; i < sampleData2.length; i++) sampleData2[i] = (byte) (i + 19);
for (int j = 0; j < IV.length; j++) IV[j] = (byte) (j + 0x01);
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
uid = kc.createSymmetricKey(KMIPConstants.ALG_AES, 128, (KMIPConstants.USAGE_MASK_ENCRYPT | KMIPConstants.USAGE_MASK_DECRYPT));
System.out.println("UID of AES key for streaming encryption 2: " + uid);
// -> the key must be activated before we can use it on the server to do encryption
kc.activate(uid);
CryptoParams params = new CryptoParams();
params.setBlockCipherMode(KMIPConstants.MODE_CBC);
params.setPaddingMethod(KMIPConstants.PAD_PKCS5);
StreamHandle sh = kc.encryptInit(uid, params, sampleData1, IV);
byte[] cipherText1 = sh.getResultdata();
assertNotEquals(null, cipherText1);
System.out.println("The encryptInit encrypted bytes [" + cipherText1.length + "]");
for (int i = 0; i < cipherText1.length; i++) {
System.out.print(cipherText1[i] + ", ");
}
System.out.print("\n");
correlation = sh.getHandle();
assertNotEquals(null, correlation);
System.out.println("The encryptInit correlation value bytes [" + correlation.length + "]");
for (int i = 0; i < correlation.length; i++) {
System.out.print(correlation[i] + ", ");
}
System.out.print("\n");
byte[] cipherText2 = kc.encryptUpdate(uid, sampleData2, sh.getHandle());
assertNotEquals(null, cipherText2);
System.out.println("\nThe encryptUpdate encrypted bytes [" + cipherText2.length + "]");
for (int i = 0; i < cipherText2.length; i++) {
System.out.print(cipherText2[i] + ", ");
}
System.out.print("\n");
byte[] cipherText3 = kc.encryptFinal(uid, sh.getHandle());
assertNotEquals(null, cipherText3);
System.out.println("\nThe encryptFinal encrypted bytes [" + cipherText3.length + "]");
for (int i = 0; i < cipherText3.length; i++) {
System.out.print(cipherText3[i] + ", ");
}
System.out.print("\n");
// [B] Stream decrypt back to the original
// -> use what was generated above from the encryptXXX() function as data input to be decrypted here
StreamHandle sh2 = kc.decryptInit(uid, params, cipherText1, IV);
byte[] clearText1 = sh2.getResultdata();
assertNotEquals(null, clearText1);
System.out.println("\n\nThe decryptInit original bytes [" + clearText1.length + "]");
for (int i = 0; i < clearText1.length; i++) {
System.out.print(clearText1[i] + ", ");
}
System.out.print("\n");
correlation = sh2.getHandle();
assertNotEquals(null, correlation);
System.out.println("The decryptInit correlation value bytes [" + correlation.length + "]");
for (int i = 0; i < correlation.length; i++) {
System.out.print(correlation[i] + ", ");
}
System.out.print("\n");
byte[] clearText2 = kc.decryptUpdate(uid, cipherText2, sh2.getHandle());
assertNotEquals(null, clearText2);
System.out.println("\nThe decryptUpdate original bytes [" + clearText2.length + "]");
for (int i = 0; i < clearText2.length; i++) {
System.out.print(clearText2[i] + ", ");
}
System.out.print("\n");
byte[] clearText3 = kc.decryptUpdate(uid, cipherText3, sh2.getHandle());
assertNotEquals(null, clearText3);
System.out.println("\nThe decryptUpdate original bytes [" + clearText3.length + "]");
for (int i = 0; i < clearText3.length; i++) {
System.out.print(clearText3[i] + ", ");
}
System.out.print("\n");
// -> NOTE: found a bug in some servers where the decrypt final fails though all the data has been decrypted
byte[] clearText4 = kc.decryptFinal(uid, sh2.getHandle());
assertNotEquals(null, clearText4);
System.out.println("\nThe decryptFinal original bytes [" + clearText4.length + "]");
for (int i = 0; i < clearText4.length; i++) {
System.out.print(clearText4[i] + ", ");
}
System.out.print("\n");
// -> an active key cannot be destroyed
kc.revoke(uid, KMIPConstants.REVOCATION_CESSATION_OF_OPERATION, "Done with test streaming encrypt 2");
kc.destroy(uid);
kc.close();
kc.freeLibrary();
} catch (Exception e) {
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}