[首页]
[文章]
[教程]
首页
Web开发
Windows开发
编程语言
数据库技术
移动平台
系统服务
微信
设计
布布扣
其他
数据分析
首页
>
其他
> 详细
license文件生成原理
时间:
2015-02-02 15:30:22
阅读:
360
评论:
0
收藏:
0
[点我收藏+]
byte
解密
weblogic
加密
oracle
hex
现在很多J2EE应用都采用一个
license文件来授权系统的使用,特别是在系统购买的早期,会提供有限制的
license文件对系统进行限制,比如试用版有譬如IP、日期、最大用户数量的限制等。
而
license控制的方法又有很多,目前比较流行,只要设计的好就很难破解的方法就是采用一对密匙(私匙加密公匙解密)来生成
License文件中的Sinature签名内容,再通过Base64或Hex来进行编码。比如原BEA公司现在是Oracle公司的WebLogic就采用的是这种方法来设置
License文件。
这里只进行一个比较简单的实现:
一共三个类:
A.KeyGenerater类生成公钥私钥对
B.Signaturer类使用私钥进行签名
C.SignProvider类用公钥验证
公钥和私钥使用Base64加密Base64这个类很多地方都可以查到。
KeyGenerater类:
public
class KeyGenerater {
private
byte[] priKey;
private
byte[] pubKey;
public
void generater() {
try {
KeyPairGenerator keygen = KeyPairGenerator .getInstance(
"RSA");
SecureRandom secrand =
new SecureRandom();
secrand.setSeed(
"www.川江号子.cn".getBytes());
// 初始化随机产生器
keygen.initialize(
1024, secrand);
KeyPair keys = keygen.genKeyPair();
PublicKey pubkey = keys.getPublic();
PrivateKey prikey = keys.getPrivate()
pubKey = Base64.encodeToByte(pubkey.getEncoded());
priKey = Base64.encodeToByte(prikey.getEncoded());
System.out.println(
"pubKey = " +
new String(pubKey));
System.out.println(
"priKey = " +
new String(priKey));
}
catch (java.lang.Exception e) {
System.out.println(
"生成密钥对失败");
e.printStackTrace();
}
}
public
byte[] getPriKey() {
return priKey;
}
public
byte[] getPubKey() {
return pubKey;
}
}
Signaturer 类:
public
class Signaturer {
public
static
byte[] sign(
byte[] priKeyText, String plainText) {
try {
PKCS8EncodedKeySpec priPKCS8 =
new PKCS8EncodedKeySpec(Base64.decode(priKeyText));
KeyFactory keyf = KeyFactory.getInstance(
"RSA");
PrivateKey prikey = keyf.generatePrivate(priPKCS8);
// 用私钥对信息生成数字签名
Signature signet = java.security.Signature.getInstance(
"MD5withRSA");
signet.initSign(prikey);
signet.update(plainText.getBytes());
byte[] signed = Base64.encodeToByte(signet.sign());
return signed;
}
catch (java.lang.Exception e) {
System.out.println(
"签名失败");
e.printStackTrace();
}
return
null;
}
}
SignProvider 类:
public
class SignProvider {
private SignProvider() {
}
public
static
boolean verify(
byte[] pubKeyText, String plainText,
byte[] signText) {
try {
// 解密由base64编码的公钥,并构造X509EncodedKeySpec对象
X509EncodedKeySpec bobPubKeySpec =
new X509EncodedKeySpec(Base64.decode(pubKeyText));
// RSA对称加密算法
KeyFactory keyFactory = KeyFactory.getInstance(
"RSA");
// 取公钥匙对象
PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);
// 解密由base64编码的数字签名
byte[] signed = Base64.decode(signText);
Signature signatureChecker = Signature.getInstance(
"MD5withRSA");
signatureChecker.initVerify(pubKey);
signatureChecker.update(plainText.getBytes());
// 验证签名是否正常
if (signatureChecker.verify(signed))
return
true;
else
return
false;
}
catch (Throwable e) {
System.out.println(
"校验签名失败");
e.printStackTrace();
return
false;
}
}
}
license文件生成原理
原文:http://www.cnblogs.com/lvdongjie/p/4267629.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年09月23日 (328)
2021年09月24日 (313)
2021年09月17日 (191)
2021年09月15日 (369)
2021年09月16日 (411)
2021年09月13日 (439)
2021年09月11日 (398)
2021年09月12日 (393)
2021年09月10日 (160)
2021年09月08日 (222)
最新文章
更多>
2021/09/28 scripts
2022-05-27
vue自定义全局指令v-emoji限制input输入表情和特殊字符
2022-05-27
9.26学习总结
2022-05-27
vim操作
2022-05-27
深入理解计算机基础 第三章
2022-05-27
C++ string 作为形参与引用传递(转)
2022-05-27
python 加解密
2022-05-27
JavaScript-对象数组里根据id获取name,对象可能有children属性
2022-05-27
SQL语句——保持现有内容在后面增加内容
2022-05-27
virsh命令文档
2022-05-27
教程昨日排行
更多>
1.
list.reverse()
2.
Django Admin 管理工具
3.
AppML 案例模型
4.
HTML 标签列表(功能排序)
5.
HTML 颜色名
6.
HTML 语言代码
7.
jQuery 事件
8.
jEasyUI 创建分割按钮
9.
jEasyUI 创建复杂布局
10.
jEasyUI 创建简单窗口
友情链接
汇智网
PHP教程
插件网
关于我们
-
联系我们
-
留言反馈
- 联系我们:wmxa8@hotmail.com
© 2014
bubuko.com
版权所有
打开技术之扣,分享程序人生!