2011-4-8 12:48

是這樣碼

package com.atlassian.extras.decoder.v2;

import com.atlassian.extras.common.LicenseException;

import com.atlassian.extras.common.org.springframework.util.DefaultPropertiesPersister;

import com.atlassian.extras.decoder.api.AbstractLicenseDecoder;

import java.io.*;

import java.security.*;

import java.security.spec.InvalidKeySpecException;

import java.security.spec.X509EncodedKeySpec;

import java.util.Properties;

import java.util.zip.Inflater;

import java.util.zip.InflaterInputStream;

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

public class Version2LicenseDecoder extends AbstractLicenseDecoder

{

public Version2LicenseDecoder()

{

}

public boolean canDecode(String licenseString)

{

licenseString = removeWhiteSpaces(licenseString);

int pos = licenseString.lastIndexOf('X');

if(pos == -1 || pos + 3 >= licenseString.length())

return false;

String lengthStr;

int encodedLicenseLength;

try

{

int version = Integer.parseInt(licenseString.substring(pos + 1, pos + 3));

if(version != 1 && version != 2)

return false;

}

catch(NumberFormatException e)

{

return false;

}

lengthStr = licenseString.substring(pos + 3);

encodedLicenseLength = Integer.valueOf(lengthStr, 31).intValue();

if(pos != encodedLicenseLength)

return false;

return true;

}

public Properties doDecode(String licenseString)

{

String encodedLicenseTextAndHash = getLicenseContent(removeWhiteSpaces(licenseString));

byte zippedLicenseBytes[] = checkAndGetLicenseText(encodedLicenseTextAndHash);

Reader licenseText = unzipText(zippedLicenseBytes);

return loadLicenseConfiguration(licenseText);

}

protected int getLicenseVersion()

{

return 2;

}

private Reader unzipText(byte licenseText[])

{

ByteArrayInputStream in = new ByteArrayInputStream(licenseText);

in.skip(LICENSE_PREFIX.length);

InflaterInputStream zipIn = new InflaterInputStream(in, new Inflater());

try

{

return new InputStreamReader(zipIn, "UTF-8");

}

catch(UnsupportedEncodingException e)

{

throw new LicenseException(e);

}

}

private String getLicenseContent(String licenseString)

{

String lengthStr = licenseString.substring(licenseString.lastIndexOf('X') + 3);

try

{

int encodedLicenseLength = Integer.valueOf(lengthStr, 31).intValue();

return licenseString.substring(0, encodedLicenseLength);

}

catch(NumberFormatException e)

{

throw new LicenseException((new StringBuilder()).append("Could NOT decode license length ").toString(), e);

}

}

private byte[] checkAndGetLicenseText(String licenseContent)

{

byte licenseText[];

try

{

byte decodedBytes[] = Base64.decodeBase64(licenseContent.getBytes());

ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);

DataInputStream dIn = new DataInputStream(in);

int textLength = dIn.readInt();

licenseText = new byte[textLength];

dIn.read(licenseText);

byte hash[] = new byte[dIn.available()];

dIn.read(hash);

try

{

Signature signature = Signature.getInstance("SHA1withDSA");

signature.initVerify(PUBLIC_KEY);

signature.update(licenseText);

if(!signature.verify(hash))

throw new LicenseException("Failed to verify the license.");

}

catch(InvalidKeyException e)

{

throw new LicenseException(e);

}

catch(SignatureException e)

{

throw new LicenseException(e);

}

catch(NoSuchAlgorithmException e)

{

throw new LicenseException(e);

}

}

catch(IOException e)

{

throw new LicenseException(e);

}

return licenseText;

}

private Properties loadLicenseConfiguration(Reader text)

{

try

{

Properties props = new Properties();

(new DefaultPropertiesPersister()).load(props, text);

return props;

}

catch(IOException e)

{

throw new LicenseException("Could NOT load properties from reader", e);

}

}

private static String removeWhiteSpaces(String licenseData)

