RSA 加密解密 可用於製做liscence

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateCrtKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import net.sf.json.JSONObject; import sun.security.rsa.RSAPrivateCrtKeyImpl; public class RSA {          public static void main(String[] args) {          //        System.out.println(System.currentTimeMillis()); //        String[] strs = Skey_RSA(1024); //        String e = strs[0]; //        String n = strs[1]; //        String d = strs[2]; //        System.out.println(e); //        System.out.println(n); //        System.out.println(d); //        String str = Enc_RSA("{name:'軟件名稱',deadline:'到期日期毫秒數'}", e, n); //        System.out.println(str+"---"); //        String str2 = Dec_RSA(str, d, n); //        System.out.println(str2); //        PrivateKey pk = new  PrivateKey(); //        pk.setModulus(new BigInteger(n)); //        pk.setPrivateExponent(new BigInteger(d)); //         //        String privateKeyFile = System.getProperty("user.dir") //        + "/WebRoot/WEB-INF/classes/private.key"; //        try { //            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(privateKeyFile)); //            oos.writeObject(pk); //            oos.flush(); //            oos.close(); //        } catch (Exception e1) { //            // TODO Auto-generated catch block //            e1.printStackTrace(); //        }         checkLicence();     }     public static void checkLicence() {         String licFile = System.getProperty("user.dir")                 + "/WebRoot/WEB-INF/classes/mobile.lic";         String privateKeyFile = System.getProperty("user.dir")                 + "/WebRoot/WEB-INF/classes/private.key";         String licence = decrypt(licFile, privateKeyFile);         if (licence != null) {             JSONObject json = JSONObject.fromObject(licence);             String name = json.getString("name");             long deadline = Long.parseLong(json.getString("deadline"));             String companyName = AppUtil.getCompanyName();             if ((!companyName.equals(name))                     || (deadline < System.currentTimeMillis())) {                 throw new RuntimeException("系統未受權或已過時,請聯繫做者進行註冊,謝謝!");             }         } else {             throw new RuntimeException("系統未受權或已過時,請聯繫做者進行註冊,謝謝!");         }     } //獲取json明文 示例 { name:'',deadline:'1371362586906'}     public static String decrypt(String licFile, String privateKeyFile) {         try {             BufferedReader in = new BufferedReader(new InputStreamReader(                     new FileInputStream(licFile), "utf-8"));             String ctext = in.readLine();             BigInteger c = new BigInteger(ctext);             FileInputStream f = new FileInputStream(privateKeyFile);             ObjectInputStream b = new ObjectInputStream(f);             RSAPrivateKey privateKey = (RSAPrivateKey) b.readObject();             BigInteger d = privateKey.getPrivateExponent();             BigInteger n = privateKey.getModulus();             BigInteger m = c.modPow(d, n);             byte[] mt = m.toByteArray();             return new String(mt, "utf-8");         } catch (Exception e) {             e.printStackTrace();         }         return null;     }     public static String[] Skey_RSA(int keylen) {// 輸入密鑰長度         String[] output = new String[5]; // 用來存儲密鑰的e n d p q         try {             KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");             kpg.initialize(keylen); // 指定密鑰的長度,初始化密鑰對生成器             KeyPair kp = kpg.generateKeyPair(); // 生成密鑰對             RSAPublicKey puk = (RSAPublicKey) kp.getPublic();             RSAPrivateCrtKey prk = (RSAPrivateCrtKeyImpl) kp.getPrivate();             BigInteger e = puk.getPublicExponent();             BigInteger n = puk.getModulus();             BigInteger d = prk.getPrivateExponent();             BigInteger p = prk.getPrimeP();             BigInteger q = prk.getPrimeQ();             output[0] = e.toString();             output[1] = n.toString();             output[2] = d.toString();             output[3] = p.toString();             output[4] = q.toString();         } catch (Exception ex) {         }         return output;     }     // 加密 在RSA公鑰中包含有兩個整數信息:e和n。對於明文數字m,計算密文的公式是m的e次方再與n求模。     public static String Enc_RSA(String mingwen, String eStr, String nStr) {         String miwen = new String();         try {             BigInteger e = new BigInteger(eStr);             BigInteger n = new BigInteger(nStr);             byte[] ptext = mingwen.getBytes("utf-8"); // 獲取明文的大整數             BigInteger m = new BigInteger(ptext);             BigInteger c = m.modPow(e, n);             miwen = c.toString();         } catch (Exception ex) {         }         return miwen;     }     // 解密     public static String Dec_RSA(String miwen, String dStr, String nStr) {         String mingwen = null;         try {             BigInteger d = new BigInteger(dStr);// 獲取私鑰的參數d,n             BigInteger n = new BigInteger(nStr);             BigInteger c = new BigInteger(miwen);             BigInteger m = c.modPow(d, n);// 解密明文             byte[] mt = m.toByteArray();// 計算明文對應的字符串並輸出             mingwen= new String(mt, "utf-8");         } catch (Exception e) {                          e.printStackTrace();         }         return mingwen;     } }
相關文章
相關標籤/搜索