《电子商务安全专业技术实验3》由会员分享,可在线阅读,更多相关《电子商务安全专业技术实验3(8页珍藏版)》请在金锄头文库上搜索。
1、西安邮电大学电子商务安全技术实验三报告系 部 名 称: 经济与管理学院学 生 姓 名:韩振伟专 业 名 称:电子商务班 级:1101班学 号:02112003时 间:2014-5-10实验三一、实验目的:首先利用Keytool工具制作数字证书,利用JAVA语言,使用数字证书对信息进行加密和解密,使学生通过实际操作加深对数字证书的生成、数字证书的加密与解密的理解。二、实验内容:安装JDK,配置Java开发环境,利用keytool工具生成keystore、再利用keytool工具导出生成的证书,然后解压eclipse,编写读取数字证书,读取数字证书中的信息,利用数字证书进行加密与解密的程序。三、实
2、验用到的主要技术及工具主要技术:Java、Bouncy Castle主要工具:Eclipse四、开发步骤:1、安装JDK,配置JAVA环境变量。2、利用keytool -genkey -validity 36000 -alias -keyalg RSA -keystore D:test.keystore命令生成证书库3、利用keytool -export -keystore D:test.keystore -alias -file D:xupt.cer rfc命令导出证书。4、解压eclipse。5、在eclipse中新建项目6、编写读取数字证书程序。package ;import ;im
3、port ;import ;import ;import ;import ;import ;import ;import ;public abstract class CertificateCoder public static final String CERT_TYPE = X.509;private static PrivateKey getPrivateKeyByKeyStore(String keyStorePath,String alias, String password) throws Exception KeyStore ks = getKeyStore(keyStorePa
4、th, password);return (PrivateKey) ks.getKey(alias, password.toCharArray();Private static PublicKey getPublicKeyByCertificate(String certificatePath)throws Exception Certificate certificate = getCertificate(certificatePath);return certificate.getPublicKey();private static Certificate getCertificate(S
5、tring certificatePath)throws Exception CertificateFactory certificateFactory = CertificateFactory.getInstance(CERT_TYPE);FileInputStream in = new FileInputStream(certificatePath); Certificate certificate = certificateFactory.generateCertificate(in);in.close();return certificate;private static Certif
6、icate getCertificate(String keyStorePath,String alias, String password) throws Exception KeyStore ks = getKeyStore(keyStorePath, password);return ks.getCertificate(alias);private static KeyStore getKeyStore(String keyStorePath, String password)throws Exception KeyStore ks = KeyStore.getInstance(KeyS
7、tore.getDefaultType();FileInputStream is = new FileInputStream(keyStorePath);ks.load(is, password.toCharArray();is.close();return ks;public static byte encryptByPrivateKey(byte data, String keyStorePath,String alias, String password) throws Exception PrivateKey privateKey = getPrivateKeyByKeyStore(k
8、eyStorePath, alias,password);Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE, privateKey);return cipher.doFinal(data);public static byte decryptByPrivateKey(byte data, String keyStorePath,String alias, String password) throws Exception PrivateKey privateK
9、ey = getPrivateKeyByKeyStore(keyStorePath, alias,password);Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm();cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);public static byte encryptByPublicKey(byte data, String certificatePath)throws Exception PublicKey publicKe
10、y = getPublicKeyByCertificate(certificatePath);Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte decryptByPublicKey(byte data, String certificatePath)throws Exception PublicKey publicKey = getPublicK
11、eyByCertificate(certificatePath);Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm();cipher.init(Cipher.DECRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte sign(byte sign, String keyStorePath, String alias,String password) throws Exception X509Certificate x509Certificate
12、= (X509Certificate) getCertificate(keyStorePath, alias, password);Signature signature = Signature.getInstance(x509Certificate.getSigAlgName();PrivateKey privateKey = getPrivateKeyByKeyStore(keyStorePath, alias,password);signature.initSign(privateKey);signature.update(sign);return signature.sign();pu
13、blic static boolean verify(byte data, byte sign,String certificatePath) throws Exception X509Certificate x509Certificate = (X509Certificate) getCertificate(certificatePath);Signature signature = Signature.getInstance(x509Certificate.getSigAlgName();signature.initVerify(x509Certificate);signature.upd
14、ate(data); return signature.verify(sign);7、编写读取数字证书中公钥的程序。package ;import static ;import ;import ;public class CertificateCoderTest private String password = keystore;private String alias = ;private String certificatePath = d:/aur.cer;private String keyStorePath = d:/test.keystore;Testpublic void test1() throws Exception 公钥加密私钥解密);String inputStr = Ceritifcate;byte data = inputStr.getBytes();byte encrypt = CertificateCoder.encryptByPublicKey(data,