From 54167ec2b9ea462fca7d029cb7af45e685053e18 Mon Sep 17 00:00:00 2001 From: lixiangrong Date: Tue, 2 Feb 2016 15:15:26 +0800 Subject: [PATCH] =?UTF-8?q?viPlugin=E6=8F=92=E4=BB=B6=E7=A0=B4=E8=A7=A3?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 64 +++++++-------- .settings/org.eclipse.core.resources.prefs | 5 ++ pom.xml | 5 ++ .../git/eh3/viplugin/CheckLicenseFile.java | 82 +++++++++++++++++++ .../osc/git/eh3/viplugin/CreateLicense.java | 11 +++ 5 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 src/main/java/osc/git/eh3/viplugin/CheckLicenseFile.java create mode 100644 src/main/java/osc/git/eh3/viplugin/CreateLicense.java diff --git a/.classpath b/.classpath index d91c17c..ae235f6 100644 --- a/.classpath +++ b/.classpath @@ -1,32 +1,32 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..365bbd6 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/pom.xml b/pom.xml index fe98261..a17007e 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,11 @@ spring-test ${spring.version} + + commons-codec + commons-codec + 1.6 + useful-code diff --git a/src/main/java/osc/git/eh3/viplugin/CheckLicenseFile.java b/src/main/java/osc/git/eh3/viplugin/CheckLicenseFile.java new file mode 100644 index 0000000..6863ba0 --- /dev/null +++ b/src/main/java/osc/git/eh3/viplugin/CheckLicenseFile.java @@ -0,0 +1,82 @@ +package osc.git.eh3.viplugin; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import org.apache.commons.codec.binary.Base64; + +public class CheckLicenseFile { + private static final String publicKey = "308201b83082012d"; + private static SecretKeySpec key; + private static Cipher cipher; + private static byte[] linebreak = new byte[0]; + private static Base64 coder; + + static { + try { + key = new SecretKeySpec("308201b83082012d".getBytes(), "AES"); + cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + coder = new Base64(32, linebreak, true); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + public static boolean checkLicenseLocations(String[] paths) throws Exception { + String path = ""; + boolean found = false; + for (int i = 0; i < paths.length; i++) { + path = paths[i] + "viPlugin2.lic"; + if (new File(path).exists()) { + found = true; + break; + } + path = paths[i] + "viplugin2.lic"; + if (new File(path).exists()) { + found = true; + break; + } + } + if (!found) { + throw new Exception("License should be in one of the following locations:\n" + paths[0] + "\n" + paths[1]); + } + return checkLicenseFile(path); + } + + private static boolean checkLicenseFile(String fileName) throws Exception { + char[] buffer = new char[(int) new File(fileName).length()]; + try { + FileReader fileReader = new FileReader(fileName); + fileReader.read(buffer); + fileReader.close(); + } catch (FileNotFoundException e) { + throw new Exception("License file not found: " + fileName); + } catch (IOException e) { + throw new Exception("Can't read license file: " + fileName); + } + FileReader fileReader; + String license = new String(buffer); + if (!decrypt(license)) { + throw new Exception("Invalid license found: " + fileName); + } + return true; + } + + public static synchronized String encrypt(String name, String email) throws Exception { + String plainText = name + "viPlugin 2.0" + email; + cipher.init(1, key); + byte[] cipherText = cipher.doFinal(plainText.getBytes()); + return new String(coder.encode(cipherText)); + } + + public static synchronized boolean decrypt(String codedText) throws Exception { + byte[] encypted = coder.decode(codedText.getBytes()); + cipher.init(2, key); + byte[] decrypted = cipher.doFinal(encypted); + String decoded = new String(decrypted); + return decoded.contains("viPlugin 2.0"); + } +} \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/viplugin/CreateLicense.java b/src/main/java/osc/git/eh3/viplugin/CreateLicense.java new file mode 100644 index 0000000..395a56d --- /dev/null +++ b/src/main/java/osc/git/eh3/viplugin/CreateLicense.java @@ -0,0 +1,11 @@ +package osc.git.eh3.viplugin; + +public class CreateLicense { + + @SuppressWarnings("static-access") + public static void main(String[] args) throws Exception { + CheckLicenseFile licenseFile = new CheckLicenseFile(); + String valueString = licenseFile.encrypt("elvin_lee", "elvin_lee@126.com"); + System.out.println("viPlugin2.lic:" + valueString); + } +} \ No newline at end of file