{

if(licenseData == null || licenseData.length() == 0)

return licenseData;

char chars[] = licenseData.toCharArray();

StringBuffer buf = new StringBuffer(chars.length);

for(int i = 0; i < chars.length; i++)

if(!Character.isWhitespace(chars[i]))

buf.append(chars[i]);

return buf.toString();

}

public static String packLicense(byte text[], byte hash[])

throws LicenseException

{

try

{

ByteArrayOutputStream out = new ByteArrayOutputStream();

DataOutputStream dOut = new DataOutputStream(out);

dOut.writeInt(text.length);

dOut.write(text);

dOut.write(hash);

byte allData[] = out.toByteArray();

String result = (new String(Base64.encodeBase64(allData))).trim();

result = (new StringBuilder()).append(result).append('X').append("0").append(2).append(Integer.toString(result.length(), 31)).toString();

result = split(result);

return result;

}

catch(IOException e)

{

throw new LicenseException(e);

}

}

private static String split(String licenseData)

{

if(licenseData == null || licenseData.length() == 0)

return licenseData;

char chars[] = licenseData.toCharArray();

StringBuffer buf = new StringBuffer(chars.length + chars.length / 76);

for(int i = 0; i < chars.length; i++)

{

buf.append(chars[i]);

if(i > 0 && i % 76 == 0)

buf.append('\n');

}

return buf.toString();

}

public static final int VERSION_NUMBER_1 = 1;

public static final int VERSION_NUMBER_2 = 2;

public static final int VERSION_LENGTH = 3;

public static final int ENCODED_LICENSE_LENGTH_BASE = 31;

public static final byte LICENSE_PREFIX[] = {

13, 14, 12, 10, 15

};

public static final char SEPARATOR = 88;

private static final PublicKey PUBLIC_KEY;

private static final int ENCODED_LICENSE_LINE_LENGTH = 76;

static

{

try

{

String pubKeyEncoded = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAIvfweZvmGo5otwawI3no7Udanxal3hX2haw962KL/nHQrnC4FG2PvUFf34OecSK1KtHDPQoSQ+DHrfdf6vKUJphw0Kn3gXm4LS8VK/LrY7on/wh2iUobS2XlhuIqEc5mLAUu9Hd+1qxsQkQ50d0lzKrnDqPsM0WA9htkdJJw2nS";

KeyFactory keyFactory = KeyFactory.getInstance("DSA");

PUBLIC_KEY = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decodeBase64("MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYUAAoGBAIvfweZvmGo5otwawI3no7Udanxal3hX2haw962KL/nHQrnC4FG2PvUFf34OecSK1KtHDPQoSQ+DHrfdf6vKUJphw0Kn3gXm4LS8VK/LrY7on/wh2iUobS2XlhuIqEc5mLAUu9Hd+1qxsQkQ50d0lzKrnDqPsM0WA9htkdJJw2nS".getBytes())));

}

catch(NoSuchAlgorithmException e)

{

throw new Error(e);

}

catch(InvalidKeySpecException e)

{

throw new Error(e);

}

}

}

