?? sign.java
字號:
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public class sign
{
private static final String publickey = "publickey.key";
private static final String privatekey = "privatekey.key";
private static final String Algorithm ="RSA";
/**
* 生成密匙對
*
*/
public static void genpairKey() {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(Algorithm);
kpg.initialize(1024);
KeyPair keypair = kpg.generateKeyPair();
PublicKey pbkey = keypair.getPublic();
PrivateKey prkey = keypair.getPrivate();
writeKey(publickey, pbkey);
writeKey(privatekey, prkey);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 將生成的密匙寫入對應的文件
* @param filename
* @param obj
*/
public static void writeKey(String filename,Object obj) {
File file = new File(filename);
try {
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream out =new ObjectOutputStream(fout);
out.writeObject(obj);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 從對應的文件中得到密匙
* @param filename
* @return
*/
public static Object getKey(String filename) {
File file = new File(filename);
try {
FileInputStream fin = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fin);
Object t = in.readObject();
return t;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
return null;
}
/*
從文件中讀取字符串
*/
public static String getString(String path) {
File file = new File(path);
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int i = 0;
try
{
FileInputStream fin = new FileInputStream(file);
while((i=fin.read(bytes))>0) {
buffer.append(new String(bytes,0,i));
}
fin.close();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return buffer.toString();
}
public static byte[] getBytes(String path) {
File file = new File(path);
try
{
int i = 0;
FileInputStream fin = new FileInputStream(file);
byte[] bytes = new byte[fin.available()];
i=fin.read(bytes);
fin.close();
return bytes;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
/*
將字符串寫入文件中
*/
public static void storeString(String str, String path) {
File file = new File(path);
try
{
FileOutputStream fout = new FileOutputStream(file);
fout.write(str.getBytes());
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void storeString(byte[] str, String path) {
File file = new File(path);
try
{
FileOutputStream fout = new FileOutputStream(file);
fout.write(str);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
簽名
*/
public static void signed(){
PrivateKey privatekey = (PrivateKey)getKey(sign.privatekey);
String before = getString("before.txt");
try {
Signature signature =Signature.getInstance("SHA1WithRSA");
signature.initSign(privatekey);
signature.update(before.getBytes("UTF8"));
byte[] b = signature.sign();
storeString(b,"sign.txt");
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
驗證簽名
*/
public static void verify(){
PublicKey key = (PublicKey)getKey(sign.publickey);
String before = getString("before.txt");
byte[] signs = getBytes("sign.txt");
try
{
Signature signature =Signature.getInstance("SHA1WithRSA");
signature.initVerify(key);
signature.update(before.getBytes("utf8"));
if(signature.verify(signs)){
System.out.println("簽名驗證正確!!");
} else {
System.out.println("簽名驗證失?。?!");
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("簽名驗證失?。?!");
}
}
public static void main(String[] args){
System.out.println("可選鍵對應操作如下");
System.out.println("1:生成公密匙對。2:簽名。3:驗證。4:退出。");
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String in = null;
String mi = null;
try
{
while(true){
in = read.readLine();
if(in.equals("1")){
System.out.println("生成中..");
genpairKey();
System.out.println("公密匙對已經生成!");
} else if(in.equals("2")){
System.out.println("簽名...");
signed();
System.out.println("生成簽名成功!");
} else if(in.equals("3")){
System.out.println("驗證中.....");
verify();
} else if(in.equals("4")){
System.out.println("Byte Byte!");
break;
} else {
System.out.println("輸入有誤,請按提示鍵進行操作!!");
}
}
}
catch (Exception e)
{
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -