电子商务安全技术实验.docx

上传人:b****5 文档编号:11538891 上传时间:2023-03-19 格式:DOCX 页数:14 大小:379.72KB
下载 相关 举报
电子商务安全技术实验.docx_第1页
第1页 / 共14页
电子商务安全技术实验.docx_第2页
第2页 / 共14页
电子商务安全技术实验.docx_第3页
第3页 / 共14页
电子商务安全技术实验.docx_第4页
第4页 / 共14页
电子商务安全技术实验.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

电子商务安全技术实验.docx

《电子商务安全技术实验.docx》由会员分享,可在线阅读,更多相关《电子商务安全技术实验.docx(14页珍藏版)》请在冰豆网上搜索。

电子商务安全技术实验.docx

电子商务安全技术实验

西安邮电大学

电子商务安全技术实验一报告

系部名称:

经济与管理学院

学生姓名:

韩振伟

专业名称:

电子商务

班级:

1101班

学号:

时间:

2014-5-10

一、实验目的:

通过JAVA语言,来实现对称密钥加密算法,非对称秘钥加密算法对信息的加密解密,通过实际操作加深学生对对称密钥加密、非对称秘钥加密解密的理解。

二、实验内容:

安装JDK,配置Java开发环境,加压eclipse,编写对称秘钥的生成、对称密钥加密、解密的程序。

编写非对称秘钥加密解密的程序,用私钥对信息进行加密,用公钥对信息进行解密,然后用公钥对信息进行加密,用私钥对信息进行解密。

三、实验用到的主要技术及工具

主要技术:

Java、BouncyCastle

主要工具:

Eclipse

四、开发步骤:

1、安装JDK,配置JAVA环境变量。

2、解压eclipse。

3、在eclipse中新建项目

4、编写使用DES算法生成秘钥的程序。

packageorg.zlex.chapter07_1;

importjava.security.Key;

importjava.security.SecureRandom;

importjavax.crypto.Cipher;

importjavax.crypto.KeyGenerator;

importjavax.crypto.SecretKey;

importjavax.crypto.SecretKeyFactory;

importjavax.crypto.spec.DESKeySpec;

publicabstractclassDESCoder{

publicstaticfinalStringKEY_ALGORITHM="DES";

publicstaticfinalStringCIPHER_ALGORITHM="DES/ECB/PKCS5PADDING";

privatestaticKeytoKey(byte[]key)throwsException{

DESKeySpecdks=newDESKeySpec(key);

SecretKeyFactorykeyFactory=SecretKeyFactory

.getInstance(KEY_ALGORITHM);

SecretKeysecretKey=keyFactory.generateSecret(dks);

returnsecretKey;

}

publicstaticbyte[]decrypt(byte[]data,byte[]key)throwsException{

Keyk=toKey(key);

Ciphercipher=Cipher.getInstance(CIPHER_ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE,k);

returncipher.doFinal(data);

}

publicstaticbyte[]encrypt(byte[]data,byte[]key)throwsException{

Keyk=toKey(key);

Ciphercipher=Cipher.getInstance(CIPHER_ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE,k);

returncipher.doFinal(data);

}

publicstaticbyte[]initKey()throwsException{

KeyGeneratorkg=KeyGenerator.getInstance(KEY_ALGORITHM);

kg.init(56,newSecureRandom());

SecretKeysecretKey=kg.generateKey();

returnsecretKey.getEncoded();

}

}

packageorg.zlex.chapter07_1;

importstaticorg.junit.Assert.*;

importmons.codec.binary.Base64;

importorg.junit.Test;

