您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 5DSS签名算法验证
5.DSS签名算法验证packageDSS;importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.security.Key;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.PrivateKey;importjava.security.PublicKey;importjava.security.SecureRandom;importjava.security.Signature;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;importjava.util.HashMap;importjava.util.Map;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;publicclassDSS_Demo{//不仅可以使用DSA算法,同样也可以使用RSA算法做数字签名/*publicstaticfinalStringKEY_ALGORITHM=RSA;publicstaticfinalStringSIGNATURE_ALGORITHM=MD5withRSA;*/publicstaticfinalStringKEY_ALGORITHM=DSA;publicstaticfinalStringSIGNATURE_ALGORITHM=DSA;publicstaticfinalStringDEFAULT_SEED=$%^*%^()(HJG8awfjas7;//默认种子publicstaticfinalStringPUBLIC_KEY=DSAPublicKey;publicstaticfinalStringPRIVATE_KEY=DSAPrivateKey;publicstaticvoidmain(String[]args)throwsException{BufferedReaderplain=newBufferedReader(newInputStreamReader(newFileInputStream(C:\\Users\\Administrator\\workspace\\Cryptology\\src\\DSS\\plaintext.text)));Stringstr;while((str=plain.readLine())!=null){byte[]data=str.getBytes();MapString,ObjectkeyMap=initKey();//构建密钥PublicKeypublicKey=(PublicKey)keyMap.get(PUBLIC_KEY);PrivateKeyprivateKey=(PrivateKey)keyMap.get(PRIVATE_KEY);System.out.println(私钥format:+privateKey.getFormat());System.out.println(公钥format:+publicKey.getFormat());//产生签名Stringsign=sign(data,getPrivateKey(keyMap));//验证签名booleanverify1=verify(aaa.getBytes(),getPublicKey(keyMap),sign);System.err.println(经验证数据和签名匹配:+verify1);booleanverify=verify(data,getPublicKey(keyMap),sign);System.err.println(经验证数据和签名匹配:+verify);//System.out.println(数字签名为+sign);OutputStreamWriterresult=newOutputStreamWriter(newFileOutputStream(C:\\Users\\Administrator\\workspace\\Cryptology\\src\\DSS\\sign.text));result.write(sign);result.close();}}/***生成密钥*@paramseed种子*@return密钥对象*@throwsException*/publicstaticMapString,ObjectinitKey(Stringseed)throwsException{System.out.println(生成密钥);KeyPairGeneratorkeygen=KeyPairGenerator.getInstance(KEY_ALGORITHM);SecureRandomsecureRandom=newSecureRandom();secureRandom.setSeed(seed.getBytes());//Modulussizemustrangefrom512to1024andbeamultipleof64keygen.initialize(640,secureRandom);KeyPairkeys=keygen.genKeyPair();PrivateKeyprivateKey=keys.getPrivate();PublicKeypublicKey=keys.getPublic();MapString,Objectmap=newHashMapString,Object(2);map.put(PUBLIC_KEY,publicKey);map.put(PRIVATE_KEY,privateKey);returnmap;}/***生成默认密钥**@return密钥对象*@throwsException*/publicstaticMapString,ObjectinitKey()throwsException{returninitKey(DEFAULT_SEED);}/***取得私钥*@paramkeyMap*@return*@throwsException*/publicstaticStringgetPrivateKey(MapString,ObjectkeyMap)throwsException{Keykey=(Key)keyMap.get(PRIVATE_KEY);returnencryptBASE64(key.getEncoded());//base64加密私钥}/***取得公钥*@paramkeyMap*@return*@throwsException*/publicstaticStringgetPublicKey(MapString,ObjectkeyMap)throwsException{Keykey=(Key)keyMap.get(PUBLIC_KEY);returnencryptBASE64(key.getEncoded());//base64加密公钥}/***用私钥对信息进行数字签名*@paramdata加密数据*@paramprivateKey私钥-base64加密的*@return*@throwsException*/publicstaticStringsign(byte[]data,StringprivateKey)throwsException{System.out.println(用私钥对信息进行数字签名);byte[]keyBytes=decryptBASE64(privateKey);PKCS8EncodedKeySpeckeySpec=newPKCS8EncodedKeySpec(keyBytes);KeyFactoryfactory=KeyFactory.getInstance(KEY_ALGORITHM);PrivateKeypriKey=factory.generatePrivate(keySpec);//生成私钥//用私钥对信息进行数字签名Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);signature.initSign(priKey);signature.update(data);returnencryptBASE64(signature.sign());}/***BASE64Encoder加密*@paramdata要加密的数据*@return加密后的字符串*/privatestaticStringencryptBASE64(byte[]data){BASE64Encoderencoder=newBASE64Encoder();Stringencode=encoder.encode(data);returnencode;}privatestaticbyte[]decryptBASE64(Stringdata)throwsException{BASE64Decoderdecoder=newBASE64Decoder();byte[]buffer=decoder.decodeBuffer(data);returnbuffer;}publicstaticbooleanverify(byte[]data,StringpublicKey,Stringsign)throwsException{byte[]keyBytes=decryptBASE64(publicKey);X509EncodedKeySpeckeySpec=newX509EncodedKeySpec(keyBytes);KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);PublicKeypubKey=keyFactory.generatePublic(keySpec);Signaturesignature=Signature.getInstance(SIGNATURE_ALGORITHM);signature.initVerify(pubKey);signature.update(data);returnsignature.verify(decryptBASE64(sign));//验证签名}}运行结果:
本文标题:5DSS签名算法验证
链接地址:https://www.777doc.com/doc-2928678 .html