Click here to Skip to main content
15,890,741 members
Home / Discussions / Java
   

Java

 
AnswerRe: Key Logger Pin
TorstenH.7-Sep-09 1:35
TorstenH.7-Sep-09 1:35 
AnswerRe: Key Logger Pin
EliottA7-Sep-09 4:44
EliottA7-Sep-09 4:44 
AnswerRe: Key Logger Pin
42774807-Sep-09 8:01
42774807-Sep-09 8:01 
QuestionDigital Signature Pin
vivhari3-Sep-09 20:26
vivhari3-Sep-09 20:26 
AnswerRe: Digital Signature Pin
vivhari3-Sep-09 20:56
vivhari3-Sep-09 20:56 
AnswerRe: Digital Signature Pin
Nagy Vilmos3-Sep-09 22:02
professionalNagy Vilmos3-Sep-09 22:02 
GeneralRe: Digital Signature Pin
42774804-Sep-09 10:50
42774804-Sep-09 10:50 
GeneralRe: Digital Signature Pin
42774804-Sep-09 16:46
42774804-Sep-09 16:46 
Ok since my last post I was thinking about how to implement (Just to learn more thats all) the digital awareness but came to a conclusion that for every application we would require to define its own awareness method which I find not practical. Luckily I remembered what we took in email security especially S/MIME the concept of envelop, therefore I thought why not do the same with this problem.

The solution is to convert the given file into a byte array text file and then digitally sign it.

The code I used is as follows (I was lazy to do the output for the data,key,sig) but its easy to do.

import java.io.FileInputStream;
import java.io.InputStream;
//import java.math.BigInteger;
import java.security.InvalidKeyException;
//import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
//import java.security.interfaces.DSAParams;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
//import java.security.spec.DSAPublicKeySpec;
//import java.security.spec.KeySpec;

public class SigningDocument 
{
	public static void main(String[] args) throws Exception 
	{
		InputStream in = new FileInputStream("dcaods.pdf");
		int bytesRead=0;
		int bytesToRead=1024;
		byte[] input = new byte[bytesToRead];
		while (bytesRead < bytesToRead) 
		{
			int result = in.read(input, bytesRead, bytesToRead - bytesRead);
			if (result == -1) break;
			bytesRead += result;
		}

		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
		keyGen.initialize(1024);
		KeyPair keypair = keyGen.genKeyPair();
		DSAPrivateKey privateKey = (DSAPrivateKey)keypair.getPrivate();
		DSAPublicKey publicKey = (DSAPublicKey)keypair.getPublic();

		/* Data to be sent
		DSAParams dsaParams = privateKey.getParams();
		BigInteger p = dsaParams.getP();
		BigInteger q = dsaParams.getQ();
		BigInteger g = dsaParams.getG();
		BigInteger y = publicKey.getY();
		KeyFactory keyFactory = KeyFactory.getInstance("DSA");
		KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
		PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);//rebuild key
		byte[] signature= createSignature(privateKey,input);
		*/

		byte[] signature= createSignature(privateKey,input);

		boolean v = verifySignature(publicKey,input,signature);

		System.out.print(v);
	}
	public static byte[] createSignature(PrivateKey key, byte[] buffer) 
	{
		try 
		{
			Signature sig = Signature.getInstance(key.getAlgorithm());
			sig.initSign(key);
			sig.update(buffer, 0, buffer.length);
			return sig.sign();
		} 
		catch (SignatureException e) {} 
		catch (InvalidKeyException e) {} 
		catch (NoSuchAlgorithmException e) {}
		
		return null;
	}
	public static boolean verifySignature(PublicKey key, byte[] buffer, byte[] signature) 
	{
		try 
		{
			Signature sig = Signature.getInstance(key.getAlgorithm());
			sig.initVerify(key);
			sig.update(buffer, 0, buffer.length);
			return sig.verify(signature);
		} 
		catch (SignatureException e) {}
		catch (InvalidKeyException e) {}
		catch (NoSuchAlgorithmException e) {}
		
		return false;
	}
}


What do you think guys?
GeneralRe: Digital Signature Pin
vivhari5-Sep-09 5:34
vivhari5-Sep-09 5:34 
GeneralRe: Digital Signature Pin
42774805-Sep-09 12:42
42774805-Sep-09 12:42 
QuestionFinding Duplicates:(Loop within a Loop) Pin
squalled3-Sep-09 20:20
squalled3-Sep-09 20:20 
AnswerRe: Finding Duplicates:(Loop within a Loop) Pin
Nagy Vilmos3-Sep-09 21:58
professionalNagy Vilmos3-Sep-09 21:58 
AnswerRe: Finding Duplicates:(Loop within a Loop) Pin
TorstenH.4-Sep-09 2:55
TorstenH.4-Sep-09 2:55 
AnswerRe: Finding Duplicates:(Loop within a Loop) Pin
42774804-Sep-09 8:52
42774804-Sep-09 8:52 
QuestionValidation Pin
hitesh.kalra2-Sep-09 2:07
hitesh.kalra2-Sep-09 2:07 
AnswerRe: Validation Pin
42774802-Sep-09 7:11
42774802-Sep-09 7:11 
GeneralRe: Validation Pin
David Skelly2-Sep-09 22:24
David Skelly2-Sep-09 22:24 
GeneralRe: Validation Pin
42774803-Sep-09 1:44
42774803-Sep-09 1:44 
GeneralRe: Validation Pin
David Skelly3-Sep-09 2:39
David Skelly3-Sep-09 2:39 
QuestionRunning Blackberry Application........ Pin
shaina223131-Aug-09 21:41
shaina223131-Aug-09 21:41 
AnswerRe: Running Blackberry Application........ Pin
EliottA2-Sep-09 3:00
EliottA2-Sep-09 3:00 
Questionplease help ... Pin
hitesh.kalra30-Aug-09 23:25
hitesh.kalra30-Aug-09 23:25 
AnswerRe: please help ... Pin
Christian Graus2-Sep-09 14:50
protectorChristian Graus2-Sep-09 14:50 
Questionjava coding Pin
hitesh.kalra30-Aug-09 23:20
hitesh.kalra30-Aug-09 23:20 
AnswerRe: java coding Pin
427748030-Aug-09 23:48
427748030-Aug-09 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.