license文件生成原理
生活随笔
收集整理的這篇文章主要介紹了
license文件生成原理
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
byte解密weblogic加密oraclehex 現(xiàn)在很多J2EE應(yīng)用都采用一個(gè)license文件來(lái)授權(quán)系統(tǒng)的使用,特別是在系統(tǒng)購(gòu)買的早期,會(huì)提供有限制的license文件對(duì)系統(tǒng)進(jìn)行限制,比如試用版有譬如IP、日期、最大用戶數(shù)量的限制等。?? ?? 而license控制的方法又有很多,目前比較流行,只要設(shè)計(jì)的好就很難破解的方法就是采用一對(duì)密匙(私匙加密公匙解密)來(lái)生成License文件中的Sinature簽名內(nèi)容,再通過(guò)Base64或Hex來(lái)進(jìn)行編碼。比如原BEA公司現(xiàn)在是Oracle公司的WebLogic就采用的是這種方法來(lái)設(shè)置License文件。?? ?? 這里只進(jìn)行一個(gè)比較簡(jiǎn)單的實(shí)現(xiàn):?? ?? 一共三個(gè)類:?? ?? A.KeyGenerater類生成公鑰私鑰對(duì)?? ?? B.Signaturer類使用私鑰進(jìn)行簽名?? ?? C.SignProvider類用公鑰驗(yàn)證?? ?? 公鑰和私鑰使用Base64加密Base64這個(gè)類很多地方都可以查到。?? ?? ??? ?? 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.川江號(hào)子.cn".getBytes());?//?初始化隨機(jī)產(chǎn)生器??? ?? ???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("生成密鑰對(duì)失敗");??? ?? ???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);??? ?? ???//?用私鑰對(duì)信息生成數(shù)字簽名??? ?? ????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編碼的公鑰,并構(gòu)造X509EncodedKeySpec對(duì)象??? ?? ???X509EncodedKeySpec?bobPubKeySpec?=?new?X509EncodedKeySpec(Base64.decode(pubKeyText));??? ?? ???//?RSA對(duì)稱加密算法??? ?? ???KeyFactory?keyFactory?=?KeyFactory.getInstance("RSA");??? ?? ???//?取公鑰匙對(duì)象??? ?? ???PublicKey?pubKey?=?keyFactory.generatePublic(bobPubKeySpec);??? ?? ???//?解密由base64編碼的數(shù)字簽名??? ?? ???byte[]?signed?=?Base64.decode(signText);??? ?? ???Signature?signatureChecker?=?Signature.getInstance("MD5withRSA");??? ?? ???signatureChecker.initVerify(pubKey);??? ?? ???signatureChecker.update(plainText.getBytes());??? ?? ???//?驗(yàn)證簽名是否正常??? ?? ???if?(signatureChecker.verify(signed))??? ?? ????return?true;??? ?? ???else??? ?? ????return?false;??? ?? ??}?catch?(Throwable?e)?{??? ?? ???System.out.println("校驗(yàn)簽名失敗");??? ?? ???e.printStackTrace();??? ?? ???return?false;??? ?? ??}??? ?? ?}??? ?? }?
轉(zhuǎn)載于:https://www.cnblogs.com/lvdongjie/p/4267629.html
總結(jié)
以上是生活随笔為你收集整理的license文件生成原理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android service交互,An
- 下一篇: C# ASP.NET MVC 图片上传的