Total Pageviews

2018/10/11

[Java] [AES] How to generate key for AES encryption and decryption?

Problem
I would like to utilize AES to do text encryption and decryption, how to generate key?


How-To
Here has sample code to generate key:
package test.albert.security;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AESKeyGenerator {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        AESKeyGenerator keyGen = new AESKeyGenerator();
        log.info(keyGen.generateKey());
    }

    public String generateKey() throws NoSuchAlgorithmException {
        String key = "";
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey secretKey = keyGen.generateKey();
            key = toHex(secretKey.getEncoded());
        } catch (NoSuchAlgorithmException e) {
            throw e;
        }
        return key;
    }

    private String toHex(final byte[] data) {
        final StringBuilder sb = new StringBuilder(data.length * 2);
        for (final byte b : data) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }
}

Here has sample code to do encrypt and decrypt:
package test.albert.security;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AESUtils {

    private final static String SECURED_KEY = "A8167A5A6CF9783099B9FF34588C52B9";

    public static String encrypt(String inputText) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKey = new SecretKeySpec(SECURED_KEY.getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        String encryptedString = Base64.encodeBase64String(cipher.doFinal(inputText.getBytes()));
        return encryptedString;
    }

    public static String decrypt(String encryptedText) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        SecretKeySpec secretKey = new SecretKeySpec(SECURED_KEY.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedText.getBytes())));
        return decryptedString;
    }

    public static void main(String[] args) throws Exception {
        String plainText = "test123abc";
        String encryptedText = AESUtils.encrypt(plainText);
        String decryptedText = AESUtils.decrypt(encryptedText);
        log.info(encryptedText);
        log.info(decryptedText);
    }
}


Remember to add commons-codec to your dependency in pom.xml
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>



2018/10/10

[Spring] [Neo4j] How to create customized query in repository

在 graph database 中有以下幾筆 Movie 的資料,假設我要找出片名有 Fa 此字眼的 Movie


Movie node entity 內容如下:
package neo4j.springdata.vo;

import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Index;
import org.neo4j.ogm.annotation.NodeEntity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@NodeEntity
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
public class Movie {

    @Id
    @GeneratedValue
    private Long id;

    @Index(unique = true)
    @NonNull
    private String name;

}


Repository interface / class 內容如下:
package neo4j.springdata.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import neo4j.springdata.repository.custom.MovieRepositoryCustom;
import neo4j.springdata.vo.Movie;

@Repository
public interface MovieRepository extends CrudRepository<Movie, Long>, MovieRepositoryCustom {
    
}


package neo4j.springdata.repository.custom;

import java.util.List;

import neo4j.springdata.vo.Movie;

public interface MovieRepositoryCustom {
    
    List<Movie> findSimilarName(String name);
    
}


package neo4j.springdata.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableList;

import neo4j.springdata.repository.custom.MovieRepositoryCustom;
import neo4j.springdata.vo.Movie;

@Component
public class MovieRepositoryImpl implements MovieRepositoryCustom {

    @Autowired
    private Session session;

    @Override
    public List<Movie> findSimilarName(String name) {
        String cypher = "match (m:Movie) where m.name =~ {name} return m";

        Map<String, Object> params = new HashMap<>();
        params.put("name", ".*" + name + ".*");
        
        Iterable<Movie> result = session.query(Movie.class, cypher, params);
        
        return ImmutableList.copyOf(result);
    }

}

Service class 內容如下:
package neo4j.springdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import neo4j.springdata.repository.MovieRepository;
import neo4j.springdata.vo.Movie;

@Service
public class MovieService {

    @Autowired
    private MovieRepository movieRepo;

    @Transactional
    public void createMovies(List<Movie> movies) {
        movieRepo.saveAll(movies);
    }

    public List<Movie> findSimilarName(String name) {
        return movieRepo.findSimilarName(name);
    }

}


Client class 內容如下:
package neo4j.springdata;

import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

import lombok.extern.slf4j.Slf4j;
import neo4j.springdata.service.MovieService;
import neo4j.springdata.vo.Movie;

@SpringBootApplication
@EnableNeo4jRepositories("neo4j.springdata.repository")
@EntityScan(basePackages = "neo4j.springdata.vo")
@Slf4j
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    CommandLineRunner createCustomQuery(MovieService movieService) {
        return args -> {
            List<Movie> movies = movieService.findSimilarName("Fa");
            log.info("movies = " + movies.toString());
        };

    }

}


pom.xml 內容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>neo4j</groupId>
    <artifactId>springdata</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springdata</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-libs-release</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>



2018/10/09

[Neo4j] Cypher example

以下有個朋友與電影的關係圖:


若想找出 John Johnson 的朋友:
// 找出 John Johnson 的朋友
match(u:User) where u.name="John Johnson"
match (u)-[r:IS_FRIEND_OF]-(n:User)
return n as friends

Neo4j browser 執行結果:



