?? gensig.java
字號(hào):
package javasecurity;
import java.io.*;
import java.security.*;
class GenSig
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.out.println("Usage: GenSig nameOfFileToSign");
}
else
{
try
{
/* 生成一對(duì)密鑰 */
KeyPairGenerator keyGen =
KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random =
SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/* 建立一個(gè)證書(shū)并用私有密鑰初始化 */
Signature dsa =
Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
/* 更新并對(duì)數(shù)據(jù)進(jìn)行簽署 */
FileInputStream fis = new FileInputStream(args[0]);
BufferedInputStream bufin = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int len;
while (bufin.available() != 0)
{
len = bufin.read(buffer);
dsa.update(buffer, 0, len);
};
bufin.close();
byte[] realSig = dsa.sign();
/* 將證書(shū)文件存儲(chǔ)到文件中 */
FileOutputStream sigfos = new FileOutputStream("sig");
sigfos.write(realSig);
sigfos.close();
/* 將公共密鑰存儲(chǔ)到文件中 */
byte[] key = pub.getEncoded();
FileOutputStream keyfos = new FileOutputStream("suepk");
keyfos.write(key);
keyfos.close();
}
catch (Exception e)
{
System.err.println("Caught exception " + e.toString());
}
}
};
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -