您的位置:首页 > 编程语言 > Java开发

eclipse Vim 插件 ViPlugin 序列号生成程序

2013-04-14 19:20 381 查看
好久以来学Java都是直接用vim和javac命令直接操作,最近学习到GUI,想利用Eclipse的GUI插件直接画GUI图形。

但是又想在Eclipse里卖弄用Vim。于是就找到了ViPlugin,但是这个小小的软件又不便宜==!

接着就找到了/article/7051916.html这篇文章,试着写了个很简单的小程序完美解决问题。用Java写了个GUI程序,GUI利用Jigloo画的图形界面。

首先我把CheckLicenseFile.class字节码文件反编译了一下(用的是java decompiler)。得到如下的代码:

//该文件位于eclipse\plugins\com.mbartl.viplugin.eclipse.layer_2.11.0\lib\licensechecker.jar中

package com.mbartl.viimplementation.license;

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())
continue;
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");
}
}

该代码很简单,我们只需要调用其中的public static synchornized Sting encrypt(Sting name, String email);即可得到”序列号“。

首先我们直接写一个库调用一下看看能不能得到结果。

码好如下代码:

/*************************************************************************
> File Name:     License.java
> Author:        Landerl Young
> Mail:          LanderlYoung@gmail.com
> Created Time:  2013/4/14 18:27:22
************************************************************************/
import com.mbartl.viimplementation.license.CheckLicenseFile;

public class License {
public static void main(String[] args){
try{
System.out.println(CheckLicenseFile.encrypt("Hello","123@gmail.com"));
} catch (Exception e){
e.printStackTrace();
}
}
}


然后编译:注意该文件需要的库文件,我的编译命令如下:
D:\Hello>javac -classpath "D:\Program Files\eclipse\plugins\com.mbartl.viplugin.eclipse.layer_2.11.0\lib\licensechecker.jar";"D:\Program Files\eclipse\plugins\com.mbartl.viplugin.eclipse.layer_2.11.0\lib\commons-codec-1.6.jar";.
License.java

在Linux下的编译命令应该把classpath的分隔符改成:(冒号)

然后试着运行

D:\Hello>java -classpath "D:\Program Files\eclipse\plugins\com.mbartl.viplugin.eclipse.layer_2.11.0\lib\licensechecker.jar";"D:\Program Files\eclipse\plugins\com.mbartl.viplugin.eclipse.layer_2.11.0\lib\commons-codec-1.6.jar";.
License

得到以下输出:

EMoe2GqYjjWd8qG2n0fMHdPmK0P5s4oTCNfvJGnTQdM


这就是个License了。

然后我做的事就是写个GUI程序,但实际上要做的事就是把这个过程用图形界面来实现而已。

编译好的jar文件在这里,Eclipse源文件在这里

然后开始用Eclipse写一个有图形界面的工具:

引入所需要的jar库文件

用Eclipse新建一个工程,注意要把需要的库文件import到Eclipse里面,方法可以直接修改工程下的.classpath文件(没有的话可以手动创建),虽然是文本配置文件不过一眼就能看懂,不需要解释。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/commons-codec-1.6.jar"/>
<classpathentry kind="lib" path="lib/licensechecker.jar"/>
<classpathentry kind="lib" path="lib/appFramework-1.0.jar"/>
<classpathentry kind="lib" path="lib/jnlp.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>


或者在左边的package explorer上右键点击你的项目文件选import->java client jar file->浏览你的jar文件所在位置。然后用Jigloo画个图形界面出来(其实图形界面操作更简单,但是用源代码写可能效率更高但是Thinking in java鼓励用图形界面话图形界面,理由是开发起来更快)。画起来很简单我就不赘述了。

效果如下:



画完后手动给Button1添加一个监听器,

jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
jTextField3.setText(CheckLicenseFile.encrypt(jTextField1.getText(), jTextField2.getText()));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
jTextField3.setText("Generate Failed!");
}

}
});


其中jButton1是按钮jTextField3是输出文本框,jTextField1是name的输入文本框,jTextField2是email的输入文本框。

然后就可使<Ctrl-F11>编译试试看了。效果还能接受:

貌似我把John拼成了Jhon==!算了,改着麻烦==!文件都传上来了。

最后就是

导出可执行jar文件

1、右键PackageExplorer,选Export->Runnable JAR file->在Lunch Configuration上选中你的入口class,然后Finish。一切都是这么帅气,一气呵成有木有。

不过还是能利用命令行工具来手动生成的,但是这就要自己编辑MANIFEST.MF文件了。

该部分更详细我单独写了的另一篇文章

然后就能试着双击jar文件运行了(可能你的jar文件打开方式被改过,点这里查看修复操作);或者用命令行 java -jar 来运行。

最后按照这篇博文的做法把得到的”序列号“保存到适当的位置。

好啦,就算是完成了。本人菜鸟……忙乎了好大会搞成的一个能运行的程序写篇文章纪念一下,希望大家多多批评。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: