版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、键入文字西安邮电大学电子商务安全技术实验一报告系 部 名 称: 经济与管理学院学 生 姓 名:韩振伟专 业 名 称:电子商务班 级:1101班学 号:02112003时 间:2014-5-10一、实验目的:通过JAVA语言,来实现对称密钥加密算法,非对称秘钥加密算法对信息的加密解密,通过实际操作加深学生对对称密钥加密、非对称秘钥加密解密的理解。二、实验内容:安装JDK,配置Java开发环境,加压eclipse,编写对称秘钥的生成、对称密钥加密、解密的程序。编写非对称秘钥加密解密的程序,用私钥对信息进行加密,用公钥对信息进行解密,然后用公钥对信息进行加密,用私钥对信息进行解密。三、实验用到的主要
2、技术及工具主要技术:Java、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 jav
3、ax.crypto.SecretKeyFactory;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 = n
4、ew DESKeySpec(key);SecretKeyFactory 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);
5、cipher.init(Cipher.DECRYPT_MODE, k);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
6、 Exception KeyGenerator kg = KeyGenerator.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 DE
7、SCoderTest Testpublic final void test() 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.encry
8、pt(inputData, key);System.err.println("加密后:t" + Base64.encodeBase64String(inputData);byte outputData = DESCoder.decrypt(inputData, key);String outputStr = new String(outputData);System.err.println("解密后:t" + outputStr);assertEquals(inputStr, outputStr);5、使用生成的秘钥对“电子商务安全技术”进行加密。6、用
9、第4步骤中生成的秘钥对第5部中生成的加密后的内容进行解密。7、使用AES算法重复4-6步骤。package org.zlex.chapter07_3;import java.security.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
10、 = "AES"public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"private static Key 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 = to
11、Key(key);Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, k);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);r
12、eturn cipher.doFinal(data);public static byte initKey() throws Exception KeyGenerator kg = KeyGenerator.getInstance(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.
13、Base64;import org.junit.Test;public class AESCoderTest Testpublic final void test() throws Exception String inputStr = "AES"byte inputData = inputStr.getBytes();System.err.println("原文:t" + inputStr);byte key = AESCoder.initKey();System.err.println("密钥:t" + Base64.encode
14、Base64String(key);inputData = AESCoder.encrypt(inputData, key);System.err.println("加密后:t" + Base64.encodeBase64String(inputData);byte outputData = AESCoder.decrypt(inputData, key);String outputStr = new String(outputData);System.err.println("解密后:t" + outputStr);assertEquals(input
15、Str, outputStr);8、使用RSA算法生成公钥和私钥。package org.zlex.chapter08_2;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import erfaces.RSAPrivateKey;impor
16、t erfaces.RSAPublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.Map;import javax.crypto.Cipher;public abstract class RSACoder public static final String KEY_ALGORITHM = "RSA"private
17、 static final String PUBLIC_KEY = "RSAPublicKey"private static final String PRIVATE_KEY = "RSAPrivateKey"private static final int KEY_SIZE = 512;public static byte decryptByPrivateKey(byte data, byte key)throws Exception PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(
18、key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);public static byte decryptByPublic
19、Key(byte data, byte key)throws Exception X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Ci
20、pher.DECRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte encryptByPublicKey(byte data, byte key)throws Exception X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PublicKey publicKey = keyFactory.generatePub
21、lic(x509KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);public static byte encryptByPrivateKey(byte data, byte key)throws Exception PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);KeyFactory
22、 keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE, privateKey);return cipher.doFinal(data);public static byte getPrivateKey(Map<String, O
23、bject> keyMap)throws Exception Key key = (Key) keyMap.get(PRIVATE_KEY);return key.getEncoded();public static byte getPublicKey(Map<String, Object> keyMap)throws Exception Key key = (Key) keyMap.get(PUBLIC_KEY);return key.getEncoded();public static Map<String, Object> initKey() throws
24、Exception KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(KEY_SIZE);KeyPair keyPair = keyPairGen.generateKeyPair();RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();Map<String
25、, Object> keyMap = new HashMap<String, Object>(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.
26、Map;public class RSACoderTest private byte publicKey;private byte privateKey;Beforepublic void initKey() throws Exception Map<String, Object> keyMap = RSACoder.initKey();publicKey = RSACoder.getPublicKey(keyMap);privateKey = RSACoder.getPrivateKey(keyMap);System.err.println("公钥: n" +
27、 Base64.encodeBase64String(publicKey);System.err.println("私钥: n" + Base64.encodeBase64String(privateKey);Testpublic void test() throws Exception System.err.println("n-私钥加密公钥解密-");String inputStr1 = "RSA加密算法"byte data1 = inputStr1.getBytes();System.err.println("原文:n
28、" + inputStr1);/ 加密byte encodedData1 = RSACoder.encryptByPrivateKey(data1, privateKey);System.err.println("加密后:n" + Base64.encodeBase64String(encodedData1);byte decodedData1 = RSACoder.decryptByPublicKey(encodedData1,publicKey);String outputStr1 = new String(decodedData1);System.err.p
29、rintln("解密后:n" + outputStr1);assertEquals(inputStr1, outputStr1);System.err.println("n-公钥加密私钥解密-");String inputStr2 = "RSA Encypt Algorithm"byte data2 = inputStr2.getBytes();System.err.println("原文:n" + inputStr2);byte encodedData2 = RSACoder.encryptByPublicKey(data2, publicKey
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度车场租赁及停车场绿化美化服务协议4篇
- 科技引领下的宇宙探索进展
- 二零二五年度车辆融资租赁合同违约责任答辩状样本8篇
- 二零二五年度车辆买卖合同含车辆绿色环保认证3篇
- 二零二五年度草坪围栏施工与城市排水系统配套合同2篇
- 2025年度个人知识产权代理佣金协议4篇
- 二零二五年度橱柜衣柜模块化设计生产合同4篇
- 2025年度个人车位买卖合同范本(写字楼)3篇
- 高效体育训练学生体能提升的秘密武器
- 2025年度绿色有机牛奶产销一体化合作合同范本4篇
- 河北省邯郸市永年区2024-2025学年九年级上学期期末考试化学试卷(含答案)
- 交通运输行政执法程序规定培训课件
- 消防员证考试题库2000题中级
- 海洋垃圾处理行业可行性分析报告
- 公共部门绩效管理案例分析
- 无人机培训计划表
- 2024届高考英语词汇3500左右
- 2024年-2025年海船船员考试-船舶人员管理考试题及答案
- 2025届安徽省皖南八校联盟高二物理第一学期期末统考试题含解析
- 三兄弟分田地宅基地协议书范文
- 《BIM土建算量与云计价》完整课件
评论
0/150
提交评论