【2017年整理】对称加密算法

上传人:豆浆 文档编号:1010074 上传时间:2017-05-25 格式:DOC 页数:9 大小:301.50KB
返回 下载 相关 举报
【2017年整理】对称加密算法_第1页
第1页 / 共9页
【2017年整理】对称加密算法_第2页
第2页 / 共9页
【2017年整理】对称加密算法_第3页
第3页 / 共9页
【2017年整理】对称加密算法_第4页
第4页 / 共9页
【2017年整理】对称加密算法_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《【2017年整理】对称加密算法》由会员分享,可在线阅读,更多相关《【2017年整理】对称加密算法(9页珍藏版)》请在金锄头文库上搜索。

1、 实验一:对称加密算法实验对称算法:实验目的:(1) 了解对称算法的基本工作流程。(2) 掌握对称算法的使用方法。硬件环境: Intel Core i3 CPU 2.67GHz4GRAM软件环境:VS2010PerlopenssL Myeclipse 10.0实验步骤:(1) 认识 OpenSSL 工具包。(2) 用简短的程序代码演示:分组加密算法(DES、AES)和流密码算法(RC4)的使用,其中包括分组算法的四种应用模式ECB、CBCCFB、OFB。1.获得 OpenSSL到 OpenSSL 的网站即可下载当前版本的 OpenSSL 源代码压缩包。最新版本为 openssl-1.0.1e.

2、tar.gz2. 编译工具编译 OpenSSL 需要 Perl 和 C 编译器。Perl 在 Windows 下使用 Active Perl。在 Windows 下可以使用 Visual C+ 编译器。3. 编译和安装步骤在 Windows 中在所有程序-visual studio 2010-visual studio tools-visual studio 命令提示:cd d:opensslperl Configure VC-WIN32msdo_msnmake -f msntdll.makcd out32dll.mstest编译结果得到头文件、链接库、运行库和 openssl.exe 工具。

3、头文件位于./inc32 或者./inculde 目录,有一个 openssl 子目录,内有几十个.h 文件。链接库即./out32dll 目录中的 libeay32.lib 和ssleay32.lib,分别是密码算法相关的和 ssl 协议相关的。运行库是./out32dll 目录中的 libeay32.dll 和 ssleay32.dll,和链接库相对应。在./out32dll 中还有一个工具 openssl.exe,可以直接用来测试性能、产生 RSA 密钥、加解密文件,甚至可以用来维护一个测试用的 CA。下图是利用 openssL speed 测试各种加密算法的速度下图实现的是 DEC 中

4、 ECB 和 BFB 的算法实验:下图是 AES 的算法是:RC4 的结果为:(3)编写一个简单但是安全的文件加密程序。DES:package test1;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;public class DesEncrypt Key key;/*根据参数生成KEY*/public void getKey(String strKey) try KeyGenerator _genera

5、tor = KeyGenerator.getInstance(DES);_generator.init(new SecureRandom(strKey.getBytes();this.key = _generator.generateKey();_generator = null; catch (Exception e) e.printStackTrace();/*加密String 明文输入,String 密文输出*/public String getEncString(String strMing) byte byteMi = null;byte byteMing = null;String

6、 strMi = ;try return byte2hex(getEncCode(strMing.getBytes(); catch (Exception e) e.printStackTrace(); 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 St

7、ring(getDesCode(hex2byte(strMi.getBytes(); catch (Exception e) e.printStackTrace(); finally byteMing = null;byteMi = null;return strMing;/*加密以byte 明文输入,byte 密文输出*/private byte getEncCode(byte byteS) byte byteFina = null;Cipher cipher;try cipher = Cipher.getInstance(DES);cipher.init(Cipher.ENCRYPT_MO

8、DE, key);byteFina = cipher.doFinal(byteS); catch (Exception e) e.printStackTrace(); finally cipher = null;return byteFina;/* 解密以byte 密文输入,以byte明文输出*/private byte getDesCode(byte byteD) Cipher cipher;byte byteFina = null;try cipher = Cipher.getInstance(DES);cipher.init(Cipher.DECRYPT_MODE, key);byteF

9、ina = cipher.doFinal(byteD); catch (Exception e) e.printStackTrace(); finally cipher = null;return byteFina;/* 二行制转字符串*/public static String byte2hex(byte b) / 一个字节的数,/ 转成16进制字符串String hs = ;String stmp = ;for (int n = 0; n b.length; n+) stmp = (java.lang.Integer.toHexString(bn & 0XFF);if (stmp.leng

10、th() = 1)hs = hs + 0 + stmp;elsehs = hs + stmp;return hs.toUpperCase(); / 转成大写public static byte hex2byte(byte b) if (b.length % 2) != 0)throw new IllegalArgumentException(长度不是偶数);byte b2 = new byteb.length / 2;for (int n = 0; n b.length; n += 2) String item = new String(b, n, 2);b2n / 2 = (byte) In

11、teger.parseInt(item, 16);return b2;public static void main(String args) System.out.println(DES);System.out.println(我的学号201100300001);DesEncrypt des = new DesEncrypt(); / 实例化一个对像des.getKey(aadd); / 生成密匙long L = System.currentTimeMillis();String strEnc = des.getEncString(我的学号201100300001 ); / 加密字符串,返回

12、 String的密文long lUseTime = System.currentTimeMillis() - L;System.out.println(加密耗时: + lUseTime + 毫秒);System.out.println(strEnc);L = System.currentTimeMillis();String strDes = des.getDesString(strEnc); / 把String 类型的密文解密System.out.println(解密耗时: + lUseTime + 毫秒);System.out.println(strDes);AES:package tes

13、t1;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/* AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES 加密后,在进行 Base64 编码转化;*/public class AESOperator /* 加密用的 Key 可以用 26 个字母和数字组成 此处使用 AES-128-C

14、BC 加密模式,key 需要为 16 位。*/private String sKey = 0123456789abcdef;private String ivParameter = 0123456789abcdef;private static AESOperator instance = null;private AESOperator() public static AESOperator getInstance() if (instance = null)instance = new AESOperator();return instance;/ 加密public String encr

15、ypt(String sSrc) throws Exception Cipher cipher = Cipher.getInstance(AES/CBC/PKCS5Padding);byte raw = sKey.getBytes();SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes();/ 使用 CBC 模式,需要一个向量iv,可增加加密算法的强度cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);byte encrypted = cipher.doFinal(sSrc.getBytes(utf-8);return new BASE64Encoder().encode(encrypted);/ 此处使用BASE64 做转码。/ 解密public String decrypt(String sSrc) throws Exception try byte raw = sKey.getBytes(ASCII);SecretKeySpec skeySpec = n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号