java策略文件_[讨论]java类文件保护策略相关推荐

  1. java .lck文件_关于Java日志log.lck文件的出现原因和关闭方法

    出现的问题 实验中要求用log记录所有出现的异常情况和主程序的各个操作,但我们主程序里的各个操作是调用的是各个类的方法,如果全部在各个类里只抛出异常在主程序里处理,那主程序就会很冗余,而且也不符合AD ...

  2. java printwriter 文件_关于java:如何将PrintWriter转换为String或写入文件?

    我正在使用JSP生成动态页面,我想将此动态生成的完整页面保存为文件存档. 在JSP中,所有内容都写入PrintWriter out = response.getWriter(); 在页面的末尾,在向客 ...

  3. linux java excel文件_使用Java语言将excel中读取到的内容导入Linux的文件中

    一.maven配置 导入excel表格需要使用的依赖: org.apache.poi poi 4.0.0 org.apache.poi poi-ooxml 4.0.0 二.测试程序 package u ...

  4. java hdfs文件_使用Java访问HDFS中的文件

    我试图使用Java API访问HDFS中的文件,但每次我都找不到文件.我用来访问的代码是: – Configuration conf = new Configuration(); conf.addRe ...

  5. java文件指针,Java 测试文件指针,java测试指针,两种方法打开文件并进行内

    Java 测试文件指针,java测试指针,两种方法打开文件并进行内 两种方法打开文件并进行内容定位package com.ronsoft.books.nio.channels;import java. ...

  6. java虚拟机标志_《Java虚拟机原理图解》1.3、class文件中的访问标志、类索引、父类索引、接口索引集合...

    讲完了class文件中的常量池,我们就相当于克服了class文件中最麻烦的模块了.现在,我们来看一下class文件中紧接着常量池后面的几个东西:访问标志.类索引.父类索引.接口索引集合. 1. 访问标 ...

  7. 没有类的java文件_没有公共类的.java文件的Java编译

    好的,所以java源文件必须至少有一个公共类,该文件应该被称为"class-name.java". 很公平. 因此,如果我有一个类,那么以下将编译: public class He ...

  8. java使用缓冲区读取文件_在Java中使用Google的协议缓冲区

    java使用缓冲区读取文件 最近发布了 有效的Java第三版 ,我一直对确定此类Java开发书籍的更新感兴趣,该书籍的最新版本仅通过Java 6进行了介绍 . 在此版本中,显然存在与Java 7 , ...

  9. java写一段程序代表心情_讨论java初步学习的方法及心情

    Java的重要性 Java语言的三大特点,面向对象.良好的跨平台性和健壮性,这三大特点使Java被广大编程人员接收并且使用.Java的核心机制有Java虚拟机和垃圾回收机制这两种,Java虚拟机通过解 ...

最新文章

  1. [Struts]Cannot find bean in any scope之一解
  2. 转:VC9(VC2008.net) 编译安装 boost 1.39 库
  3. Java 数组及多维数组
  4. 灰度测试试验流量“洗牌”
  5. Springboot2 Swagger3 集成
  6. 9203 0427 随堂小结
  7. 华为p50预计售价鸿蒙是什么,华为P50pro曝光,鸿蒙0S+5500毫安,售价却让人买不起...
  8. SAP 软件价格体系及SAP项目实施费用构成介绍
  9. 基本遗传算法(GA)详解
  10. 合宙 Air 724UG模组(4G Cat.1通信模组)测试过程
  11. 作业 5:词频统计——增强功能
  12. mysql next key_关于mysql next-key锁的一些个人理解
  13. Android开发——RelativeLayout.LayoutParams的使用
  14. 计算机专业:考研 VS 工作
  15. 本人初学时java基础笔记
  16. C# 文件以txt文档模式存储,存储地址的设置 saveFileDialog控件的使用
  17. 单片机多功能电子琴课设_基于51单片机设计的简易电子琴
  18. 硬件大熊原创合集(2023/02更新)
  19. U5 CentOS系统的U盘启动与安装-孙宇彤-专题视频课程
  20. Linux安装Maven、POM及配置文件详解

热门文章

  1. Zabbix触发器_action动作及模板应用(二)
  2. 【翻译】针对多种设备定制Ext JS 5应用程序
  3. 读写XML文档时,去掉新增加节点的“空命名空间”(xmlns=””)
  4. 页面图片延时加载(附实例下载)
  5. sencha touch中list如何撑满整个view
  6. VS2010 测试功能之旅:编码的UI测试(4)-通过编写测试代码的方式“.NET研究”建立UI测试(下)...
  7. OJ1159: 最大的两个数(指针专题)(C语言)
  8. php在那个位置加载语言包,thinkphp 3.23语言包加载
  9. 信息学奥赛一本通 1982:【19CSPJ普及组】数字游戏
  10. 信息学奥赛一本通(1220:单词接龙)