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();
}
}
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();
}
}
解决方案 »
- 为什么package explorer下的工程 的很多文件(夹)图标的左下方都有个小红叉,怎么解决
- 关于这个程序,大家来看看
- JLabel问题
- 利用JSci画图中碰到的问题
- 跪谢!我作拉一个简单的想记事本样的程序可以运行,但有点问题请进来看看可以吗谢谢。。(有源程序,具体问题描述)
- os版本问题
- java mina 服务器和 c++ 客户端通讯的问题
- 明明有那个类的JAR 却不能导入进来 这么怎么回事呢
- 一个很普通的问题,关于C/S的一个socket,编译可通过,就是连不上,麻烦各位看一下,急!!!
- 用Socket做网络功能有什么限制对很多用户而言如何提高效率?
- 初学者的问题!!!
- 求口令加密源代码
http://expert.csdn.net/Expert/TopicView1.asp?id=2097451
http://expert.csdn.net/Expert/TopicView1.asp?id=2137716