如何加密嵌入Java的Neo4j?

时间:2016-09-26 11:12:08

标签: java encryption neo4j aes

我正在研究如何加密嵌入Java的Neo4j数据库,因此,我有一个类,即创建2个节点和1个关系,如下所示:

package Neo4j;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class Neo4jJavaAPIDBOperation {
public static void main(String[] args) {

        GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
    GraphDatabaseService db= dbFactory.newEmbeddedDatabase("D:/TPNeo4jDB");
    try (Transaction tx = db.beginTx()) {

    //  NODES   

        // John Node

        Node JohnNode = db.createNode(Person.John);
        JohnNode.setProperty("Name", "John");
        JohnNode.setProperty("Age", "25");
        JohnNode.setProperty("Tel.No.", "123454");

        // Tom Node 

        Node tomNode = db.createNode(Person.Tom);
        tomNode.setProperty("Name", "Tom");
        tomNode.setProperty("Age", "28");
        tomNode.setProperty("Tel.No.", "587496");

        //Relationship

        Relationship relationship = JohnNode.createRelationshipTo
                (tomNode,PersonRelationships.KNOWS);
                relationship.setProperty("Since","2012");

                tx.success();

    }

      System.out.println("Done successfully");

}  }

我有另一个加密算法类,这里是(AES)Alg。第一节课是:

package aes;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class AESencrp {

     private static final String ALGO = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());


        // Encryption:

        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
} }

第二节课是:

package aes;

public class Checker {

    public static void main(String[] args) throws Exception {

        String password = "The plain text ";
        String passwordEnc = AESencrp.encrypt(password);
        String passwordDec = AESencrp.decrypt(passwordEnc);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
    }
}

到目前为止一切正常,只是我从Neo4j类获取属性并手动输入并获得加密值,

所以我的问题是如何自动完成,我的意思是如何自动加密所有Neo4j节点?

非常感谢

0 个答案:

没有答案