我正在使用SHA1withRSA算法来签署文本文件,但是当我使用例如java JDK 1.6创建签名并尝试在同一台机器上验证同一文件但在JDK 1.7上它未经验证时,请在这里帮助我的代码签名
char[] pwd = "XXXXXX".toCharArray();
KeyStore ks = getKeyStore(args[0], pwd);
//KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("test", "abc123".toCharArray()); PrivateKey priv = (PrivateKey) ks.getKey("wps", pwd); PublicKey pub = ks.getCertificate("wps").getPublicKey(); //PrivateKey priv = pair.getPrivate(); //PublicKey pub = pair.getPublic(); Signature rsa = Block quote
Signature.getInstance("SHA1withRSA");
rsa.initSign(priv);
FileInputStream fis = new FileInputStream(args[1]);
DataInputStream dis = new DataInputStream(fis);
//BufferedReader bf = new BufferedReader(new InputStreamReader(dis));
BufferedReader bf = new BufferedReader(new InputStreamReader(dis,"UTF8"));
String strLine;
String strSign = "";
int len = 0;
boolean alreadySigned = false;
while ((strLine = bf.readLine()) != null) {
len +=strLine.getBytes("UTF8").length;
//if (strLine.indexOf("-") > -1) {
if (strLine.startsWith("-")) {
while ((strLine = bf.readLine()) != null) {
strSign += strLine;
}
}
}
System.out.println("Number of bytes:" + len);
fis.close();
fis = new FileInputStream(args[1]);
dis = new DataInputStream(fis);
//bf = new BufferedReader(new InputStreamReader(dis));
bf = new BufferedReader(new InputStreamReader(fis, "UTF8"));
while ((strLine = bf.readLine()) != null) {
//if (strLine.indexOf("-") > -1) {
if (strLine.startsWith("-")) {
System.out.println("Substring" + strLine.substring(0,strLine.length() - 1));
//rsa.update(strLine.substring(0,strLine.length() - 1).getBytes("UTF-8"));
rsa.update(strLine.substring(0,strLine.length() - 1).getBytes());
}else
//rsa.update(strLine.getBytes("UTF-8"));
rsa.update(strLine.getBytes());
}
fis.close();
realSig = rsa.sign();
FileOutputStream sigfos = new FileOutputStream(args[1], true);
OutputStreamWriter wr = new OutputStreamWriter(sigfos, "UTF8");
String base64 = new sun.misc.BASE64Encoder().encode(realSig);
String lineSep = System.getProperty("line.separator");
//wr.write(lineSep.getBytes());
wr.write(lineSep);
//sigfos.write("[Signature]".getBytes());
//sigfos.write(lineSep.getBytes());
base64=base64.replaceAll(System.getProperty("line.separator"),"");
//sigfos.write(base64.getBytes());
//sigfos.flush();
//sigfos.close();
wr.write(base64);
wr.flush();
wr.close();
这是验证的代码
realSig = new sun.misc.BASE64Decoder().decodeBuffer(strSign);
//Verify
rsa = Signature.getInstance("SHA1withRSA");
rsa.initVerify(pub);
fis = new FileInputStream(args[1]);
//dis = new DataInputStream(fis);
bf = new BufferedReader(new InputStreamReader(fis,"UTF8"));
//bf = new BufferedReader(new InputStreamReader(dis));
strLine = null;
while ((strLine = bf.readLine()) != null) {
//if (strLine.indexOf("-") > -1) {
if (strLine.startsWith("-")) {
rsa.update(strLine.substring(0,strLine.length() - 1).getBytes());
break;
}
rsa.update(strLine.getBytes());
}
;
fis.close();
请告知我如何在所有jdk版本上验证签名