package sample.crypto;import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.security.KeyPair;
import java.security.Signature;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.MessageDigest;
import java.security.KeyPairGenerator;
import java.security.SignatureException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;public final class DigitalSignatureSample
extends Object
{
public static void main(String[] args)
throws IOException,
SignatureException,
InvalidKeyException,
ClassNotFoundException,
NoSuchAlgorithmException
{
KeyPairGenerator keyPairGenerator = null;
KeyPair keyPair = null;
PublicKey publicKey = null;
PrivateKey privateKey = null; byte[] source = null;
byte[] result = null;
boolean passed = false; String publicKeyFileName = null;
String privateKeyFileName = null;
String sourceFileName = null;
String resultFileName = null;
String messageDigestAlgorithm = null; for (int i = 0; i<CRYPTOGRAPHERS.length; i++)
{
publicKeyFileName =
PUBLIC_KEY_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
privateKeyFileName =
PRIVATE_KEY_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
sourceFileName =
SOURCE_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
resultFileName =
RESULT_FILE_NAME+CRYPTOGRAPHERS[i]+".txt";
messageDigestAlgorithm = "MD5with"+CRYPTOGRAPHERS[i]; source = loadSource(getSourceFile());
keyPairGenerator =
KeyPairGenerator.getInstance(CRYPTOGRAPHERS[i]);
keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate(); if ((publicKey instanceof RSAPublicKey)&&
(privateKey instanceof RSAPrivateKey))
result = signRSA(
(RSAPrivateKey)privateKey,
messageDigestAlgorithm,
source); savePublicKey(publicKey, publicKeyFileName);
savePrivateKey(privateKey, privateKeyFileName);
saveResult(result, resultFileName); publicKey = loadPublicKey(publicKeyFileName);
privateKey = loadPrivateKey(privateKeyFileName);
result = loadResult(resultFileName); if ((publicKey instanceof RSAPublicKey)&&
(privateKey instanceof RSAPrivateKey))
passed = verifyRSA(
(RSAPublicKey)publicKey,
messageDigestAlgorithm,
source,
result); System.out.println("Veritication: "+passed);
}
} private static void saveByteArray(
byte[] byteArray,
String fileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(fileName);
outFile.write(byteArray);
outFile.close();
} private static byte[] loadByteArray(
String fileName)
throws IOException
{
FileInputStream inFile = new FileInputStream(fileName);
ByteArrayOutputStream outByteArray =
new ByteArrayOutputStream();
int i = -1;
while ((i = inFile.read())!=-1)
outByteArray.write((byte)i);
byte[] byteArray = outByteArray.toByteArray();
inFile.close();
outByteArray.close();
return byteArray;
} private static void saveObject(
Object object,
String fileName)
throws IOException
{
FileOutputStream outFile =
new FileOutputStream(fileName);
ObjectOutputStream outObject =
new ObjectOutputStream(outFile); outObject.writeObject(object); outObject.close();
outFile.close();
} private static Object loadObject(
String fileName)
throws IOException,
ClassNotFoundException
{
FileInputStream inFile =
new FileInputStream(fileName);
ObjectInputStream inObject =
new ObjectInputStream(inFile);
Object object = inObject.readObject();
inObject.close();
inFile.close();
return object;
} private static void saveSource(
byte[] source,
String fileName)
throws IOException
{
saveByteArray(source, fileName);
} private static byte[] loadSource(File file)
throws IOException
{
FileInputStream inFile = new FileInputStream(file);
ByteArrayOutputStream outByteArray =
new ByteArrayOutputStream();
int i = -1;
while ((i = inFile.read())!=-1)
outByteArray.write((byte)i);
return outByteArray.toByteArray();
} private static void saveResult(
byte[] result,
String fileName)
throws IOException
{
saveByteArray(result, fileName);
} private static byte[] loadResult(
String fileName)
throws IOException
{
return loadByteArray(fileName);
} private static void savePublicKey(
PublicKey publicKey,
String fileName)
throws IOException
{
saveObject(publicKey, fileName);
} private static PublicKey loadPublicKey(
String fileName)
throws IOException,
ClassNotFoundException
{
return (PublicKey)loadObject(fileName);
} private static void savePrivateKey(
PrivateKey privateKey,
String fileName)
throws IOException
{
saveObject(privateKey, fileName);
} private static PrivateKey loadPrivateKey(
String fileName)
throws IOException,
ClassNotFoundException
{
return (PrivateKey)loadObject(fileName);
} private static byte[] signRSA(
RSAPrivateKey privateKey,
String algorithm,
byte[] source)
throws SignatureException,
InvalidKeyException,
NoSuchAlgorithmException
{
Signature signature = Signature.getInstance(algorithm);
signature.initSign(privateKey);
signature.update(source);
return signature.sign();
} private static boolean verifyRSA(
RSAPublicKey publicKey,
String algorithm,
byte[] source,
byte[] result)
throws SignatureException,
InvalidKeyException,
NoSuchAlgorithmException
{
Signature signature = Signature.getInstance(algorithm);
signature.initVerify(publicKey);
signature.update(source);
return signature.verify(result);
} private static File getSourceFile()
throws IOException
{
return new File("Source.txt");
} private static final String[] CRYPTOGRAPHERS =
new String[]
{
"RSA"
}; private static final String RESULT_FILE_NAME =
"DigitalSignatureResult_"; private static final String SOURCE_FILE_NAME =
"DigitalSignatureSource_"; private static final String PUBLIC_KEY_FILE_NAME =
"DigitalSignaturePublicKey_"; private static final String PRIVATE_KEY_FILE_NAME =
"DigitalSignaturePrivateKey_"; private DigitalSignatureSample()
{
super();
}
}