5.DSS签名算法验证
package DSS;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
统计核算
import java.io.OutputStreamWriter;
import java.curity.Key;
import java.curity.KeyFactory;
import java.curity.KeyPair;
import java.curity.KeyPairGenerator;
import java.curity.PrivateKey;
import java.curity.PublicKey;
import java.curity.SecureRandom;
import java.curity.Signature;
import java.curity.spec.PKCS8EncodedKeySpec;
import java.curity.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DSS_Demo {
//不仅可以使用DSA算法,同样也可以使用RSA算法做数字签名
/*public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";*/
public static final String KEY_ALGORITHM = "DSA";
public static final String SIGNATURE_ALGORITHM = "DSA";
public static final String DEFAULT_SEED = "$%^*%^()(HJG8awfjas7"; //默认种子
public static final String PUBLIC_KEY = "DSAPublicKey";
public static final String PRIVATE_KEY = "DSAPrivateKey";
public static void main(String[] args) throws Exception {
BufferedReader plain=new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Urs\\Administrator\\workspace\\Cryptology\\src\\DSS\\")));
String str ;
while((adLine())!=null)
{
byte[] data = Bytes();
Map<String, Object> keyMap = initKey();// 构建密钥
龙年对联
白带发黄是什么原因 PublicKey publicKey = (PublicKey) (PUBLIC_KEY);
PrivateKey privateKey = (PrivateKey) (PRIVATE_KEY);
System.out.println("私钥format:" + Format());
System.out.println("公钥format:" + Format());
// 产生签名 破釜沉舟近义词
String sign = sign(data, getPrivateKey(keyMap));
// 验证签名
boolean verify1 = verify("aaa".getBytes(), getPublicKey(keyMap), sign);
println("经验证 数据和签名匹配: " + verify1);
boolean verify = verify(data, getPublicKey(keyMap), sign);
println("经验证 数据和签名匹配: "+verify);
// System.out.println("数字签名为 "+sign);
OutputStreamWriter result=new OutputStreamWriter(new FileOutputStream("C:\\Urs\\Administrator\\workspace\\Cryptology\\src\\DSS\\"));
心理学书籍推荐 result.write(sign);
result.clo();
}
}
/**
* 生成密钥
* @param ed 种子
* @return 密钥对象 拥挤的公交车
* @throws Exception
*/
public static Map<String, Object> initKey(String ed) throws Exception {
System.out.println("生成密钥");
KeyPairGenerator keygen = Instance(KEY_ALGORITHM);
SecureRandom cureRandom = new SecureRandom();
cureRandom.Bytes());
//Modulus size must range from 512 to 1024 and be a multiple of 64
keygen.initialize(640, cureRandom);
KeyPair keys = KeyPair();
PrivateKey privateKey = Private();
PublicKey publicKey = Public();
Map<String, Object> map = new HashMap<String, Object>(2);
map.put(PUBLIC_KEY, publicKey); 什锦菜的做法
map.put(PRIVATE_KEY, privateKey);
return map;
}
/**
* 生成默认密钥
*
* @return 密钥对象
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
return initKey(DEFAULT_SEED);
益智游戏教案
}
/**
* 取得私钥
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) (PRIVATE_KEY);
return Encoded()); //ba64加密私钥
}
/**
* 取得公钥
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) (PUBLIC_KEY);