JAVAサイン

6630 ワード

JAva署名は、主にユーザーがファイル内の機密情報を変更することを防止するために使用されます.
    1.公開秘密鍵ペアを生成します.コードは次のとおりです.
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

public class Test {
	public static void main(String[] args) throws Exception{
		FileOutputStream fos_public = new FileOutputStream("f:\\public.key");
		ObjectOutputStream oos_public = new ObjectOutputStream(fos_public);
		FileOutputStream fos_private = new FileOutputStream("f:\\private.key");
		ObjectOutputStream oos_private = new ObjectOutputStream(fos_private);
		//   DSA      
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");  
		keyGen.initialize(1024); 
		//           
		KeyPair key = keyGen.generateKeyPair(); 
		PublicKey publicKey = key.getPublic();  
		PrivateKey privateKey = key.getPrivate(); 
		//    
		oos_public.writeObject(publicKey); 
		oos_private.writeObject(privateKey);  
		fos_public.close();   
		oos_public.close(); 
		fos_private.close(); 
		oos_private.close(); 
	}
}

     2.保護が必要な書類に署名する
    
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.Signature;

public class GenSgn {
	public static void main(String[] args) throws Exception{
		File file_info = new File("f:\\test.txt");
		FileInputStream fis_info = new FileInputStream(file_info);
		int fileInfoLength = (int)file_info.length();
		byte[] infoBytes = new byte[fileInfoLength];
		fis_info.read(infoBytes);
		fis_info.close();
		
		FileInputStream fis_private = new FileInputStream("f:\\private.key"); 
		ObjectInputStream ois_private = new ObjectInputStream(fis_private);
		PrivateKey privateKey = (PrivateKey)ois_private.readObject();
		fis_private.close();
		ois_private.close();
		//     
		Signature sig=Signature.getInstance("DSA");
		//                
		sig.initSign(privateKey);
		// msgBytes       
		sig.update(infoBytes);
		//    ,         signatureBytes   
		byte[] signatureBytes = sig.sign();
		/*
		//       signature.sgn 
		FileOutputStream fos_signature = new FileOutputStream("f:\\signature.sgn");   
		fos_signature.write(signatureBytes);   
		fos_signature.close(); 
		*/
		
		
		
		
		FileOutputStream fos_xxx = new FileOutputStream("f:\\signature.xxx");
		String s1 = "";
		for(byte b:infoBytes){
			s1 += b+","; 
		}
		String s2 = "";
		for(byte b:signatureBytes){
			s2 += b+","; 
		}
		System.out.println(new String(infoBytes));
		System.out.println();
		String t = s1.substring(0,s1.length())+"

"+s2.substring(0,s2.length()); fos_xxx.write(t.getBytes()); fos_xxx.close(); } }

    3.検証ファイル
    
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PublicKey;
import java.security.Signature;


public class ValiSgn {

	public static void main(String[] args)throws Exception {
		/*
		//        
		File file_info = new File("D:\\upload\\test.txt");
		FileInputStream fis_info = new FileInputStream(file_info);   
		int fileInfoLength = (int)file_info.length();  
		byte[] infoBytes = new byte[fileInfoLength];   
		fis_info.read(infoBytes);   
		fis_info.close();
		
		
		//           
		FileInputStream fis_public = new FileInputStream("f:\\public.key");   
		ObjectInputStream ois_public = new ObjectInputStream(fis_public);  
		PublicKey publicKey = (PublicKey)ois_public.readObject();
		fis_public.close();
		ois_public.close(); 
		
		//          
		File file_signature = new File("D:\\upload\\signature.sgn");
		FileInputStream fis_signature = new FileInputStream(file_signature);   
		int fileSignatureLength = (int)file_signature.length();   
		byte[] signatureBytes = new byte[fileSignatureLength];
		fis_signature.read(signatureBytes);   
		fis_signature.close(); 
		
		System.out.println(new String(signatureBytes));
		
		//          
		Signature sig=Signature.getInstance("DSA");   
		sig.initVerify(publicKey);
		sig.update(infoBytes);   
		if(sig.verify(signatureBytes)){
			System.out.println("       "); 
		}else{
			System.out.println("     ");
		}
		*/
		
		File file_t = new File("f:\\signature.xxx");
		FileInputStream fis_t = new FileInputStream(file_t);
		int fileInfoLength = (int)file_t.length();  
		byte[] tBytes = new byte[fileInfoLength];   
		fis_t.read(tBytes);   
		fis_t.close();
		
		String t = new String(tBytes);
		String[] s1 = t.substring(0,t.indexOf("

")).replace("

", "").split(","); String[] s2 = t.substring(t.indexOf("

")).replace("

", "").split(","); byte[] b1 = new byte[s1.length]; for(int i=0;i<s1.length;i++){ b1[i]=Byte.parseByte(s1[i]); } byte[] b2 = new byte[s2.length]; for(int i=0;i<s2.length;i++){ b2[i]=Byte.parseByte(s2[i]); } FileInputStream fis_public = new FileInputStream("f:\\public.key"); ObjectInputStream ois_public = new ObjectInputStream(fis_public); PublicKey publicKey = (PublicKey)ois_public.readObject(); fis_public.close(); ois_public.close(); Signature sig=Signature.getInstance("DSA"); sig.initVerify(publicKey); sig.update(b1); if(sig.verify(b2)){ System.out.println(" "); }else{ System.out.println(" "); } } }

    ファイルが変更されていない場合は、検証に合格できますが、逆に合格できません.
    なお,署名が修正された例外処理を加える.