publicclassDESCoderTest{

@Test

publicfinalvoidtest()throwsException{

StringinputStr="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.encodeBase64String(inputData));

byte[]outputData=DESCoder.decrypt(inputData,key);

StringoutputStr=newString(outputData);

System.err.println("解密后:

\t"+outputStr);

assertEquals(inputStr,outputStr);

}

}

5、使用生成的秘钥对“电子商务安全技术”进行加密。

6、用第4步骤中生成的秘钥对第5部中生成的加密后的内容进行解密。

7、使用AES算法重复4-6步骤。

packageorg.zlex.chapter07_3;

importjava.security.Key;

importjavax.crypto.Cipher;

importjavax.crypto.KeyGenerator;

importjavax.crypto.SecretKey;

importjavax.crypto.spec.SecretKeySpec;

publicabstractclassAESCoder{

publicstaticfinalStringKEY_ALGORITHM="AES";

publicstaticfinalStringCIPHER_ALGORITHM="AES/ECB/PKCS5Padding";

privatestaticKeytoKey(byte[]key)throwsException{

SecretKeysecretKey=newSecretKeySpec(key,KEY_ALGORITHM);

returnsecretKey;

}

publicstaticbyte[]decrypt(byte[]data,byte[]key)throwsException{

Keyk=toKey(key);

Ciphercipher=Cipher.getInstance(CIPHER_ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE,k);

returncipher.doFinal(data);

}

publicstaticbyte[]encrypt(byte[]data,byte[]key)throwsException{

Keyk=toKey(key);

Ciphercipher=Cipher.getInstance(CIPHER_ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE,k);

returncipher.doFinal(data);

}

publicstaticbyte[]initKey()throwsException{

KeyGeneratorkg=KeyGenerator.getInstance(KEY_ALGORITHM);

kg.init(128);

SecretKeysecretKey=kg.generateKey();

returnsecretKey.getEncoded();

}

}

packageorg.zlex.chapter07_3;

importstaticorg.junit.Assert.*;

importmons.codec.binary.Base64;

importorg.junit.Test;

publicclassAESCoderTest{

@Test

publicfinalvoidtest()throwsException{

StringinputStr="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=AESCoder.decrypt(inputData,key);

StringoutputStr=newString(outputData);

System.err.println("解密后:

\t"+outputStr);

assertEquals(inputStr,outputStr);

}

}

8、使用RSA算法生成公钥和私钥。

packageorg.zlex.chapter08_2;

importjava.security.Key;

importjava.security.KeyFactory;

importjava.security.KeyPair;

importjava.security.KeyPairGenerator;

importjava.security.PrivateKey;

importjava.security.PublicKey;

importjava.security.interfaces.RSAPrivateKey;

importjava.security.interfaces.RSAPublicKey;

importjava.security.spec.PKCS8EncodedKeySpec;

importjava.security.spec.X509EncodedKeySpec;

importjava.util.HashMap;

importjava.util.Map;

importjavax.crypto.Cipher;

publicabstractclassRSACoder{

publicstaticfinalStringKEY_ALGORITHM="RSA";

privatestaticfinalStringPUBLIC_KEY="RSAPublicKey";

privatestaticfinalStringPRIVATE_KEY="RSAPrivateKey";

privatestaticfinalintKEY_SIZE=512;

publicstaticbyte[]decryptByPrivateKey(byte[]data,byte[]key)

throwsException{

PKCS8EncodedKeySpecpkcs8KeySpec=newPKCS8EncodedKeySpec(key);

KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

PrivateKeyprivateKey=keyFactory.generatePrivate(pkcs8KeySpec);

Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE,privateKey);

returncipher.doFinal(data);

}

publicstaticbyte[]decryptByPublicKey(byte[]data,byte[]key)

throwsException{

X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(key);

KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

PublicKeypublicKey=keyFactory.generatePublic(x509KeySpec);

Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE,publicKey);

returncipher.doFinal(data);

}

publicstaticbyte[]encryptByPublicKey(byte[]data,byte[]key)

throwsException{

X509EncodedKeySpecx509KeySpec=newX509EncodedKeySpec(key);

KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

PublicKeypublicKey=keyFactory.generatePublic(x509KeySpec);

Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE,publicKey);

returncipher.doFinal(data);

}

publicstaticbyte[]encryptByPrivateKey(byte[]data,byte[]key)

throwsException{

PKCS8EncodedKeySpecpkcs8KeySpec=newPKCS8EncodedKeySpec(key);

KeyFactorykeyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

PrivateKeyprivateKey=keyFactory.generatePrivate(pkcs8KeySpec);

Ciphercipher=Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE,privateKey);

returncipher.doFinal(data);

}

publicstaticbyte[]getPrivateKey(MapkeyMap)

throwsException{

Keykey=(Key)keyMap.get(PRIVATE_KEY);

returnkey.getEncoded();

}

publicstaticbyte[]getPublicKey(MapkeyMap)

throwsException{

Keykey=(Key)keyMap.get(PUBLIC_KEY);

returnkey.getEncoded();

}

publicstaticMapinitKey()throwsException{

KeyPairGeneratorkeyPairGen=KeyPairGenerator

.getInstance(KEY_ALGORITHM);keyPairGen.initialize(KEY_SIZE);

KeyPairkeyPair=keyPairGen.generateKeyPair();

RSAPublicKeypublicKey=(RSAPublicKey)keyPair.getPublic();

RSAPrivateKeyprivateKey=(RSAPrivateKey)keyPair.getPrivate();

MapkeyMap=newHashMap

(2);

keyMap.put(PUBLIC_KEY,publicKey);

keyMap.put(PRIVATE_KEY,privateKey);

returnkeyMap;

}

}

packageorg.zlex.chapter08_2;

importstaticorg.junit.Assert.*;

importmons.codec.binary.Base64;

importorg.junit.Before;

importorg.junit.Test;

importjava.util.Map;

publicclassRSACoderTest{

privatebyte[]publicKey;

privatebyte[]privateKey;

@Before

publicvoidinitKey()throwsException{

MapkeyMap=RSACoder.initKey();

publicKey=RSACoder.getPublicKey(keyMap);

privateKey=RSACoder.getPrivateKey(keyMap);

System.err.println("公钥:

\n"+Base64.encodeBase64String(publicKey));

System.err.println("私钥:

\n"+Base64.encodeBase64String(privateKey));

}

@Test

publicvoidtest()throwsException{

System.err.println("\n---私钥加密——公钥解密---");

StringinputStr1="RSA加密算法";

byte[]data1=inputStr1.getBytes();

System.err.println("原文:

\n"+inputStr1);

//加密

byte[]encodedData1=RSACoder.encryptByPrivateKey(data1,privateKey);

System.err.println("加密后:

\n"+Base64.encodeBase64String(encodedData1));

byte[]decodedData1=RSACoder.decryptByPublicKey(encodedData1,

publicKey);

StringoutputStr1=newString(decodedData1);

System.err.println("解密后:

\n"+outputStr1);

assertEquals(inputStr1,outputStr1);

System.err.println("\n---公钥加密——私钥解密---");

StringinputStr2="RSAEncyptAlgorithm";

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);

StringoutputStr2=newString(decodedData2);

System.err.println("解密后:

"+outputStr2);

assertEquals(inputStr2,outputStr2);

}

}

9、用公钥对“电子商务安全技术RSA”进行加密。

10、用私钥对第九步中加密的信息进行解密。

11、用生成的私钥对“电子商务安全技术RSA”进行加密。

12、用公钥对11步中的信息进行解密。

13、请把你的公钥发送给你旁边的同学,让该同学用公钥进行加密,然后再把加密后的信息发送给你,你再用你的私钥对信息进行解密。

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1