若想找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影:
// 找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影
match(u1:User)-[:HAS_SEEN]->(m1:Movie)<-[:HAS_SEEN]-(u2:User)-[:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2))
return u1 as user, collect(m2.name) as movies

Neo4j browser 執行結果:



若想找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影:
// 找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影
match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2))
return m2 as movie, r2.stars as stars

Neo4j browser 執行結果:




若想找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星:
// 找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星
match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie)
where u1.name="John Johnson" and not((u1)-[:HAS_SEEN]->(m2)) and r2.stars > 3
return m2 as movie, r2.stars as stars


Neo4j browser 執行結果:

於 Spring Data 中, repository class 定義如下:
package neo4j.springdata.repository;

import java.util.List;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import neo4j.springdata.vo.User;
import neo4j.springdata.vo.result.UserQueryResult;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

    @Query(" match(u:User) where u.name={name} " + " match (u)-[r:IS_FRIEND_OF]-(n:User) " + " return n as friends ")
    List<UserQueryResult> findFriends(@Param("name") String name);

    @Query(" match(u1:User)-[:HAS_SEEN]->(m1:Movie)<-[:HAS_SEEN]-(u2:User)-[:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) "
            + " return u1 as user, collect(m2.name) as movies ")
    List<UserQueryResult> findRecommendedMovies(@Param("name") String name);

    @Query(" match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) " + " return m2 as movie, r2.stars as stars")
    List<UserQueryResult> findUnwatchedMovies(@Param("name") String name);

    @Query(" match (m1:Movie)<-[r1:HAS_SEEN]-(u1:User)-[:IS_FRIEND_OF*..2]-(u2:User)-[r2:HAS_SEEN]->(m2:Movie) "
            + " where u1.name={name} and not((u1)-[:HAS_SEEN]->(m2)) and r2.stars > 3 "
            + " return m2 as movie, r2.stars as stars ")
    List<UserQueryResult> findUnwatchedGoodMovies(@Param("name") String name);

}


Query result bean 定義如下:
package neo4j.springdata.vo.result;

import java.util.List;

import org.springframework.data.neo4j.annotation.QueryResult;

import lombok.Data;
import lombok.ToString;
import neo4j.springdata.vo.Movie;
import neo4j.springdata.vo.User;

@QueryResult
@Data
@ToString
public class UserQueryResult {

    private User friends;
    
    private User user;
    private List<String> movies;
    
    private Movie movie;
    private Integer stars;
    
}


Service class 定義如下:
package neo4j.springdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import neo4j.springdata.repository.UserRepository;
import neo4j.springdata.vo.result.UserQueryResult;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepo;
  
    public List<UserQueryResult> findFriends(String name) {
        return userRepo.findFriends(name);
    }

    public List<UserQueryResult> findRecommendedMovies(String name) {
        return userRepo.findRecommendedMovies(name);
    }

    public List<UserQueryResult> findUnwatchedMovies(String name) {
        return userRepo.findUnwatchedMovies(name);
    }

    public List<UserQueryResult> findUnwatchedGoodMovies(String name) {
        return userRepo.findUnwatchedGoodMovies(name);
    }
}


Client code:
package neo4j.springdata;

import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

import com.google.common.base.Joiner;

import lombok.extern.slf4j.Slf4j;
import neo4j.springdata.service.UserService;
import neo4j.springdata.vo.Movie;
import neo4j.springdata.vo.result.UserQueryResult;

@SpringBootApplication
@EnableNeo4jRepositories
@Slf4j
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    @Bean
    CommandLineRunner queryTest(UserService service) {
        return args -> {
            String name = "John Johnson";
            
            log.info("找出 John Johnson 的朋友:");
            List<UserQueryResult> friends = service.findFriends(name);
            friends.forEach(f->log.info(f.toString()));
            
            log.info("找出 John Johnson 看過的電影,且找出看過此部電影的人,也看過哪部電影");
            List<UserQueryResult> recommendedMovies = service.findRecommendedMovies(name);
            recommendedMovies.forEach(r -> {
                String uName = r.getUser().getName();
                List<String> movies = r.getMovies();
                log.info("user name = " + uName + ", watched " + Joiner.on(",").join(movies));
            });
            
            log.info("找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影");
            List<UserQueryResult> unwatchedMovies = service.findUnwatchedMovies(name);
            unwatchedMovies.forEach(u -> {
                Movie movie = u.getMovie();
                Integer stars = u.getStars();
                log.info("movie name = " + movie.getName() + ", stars = " + stars);
            });
            
            log.info("找出 John Johnson 的朋友看過,但是 John Johnson 沒看過的電影,且朋友對此部電影的評價要超過 3 顆星");
            List<UserQueryResult> unwatchedGoodMovies = service.findUnwatchedGoodMovies(name);
            unwatchedGoodMovies.forEach(u -> {
                Movie movie = u.getMovie();
                Integer stars = u.getStars();
                log.info("Good movie name = " + movie.getName() + ", stars = " + stars);
            });
        };
    }

}