CAP协议字符串签名
来源:互联网 发布:mac查询端口占用情况 编辑:程序博客网 时间:2024/06/05 08:26
/** * <p>标题: Signer.</p> * * <p>描述: 签名和验证CAP</p> * * <p>版权: Copyright (c) 2009</p> * * @author joy.jie * @version 1.0.0 */public class DefaultCAPSigner implements ICAPSigner { private Signature sfs; private Signature sfv; /** * * 构造函数. * * @param pwd String 证书库密码 * @param alias String 证书库别名 * @param priKeyFile 私钥文件名 * @param pubKeyFile String 公钥保存文件名 */ public DefaultCAPSigner(String pwd, String alias, String priKeyFile, String pubKeyFile) { if(pwd != null && !pwd.equals("")){ this.sfs = CtSignature.createSignatureForSign(pwd, alias, priKeyFile); } this.sfv = CtSignature.createSignatureForVerify(pubKeyFile); } /** * 签名. * * @param cap String 待签名CAP协议字符串 * @return 签名后的字符串 * @throws Exception */ public synchronized String signatureCAP(String cap) throws SignException { try { ///// 此处请验证多线程环境下是否需要加上同步处理(joy.jie) ///// this.sfs.update(cap.getBytes()); byte[] signedText = this.sfs.sign(); return StringUtils.replace(cap,"<DigitalSign/>", "<DigitalSign>" + HexUtils.toHexString(signedText) + "</DigitalSign>"); } catch (Exception ex) { ex.printStackTrace(); throw new SignException(SignException.TYPE_SIG,ex.getMessage(),ex); } } /** * 普通字符串签名 * @param simpleStr * @return * @throws SignException */ public synchronized String signatureSimple(String simpleStr) throws SignException { try { this.sfs.update(simpleStr.getBytes()); byte[] signedText = this.sfs.sign(); return HexUtils.toHexString(signedText); } catch (Exception ex) { throw new SignException(SignException.TYPE_SIG,ex.getMessage(),ex); } } /** * 验证. * * @param cap String CAP协议字符串 * @return boolean 验证是否通过 */ public synchronized boolean verifyCAP(String cap) throws SignException { try { String originalText = StringUtils.replace(cap,StringUtils.substring(cap,StringUtils .indexOf(cap,"<DigitalSign>"), StringUtils.indexOf(cap,"</DigitalSign>") + "</DigitalSign>".length()), "<DigitalSign/>"); String signedText = StringUtils.substring(cap,StringUtils.indexOf(cap,"<DigitalSign>") + "<DigitalSign>".length(), StringUtils.indexOf(cap,"</DigitalSign>")); this.sfv.update(originalText.getBytes()); return this.sfv.verify(HexUtils.fromHexString(signedText)); } catch (Exception ex) { throw new SignException(SignException.TYPE_VRF,ex.getMessage(),ex); } } /** * 普通字符串验签 * * @param originalText * @param signedText * @return * @throws SignException */ public synchronized boolean verifySimple(String originalText, String signedText) throws SignException { try{ this.sfv.update(originalText.getBytes()); return this.sfv.verify(HexUtils.fromHexString(signedText)); }catch(Exception e){ throw new SignException(SignException.TYPE_VRF,e.getMessage(),e); } } /** * 获得sfs. * * @return sfs */ public Signature getSfs() { return sfs; } /** * 设置sfs. * * @param sfs 要设置的 sfs */ public void setSfs(Signature sfs) { this.sfs = sfs; } /** * 获得sfv. * * @return sfv */ public Signature getSfv() { return sfv; } /** * 设置sfv. * * @param sfv 要设置的 sfv */ public void setSfv(Signature sfv) { this.sfv = sfv; } public static void main(String[] args) throws SignException { DefaultCAPSigner s = new DefaultCAPSigner("12345678", "ydsc", "D:/ydsc.keystore", "D:/ydsc.cer"); System.out.println(s.verifyCAP("<?xml version=\"1.0\" encoding=\"UTF-8\"?><CAPRoot><SessionHeader><ServiceCode>CAP01001</ServiceCode><Version>2009062411025800</Version><ActionCode>0</ActionCode><TransactionID>11062201307124501978113</TransactionID><SrcSysID>11062</SrcSysID><DstSysID>11000</DstSysID><ReqTime>20130712214951</ReqTime><DigitalSign>302C021433BD45D34D4970225B2AF23913BE49CE6534AC6102142E77E0F300400619664C8ADF6D3C966A3343F10A</DigitalSign></SessionHeader><SessionBody><AuthReq><AuthInfo><AccountType>2000004</AccountType><AccountID>15301586546</AccountID><PWDType>01</PWDType><Password>U/niP0i6IU0=</Password></AuthInfo></AuthReq></SessionBody></CAPRoot>")); System.out.println(s.signatureCAP("<?xml version=\"1.0\" encoding=\"UTF-8\"?><CAPRoot><SessionHeader><ServiceCode>CAP01001</ServiceCode><Version>2009062411025800</Version><ActionCode>0</ActionCode><TransactionID>11062201307124501978113</TransactionID><SrcSysID>11062</SrcSysID><DstSysID>11000</DstSysID><ReqTime>20130712214951</ReqTime><DigitalSign/></SessionHeader><SessionBody><AuthReq><AuthInfo><AccountType>2000004</AccountType><AccountID>15301586546</AccountID><PWDType>01</PWDType><Password>U/niP0i6IU0=</Password></AuthInfo></AuthReq></SessionBody></CAPRoot>")); }}
0 0
- CAP协议字符串签名
- 签名字符串
- libpcap--GTPv2协议的cap文件解析
- cap
- CAP
- CAP
- CAP
- CAP
- CAP?
- 字符串 签名认证
- 分布式事务 Paxos协议CAP原理BASE原理
- TR069协议格式化签名数据包(一)
- CAP原理(CAP Theorem)
- CAP原理(CAP Theorem)
- CAP原理(CAP Theorem)
- CAP原理(CAP THEOREM)
- CAP原理(CAP Theorem)
- CAP原则(CAP定理)
- 外部解决滑动冲突水平方向和竖直方向滑动冲突实例
- OAuth2.0大致模型介绍
- 剩余的文件数
- 400. Nth Digit
- Leetcode #312 Burst Balloons
- CAP协议字符串签名
- mysql binlog row格式查看
- python 定时任务
- iOS高效调试
- STM32启动文件——startup_stm32f10x_hd.s
- 设计模式--Bridge 模式
- 项目实战-ViewPager
- Eclipse 自动对齐快捷键
- Android中的Thread, Looper和Handler机制(附带HandlerThread与AsyncTask)