2023年12月18日发(作者:)

实验一:对称加密算法实验

对称算法:

实验目的:

(1) 了解对称算法的基本工作流程。

(2) 掌握对称算法的使用方法。

硬件环境:

Intel Core i3 CPU 2.67GHz

4GRAM

软件环境:

VS2010

Perl

openssL

Myeclipse 10.0

实验步骤:

(1) 认识OpenSSL工具包。

(2) 用简短的程序代码演示:分组加密算法(DES、AES)和流密码算法(RC4)的使用,其中包括分组算法的四种应用模式ECB、CBCCFB、OFB。

1.获得OpenSSL

到OpenSSL的网站即可下载当前版本的OpenSSL源代码压缩包。

最新版本为

2. 编译工具

编译OpenSSL需要Perl和C编译器。

Perl在Windows下使用Active Perl。

在Windows下可以使用Visual C++ 编译器。

3. 编译和安装步骤

在Windows中在所有程序->visual studio 2010->visual studio tools->visual studio命令提示:

>cd d:openssl

>perl Configure VC-WIN32

>>msdo_ms

>nmake -f

>cd out32dll

>..mstest

编译结果得到头文件、链接库、运行库和工具。头文件位于./inc32或者./inculde目录,有一个openssl子目录,内有几十个.h文件。链接库即./out32dll目录中的 和,分别是密码算法相关的和ssl协议相关的。运行库是./out32dll目录中的 和,和链接库相对应。在./out32dll中还有一个工具,可以直接用来测试性能、产生RSA密钥、加解密文件,甚至可以用来维护一个测试用的CA。

下图是利用openssL speed测试各种加密算法的速度

下图实现的是DEC中ECB和BFB的算法实验:

下图是AES的算法是:

RC4的结果为:

(3)编写一个简单但是安全的文件加密程序。

DES:

package test1;

import ;

import Random;

import ;

import erator;

public class DesEncrypt {

Key key;

/*

*根据参数生成KEY

*/

public void getKey(String strKey) {

try {

KeyGenerator _generator = tance("DES");

_(new SecureRandom(es()));

= _teKey();

_generator = null;

} catch (Exception e) {

tackTrace();

}

}

/*

*加密String明文输入,String密文输出

*/

public String getEncString(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

try {

return byte2hex(getEncCode(es()));

} catch (Exception e) {

tackTrace();

} finally {

byteMing = null;

byteMi = null;

}

return strMi;

}

/*

*解密 以String密文输入,String明文输出

*/

public String getDesString(String strMi) {

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

return new String(getDesCode(hex2byte(es())));

} catch (Exception e) {

tackTrace();

} finally {

byteMing = null;

byteMi = null;

}

return strMing;

}

/**

*加密以byte[]明文输入,byte[]密文输出

*/

private byte[] getEncCode(byte[] byteS) {

byte[] byteFina = null;

Cipher cipher;

try {

cipher = tance("DES");

(T_MODE, key);

byteFina = l(byteS);

} catch (Exception e) {

tackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/**

* 解密以byte[]密文输入,以byte[]明文输出

*/

private byte[] getDesCode(byte[] byteD) {

Cipher cipher;

byte[] byteFina = null;

try {

cipher = tance("DES");

(T_MODE, key);

byteFina = l(byteD);

} catch (Exception e) {

tackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/**

* 二行制转字符串

*/

public static String byte2hex(byte[] b) { // 一个字节的数,

// 转成16进制字符串

String hs = "";

String stmp = "";

for (int n = 0; n < ; n++) {

stmp = (tring(b[n] & 0XFF));

if (() == 1)

hs = hs + "0" + stmp;

else

hs = hs + stmp;

}

return rCase(); // 转成大写

}

public static byte[] hex2byte(byte[] b) {

if (( % 2) != 0)

throw new IllegalArgumentException("长度不是偶数");

byte[] b2 = new byte[ / 2];

for (int n = 0; n < ; n += 2) {

String item = new String(b, n, 2);

b2[n / 2] = (byte) nt(item, 16);

}

return b2;

}

public static void main(String[] args) {

n("DES");

n("我的学号2");

DesEncrypt des = new DesEncrypt(); // 实例化一个对像

("aadd"); // 生成密匙

long L = tTimeMillis();

String strEnc = String("我的学号2"); // 加密字符串,返回String的密文

long lUseTime = tTimeMillis() - L;

n("加密耗时:" + lUseTime + "毫秒");

n(strEnc);

L = tTimeMillis();

String strDes = String(strEnc); // 把String 类型的密文解密

n("解密耗时:" + lUseTime + "毫秒");

n(strDes);

}

}

AES:

package test1;

import ;

import meterSpec;

import KeySpec;

import 64Decoder;

import 64Encoder;

/**

* AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化;

*/

public class AESOperator {

/*

* 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密 */

private String sKey = "abcdef";

private String ivParameter = "abcdef";

private static AESOperator instance = null;

private AESOperator() {

}

public static AESOperator getInstance() {

}

// 加密

public String encrypt(String sSrc) throws Exception {

Cipher cipher = tance("AES/CBC/PKCS5Padding");

byte[] raw = es();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

IvParameterSpec iv = new

if (instance == null)

instance = new AESOperator();

return instance;

模式,key需要为16位。

IvParameterSpec(es());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度

public static void main(String[] args) throws Exception {

// 需要加密的字串

String cSrc = "我的学号2";

n("AES");

n(cSrc);

// 加密

long L = tTimeMillis();

String enString = tance().encrypt(cSrc);

// 解密

public String decrypt(String sSrc) throws Exception {

}

try {

}

byte[] raw = es("ASCII");

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = tance("AES/CBC/PKCS5Padding");

IvParameterSpec iv = new

}

(T_MODE, skeySpec, iv);

byte[] encrypted = l(es("utf-8"));

return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。

IvParameterSpec(es());

(T_MODE, skeySpec, iv);

byte[] encrypted1 = new

BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密

byte[] original = l(encrypted1);

String originalString = new String(original, "utf-8");

return originalString;

return null;

} catch (Exception ex) {

}

}

n("加密后的字串是:" + enString);

long lUseTime = tTimeMillis() - L;

n("加密耗时:" + lUseTime + "毫秒");

// 解密

L = tTimeMillis();

String DeString = tance().decrypt(enString);

n("解密耗时:" + lUseTime + "毫秒");

n("解密后的字串是:" + DeString);

lUseTime = tTimeMillis() - L;