Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
JniOAEP2Kmip.java
package com.p6r.kmip;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
public class JniOAEP2Kmip {
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_OAEP2KMIP() {
System.out.println("@Test - JNICall-OAEP2KMIP");
P6KMIPClient kc = new P6KMIPClient();
try {
// [A] Perform OAEP encryption with keys created on the KMIP server
kc.initializeLibrary(P6KMIPClient.FLAGS_NONE);
kc.open(_hostName, null);
KeyPairIds pi = kc.createKeyPair(KMIPConstants.ALG_RSA, 1024, KMIPConstants.USAGE_MASK_ENCRYPT, KMIPConstants.USAGE_MASK_DECRYPT);
System.out.println("\nUID of new public key: " + pi.getPublicKeyUID());
System.out.println("UID of new private key: " + pi.getPrivateKeyUID());
kc.activate(pi.getPublicKeyUID());
kc.activate(pi.getPrivateKeyUID());
// [B] Now we use the above key to perform OAEP encryption
byte[] clearText = new byte[28];
clearText[0] = 0x66;
clearText[1] = 0x28;
clearText[2] = 0x19;
clearText[3] = 0x4e;
clearText[4] = 0x12;
clearText[5] = 0x07;
clearText[6] = 0x3d;
clearText[7] = (byte)0xb0;
clearText[8] = 0x3b;
clearText[9] = (byte)0xa9;
clearText[10] = 0x4c;
clearText[11] = (byte)0xda;
clearText[12] = (byte)0x9e;
clearText[13] = (byte)0xf9;
clearText[14] = 0x53;
clearText[15] = 0x23;
clearText[16] = (byte)0x97;
clearText[17] = (byte)0xd5;
clearText[18] = 0x0d;
clearText[19] = (byte)0xba;
clearText[20] = 0x79;
clearText[21] = (byte)0xb9;
clearText[22] = (byte)0x87;
clearText[23] = 0x00;
clearText[24] = 0x4a;
clearText[25] = (byte)0xfe;
clearText[26] = (byte)0xfe;
clearText[27] = 0x34;
// -> public key is used for encryption, private key for decryption
byte[] PSource = new byte[15];
PSource[0] = 0x4b;
PSource[1] = 0x69;
PSource[2] = 0x6c;
PSource[3] = 0x72;
PSource[4] = 0x6f;
PSource[5] = 0x79;
PSource[6] = 0x20;
PSource[7] = 0x77;
PSource[8] = 0x61;
PSource[9] = 0x73;
PSource[10] = 0x20;
PSource[11] = 0x68;
PSource[12] = 0x65;
PSource[13] = 0x72;
PSource[14] = 0x65;
// -> notice that here we specify the cryptographic parameters on the encrypt call rather than associate them with the keys
CryptoParams cp = new CryptoParams();
cp.setPaddingMethod(KMIPConstants.PAD_OAEP);
cp.setHashAlgorithm(KMIPConstants.HASH_SHA384);
cp.setMaskGenerator(KMIPConstants.MASKGENERATOR_MGF1);
cp.setMaskGeneratorHashAlgorithm(KMIPConstants.HASH_SHA256);
cp.setCryptoAlgorithm(KMIPConstants.ALG_RSA);
cp.setPSource(PSource);
// OAEP cannot be streamed
byte[] cipherText = kc.encrypt(pi.getPublicKeyUID(), cp, clearText, null);
System.out.println("\nEncrypted data using RSA OAEP and PSS [" + cipherText.length + "]");
for( int i=0; i < cipherText.length; i++ ) {
System.out.print(cipherText[i] + " ");
}
System.out.println("\n");
byte[] plainText = kc.decrypt(pi.getPrivateKeyUID(), cp, cipherText, null);
System.out.println("Decrypted original data using RSA OAEP and PSS [" + plainText.length + "]");
for( int i=0; i < plainText.length; i++ ) {
System.out.print(plainText[i] + " ");
}
System.out.println("\n");
// [C] An active key cannot be destroyed
kc.revoke(pi.getPublicKeyUID(), KMIPConstants.REVOCATION_CESSATION_OF_OPERATION, "Done with test 2");
kc.revoke(pi.getPrivateKeyUID(), KMIPConstants.REVOCATION_CESSATION_OF_OPERATION, "Done with test 2");
kc.destroy(pi.getPublicKeyUID());
kc.destroy(pi.getPrivateKeyUID());
kc.close();
kc.freeLibrary();
} catch (Exception e) {
System.out.println(e.toString());
assertEquals(0, 1);
}
}
}