/// <summary> /// Generate an RSA key pair according to keySize_. /// </summary> /// /// <param name="privateKeyBlob"></param> /// <param name="publicKeyBlob"></param> private void generateKeyPair(Blob[] privateKeyBlob, Blob[] publicKeyBlob) { RsaKeyParams paras = new RsaKeyParams(keySize_); DecryptKey privateKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); privateKeyBlob[0] = privateKey.getKeyBits(); EncryptKey publicKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(privateKeyBlob[0]); publicKeyBlob[0] = publicKey.getKeyBits(); }
public void testGetGroupKey() { // Create the group manager. GroupManager manager = new GroupManager(new Name("Alice"), new Name( "data_type"), new Sqlite3GroupManagerDb( System.IO.Path.GetFullPath(groupKeyDatabaseFilePath.Name)), 1024, 1, keyChain); setManager(manager); // Get the data list from the group manager. double timePoint1 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T093000"); IList result = manager.getGroupKey(timePoint1); AssertEquals(4, result.Count); // The first data packet contains the group's encryption key (public key). Data data = (Data) result[0]; AssertEquals( "/Alice/READ/data_type/E-KEY/20150825T090000/20150825T100000", data.getName().toUri()); EncryptKey groupEKey = new EncryptKey(data.getContent()); // Get the second data packet and decrypt. data = (Data) result[1]; AssertEquals( "/Alice/READ/data_type/D-KEY/20150825T090000/20150825T100000/FOR/ndn/memberA/ksk-123", data.getName().toUri()); /////////////////////////////////////////////////////// Start decryption. Blob dataContent = data.getContent(); // Get the nonce key. // dataContent is a sequence of the two EncryptedContent. EncryptedContent encryptedNonce = new EncryptedContent(); encryptedNonce.wireDecode(dataContent); AssertEquals(0, encryptedNonce.getInitialVector().size()); AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, encryptedNonce.getAlgorithmType()); EncryptParams decryptParams = new EncryptParams( net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep); Blob blobNonce = encryptedNonce.getPayload(); Blob nonce = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(decryptKeyBlob, blobNonce, decryptParams); // Get the payload. // Use the size of encryptedNonce to find the start of encryptedPayload. ByteBuffer payloadContent = dataContent.buf().duplicate(); payloadContent.position(encryptedNonce.wireEncode().size()); EncryptedContent encryptedPayload = new EncryptedContent(); encryptedPayload.wireDecode(payloadContent); AssertEquals(16, encryptedPayload.getInitialVector().size()); AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc, encryptedPayload.getAlgorithmType()); decryptParams.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc); decryptParams.setInitialVector(encryptedPayload.getInitialVector()); Blob blobPayload = encryptedPayload.getPayload(); Blob largePayload = net.named_data.jndn.encrypt.algo.AesAlgorithm.decrypt(nonce, blobPayload, decryptParams); // Get the group D-KEY. DecryptKey groupDKey = new DecryptKey(largePayload); /////////////////////////////////////////////////////// End decryption. // Check the D-KEY. EncryptKey derivedGroupEKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(groupDKey .getKeyBits()); AssertTrue(groupEKey.getKeyBits().equals(derivedGroupEKey.getKeyBits())); // Check the third data packet. data = (Data) result[2]; AssertEquals( "/Alice/READ/data_type/D-KEY/20150825T090000/20150825T100000/FOR/ndn/memberB/ksk-123", data.getName().toUri()); // Check the fourth data packet. data = (Data) result[3]; AssertEquals( "/Alice/READ/data_type/D-KEY/20150825T090000/20150825T100000/FOR/ndn/memberC/ksk-123", data.getName().toUri()); // Check invalid time stamps for getting the group key. double timePoint2 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150826T083000"); AssertEquals(0, manager.getGroupKey(timePoint2).Count); double timePoint3 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150827T023000"); AssertEquals(0, manager.getGroupKey(timePoint3).Count); }