《电子商务安全技术实验.doc》由会员分享,可在线阅读,更多相关《电子商务安全技术实验.doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、西安邮电大学电子商务安全技术实验一报告系 部 名 称: 经济与管理学院学 生 姓 名:韩振伟专 业 名 称:电子商务班 级:1101班学 号:时 间:2014-5-10一、实验目的:通过JAVA语言,来实现对称密钥加密算法,非对称秘钥加密算法对信息的加密解密,通过实际操作加深学生对对称密钥加密、非对称秘钥加密解密的理解。二、实验内容:安装JDK,配置Java开发环境,加压eclipse,编写对称秘钥的生成、对称密钥加密、解密的程序。编写非对称秘钥加密解密的程序,用私钥对信息进行加密,用公钥对信息进行解密,然后用公钥对信息进行加密,用私钥对信息进行解密。三、实验用到的主要技术及工具主要技术:Ja
2、va、Bouncy Castle主要工具:Eclipse四、开发步骤:1、安装JDK,配置JAVA环境变量。2、解压eclipse。3、在eclipse中新建项目4、编写使用DES算法生成秘钥的程序。package org.zlex.chapter07_1;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.Se
3、cretKeyFactory;import javax.crypto.spec.DESKeySpec;public abstract class DESCoder public static final String KEY_ALGORITHM = DES;public static final String CIPHER_ALGORITHM = DES/ECB/PKCS5PADDING;private static Key toKey(byte key) throws Exception DESKeySpec dks = new DESKeySpec(key);SecretKeyFactor
4、y keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);SecretKey secretKey = keyFactory.generateSecret(dks);return secretKey;public static byte decrypt(byte data, byte key) throws Exception Key k = toKey(key);Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, k
5、);return cipher.doFinal(data);public static byte encrypt(byte data, byte key) throws Exception Key k = toKey(key);Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, k);return cipher.doFinal(data);public static byte initKey() throws Exception KeyGenerator kg = KeyGe
6、nerator.getInstance(KEY_ALGORITHM);kg.init(56, new SecureRandom();SecretKey secretKey = kg.generateKey();return secretKey.getEncoded();package org.zlex.chapter07_1;import static org.junit.Assert.*;import mons.codec.binary.Base64;import org.junit.Test;public class DESCoderTest Testpublic final void t
7、est() throws Exception String inputStr = DES;byte inputData = inputStr.getBytes();System.err.println(原文:t + inputStr);byte key = DESCoder.initKey();System.err.println(密钥:t + Base64.encodeBase64String(key);inputData = DESCoder.encrypt(inputData, key);System.err.println(加密后:t + Base64.encodeBase64Stri
8、ng(inputData);byte outputData = DESCoder.decrypt(inputData, key);String outputStr = new String(outputData);System.err.println(解密后:t + outputStr);assertEquals(inputStr, outputStr);5、使用生成的秘钥对“电子商务安全技术”进行加密。6、用第4步骤中生成的秘钥对第5部中生成的加密后的内容进行解密。7、使用AES算法重复4-6步骤。package org.zlex.chapter07_3;import java.securi
9、ty.Key;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public abstract class AESCoder public static final String KEY_ALGORITHM = AES;public static final String CIPHER_ALGORITHM = AES/ECB/PKCS5Padding;private static Key
10、toKey(byte key) throws Exception SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);return secretKey;public static byte decrypt(byte data, byte key) throws Exception Key k = toKey(key);Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, k);return cipher.doF
11、inal(data);public static byte encrypt(byte data, byte key) throws Exception Key k = toKey(key);Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, k);return cipher.doFinal(data);public static byte initKey() throws Exception KeyGenerator kg = KeyGenerator.getInstance
12、(KEY_ALGORITHM);kg.init(128);SecretKey secretKey = kg.generateKey();return secretKey.getEncoded();package org.zlex.chapter07_3;import static org.junit.Assert.*;import mons.codec.binary.Base64;import org.junit.Test;public class AESCoderTest Testpublic final void test() throws Exception String inputSt
13、r = AES;byte inputData = inputStr.getBytes();System.err.println(原文:t + inputStr);byte key = AESCoder.initKey();System.err.println(密钥:t + Base64.encodeBase64String(key);inputData = AESCoder.encrypt(inputData, key);System.err.println(加密后:t + Base64.encodeBase64String(inputData);byte outputData = AESCo
14、der.decrypt(inputData, key);String outputStr = new String(outputData);System.err.println(解密后:t + outputStr);assertEquals(inputStr, outputStr);8、使用RSA算法生成公钥和私钥。package org.zlex.chapter08_2;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPa
15、irGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.M
16、ap;import javax.crypto.Cipher;public abstract class RSACoder public static final String KEY_ALGORITHM = RSA;private static final String PUBLIC_KEY = RSAPublicKey;private static final String PRIVATE_KEY = RSAPrivateKey;private static final int KEY_SIZE = 512;public static byte decryptByPrivateKey(byt
17、e data, byte key)throws Exception PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Ci
18、pher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);public static byte decryptByPublicKey(byte data, byte key)throws Exception X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePu
19、blic(x509KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.DECRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte encryptByPublicKey(byte data, byte key)throws Exception X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory ke
20、yFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte encryptByPrivateKey(byte data, byte ke
21、y)throws Exception PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.ENCRYPT_MO
22、DE, privateKey);return cipher.doFinal(data);public static byte getPrivateKey(Map keyMap)throws Exception Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();public static byte getPublicKey(Map keyMap)throws Exception Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();public s
23、tatic Map initKey() throws Exception KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(KEY_SIZE);KeyPair keyPair = keyPairGen.generateKeyPair();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair
24、.getPrivate();Map keyMap = new HashMap(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;package org.zlex.chapter08_2;import static org.junit.Assert.*;import mons.codec.binary.Base64;import org.junit.Before;import org.junit.Test;import java.util.Map;public class
25、RSACoderTest private byte publicKey;private byte privateKey;Beforepublic void initKey() throws Exception Map keyMap = RSACoder.initKey();publicKey = RSACoder.getPublicKey(keyMap);privateKey = RSACoder.getPrivateKey(keyMap);System.err.println(公钥: n + Base64.encodeBase64String(publicKey);System.err.pr
26、intln(私钥: n + Base64.encodeBase64String(privateKey);Testpublic void test() throws Exception System.err.println(n-私钥加密公钥解密-);String inputStr1 = RSA加密算法;byte data1 = inputStr1.getBytes();System.err.println(原文:n + inputStr1);/ 加密byte encodedData1 = RSACoder.encryptByPrivateKey(data1, privateKey);System
27、.err.println(加密后:n + Base64.encodeBase64String(encodedData1);byte decodedData1 = RSACoder.decryptByPublicKey(encodedData1,publicKey);String outputStr1 = new String(decodedData1);System.err.println(解密后:n + outputStr1);assertEquals(inputStr1, outputStr1);System.err.println(n-公钥加密私钥解密-);String inputStr
28、2 = RSA Encypt Algorithm;byte data2 = inputStr2.getBytes();System.err.println(原文:n + inputStr2);byte encodedData2 = RSACoder.encryptByPublicKey(data2, publicKey);System.err.println(加密后:n + Base64.encodeBase64String(encodedData2);byte decodedData2 = RSACoder.decryptByPrivateKey(encodedData2,privateKey);String outputStr2 = new String(decodedData2);System.err.println(解密后: + outputStr2);assertEquals(inputStr2, outputStr2);9、用公钥对“电子商务安全技术RSA”进行加密。10、用私钥对第九步中加密的信息进行解密。11、用生成的私钥对“电子商务安全技术RSA”进行加密。12、用公钥对11步中的信息进行解密。13、请把你的公钥发送给你旁边的同学,让该同学用公钥进行加密,然后再把加密后的信息发送给你,你再用你的私钥对信息进行解密。