zoukankan      html  css  js  c++  java
  • license文件生成原理

     
    1. 现在很多J2EE应用都采用一个license文件来授权系统的使用,特别是在系统购买的早期,会提供有限制的license文件对系统进行限制,比如试用版有譬如IP、日期、最大用户数量的限制等。  
    2.   
    3. license控制的方法又有很多,目前比较流行,只要设计的好就很难破解的方法就是采用一对密匙(私匙加密公匙解密)来生成License文件中的Sinature签名内容,再通过Base64或Hex来进行编码。比如原BEA公司现在是Oracle公司的WebLogic就采用的是这种方法来设置License文件。  
    4.   
    5. 这里只进行一个比较简单的实现:  
    6.   
    7. 一共三个类:  
    8.   
    9. A.KeyGenerater类生成公钥私钥对  
    10.   
    11. B.Signaturer类使用私钥进行签名  
    12.   
    13. C.SignProvider类用公钥验证  
    14.   
    15. 公钥和私钥使用Base64加密Base64这个类很多地方都可以查到。  
    16.   
    17.    
    18.   
    19. KeyGenerater类:  
    20.   
    21.    
    22.   
    23. public class KeyGenerater {   
    24.   
    25.  private byte[] priKey;   
    26.   
    27.  private byte[] pubKey;   
    28.   
    29.  public void generater() {   
    30.   try {   
    31.   
    32.   KeyPairGenerator keygen = KeyPairGenerator .getInstance("RSA");   
    33.   
    34.    SecureRandom secrand = new SecureRandom();   
    35.   
    36.    secrand.setSeed("www.川江号子.cn".getBytes()); // 初始化随机产生器   
    37.   
    38.    keygen.initialize(1024, secrand);   
    39.   
    40.    KeyPair keys = keygen.genKeyPair();   
    41.   
    42.    PublicKey pubkey = keys.getPublic();   
    43.   
    44.    PrivateKey prikey = keys.getPrivate()   
    45.   
    46.    pubKey = Base64.encodeToByte(pubkey.getEncoded());   
    47.   
    48.    priKey = Base64.encodeToByte(prikey.getEncoded());   
    49.   
    50.    System.out.println("pubKey = " + new String(pubKey));   
    51.   
    52.    System.out.println("priKey = " + new String(priKey));   
    53.   
    54.   } catch (java.lang.Exception e) {   
    55.   
    56.    System.out.println("生成密钥对失败");   
    57.   
    58.    e.printStackTrace();   
    59.   
    60.   }   
    61.   
    62.  }   
    63.   
    64.  public byte[] getPriKey() {   
    65.   
    66.   return priKey;   
    67.   
    68.  }   
    69.   
    70.  public byte[] getPubKey() {   
    71.   
    72.   return pubKey;   
    73.   
    74.  }   
    75.   
    76. }  
    77.    
    78.   
    79. Signaturer 类:    
    80.   
    81.    
    82.   
    83. public class Signaturer {   
    84.   
    85.  public static byte[] sign(byte[] priKeyText, String plainText) {   
    86.   
    87.   try {   
    88.   
    89.    PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(priKeyText));   
    90.   
    91.    KeyFactory keyf = KeyFactory.getInstance("RSA");   
    92.   
    93.    PrivateKey prikey = keyf.generatePrivate(priPKCS8);   
    94.   
    95.    // 用私钥对信息生成数字签名   
    96.   
    97.     Signature signet = java.security.Signature.getInstance("MD5withRSA");   
    98.   
    99.    signet.initSign(prikey);   
    100.   
    101.    signet.update(plainText.getBytes());   
    102.   
    103.    byte[] signed = Base64.encodeToByte(signet.sign());   
    104.   
    105.    return signed;   
    106.   
    107.   } catch (java.lang.Exception e) {   
    108.   
    109.    System.out.println("签名失败");   
    110.   
    111.    e.printStackTrace();   
    112.   
    113.   }   
    114.   
    115.   return null;   
    116.   
    117.  }   
    118.   
    119. }   
    120.   
    121.   
    122.  SignProvider 类:  
    123.   
    124. public class SignProvider {   
    125.   
    126.  private SignProvider() {   
    127.   
    128.  }   
    129.   
    130.  public static boolean verify(byte[] pubKeyText, String plainText,   
    131.   
    132.    byte[] signText) {   
    133.   
    134.   try {   
    135.   
    136.    // 解密由base64编码的公钥,并构造X509EncodedKeySpec对象   
    137.   
    138.    X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64.decode(pubKeyText));   
    139.   
    140.    // RSA对称加密算法   
    141.   
    142.    KeyFactory keyFactory = KeyFactory.getInstance("RSA");   
    143.   
    144.    // 取公钥匙对象   
    145.   
    146.    PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec);   
    147.   
    148.    // 解密由base64编码的数字签名   
    149.   
    150.    byte[] signed = Base64.decode(signText);   
    151.   
    152.    Signature signatureChecker = Signature.getInstance("MD5withRSA");   
    153.   
    154.    signatureChecker.initVerify(pubKey);   
    155.   
    156.    signatureChecker.update(plainText.getBytes());   
    157.   
    158.    // 验证签名是否正常   
    159.   
    160.    if (signatureChecker.verify(signed))   
    161.   
    162.     return true;   
    163.   
    164.    else   
    165.   
    166.     return false;   
    167.   
    168.   } catch (Throwable e) {   
    169.   
    170.    System.out.println("校验签名失败");   
    171.   
    172.    e.printStackTrace();   
    173.   
    174.    return false;   
    175.   
    176.   }   
    177.   
    178.  }   
    179.   
  • 相关阅读:
    自顶向下的单元测试策略
    孤立的测试策略
    单元测试
    控制流与数据流,信息流
    白盒测试的特点
    白盒测试
    黑盒测试优缺点
    appium对博客园APP进行自动化测试
    招聘测试人员,我在面试什么?
    测试开发这一年
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4267629.html
Copyright © 2011-2022 走看看