表密码算法c语言实验报告,大学密码学实验报告(含代码) 试验1 古典密码算法.doc...

西华大学计算机系上机实践报告

课程:级::名称:上机实践日期:2009-11-10上机实践编号:组:

目的

内容与

根据对置换密码算法的介绍,自己创建明文信息,并选择一个密钥,编写替代密码算法的实现程序,实现加密和解密操作。

根据上述密码算法最后的实现程序提供加密和解密两个接口:int encrypt( ) 和int decrypt(). 当加密和解密成功时返回CRYPT_OK, 失败时返回CRYPT_ERROR.。

三、用环境

Windows 或Linux 操作系统的PC机,具有GCC(Linux)、VC(Windows) 等C语言编译环境。

四#include "mycrypt.h"

#ifdef YARROW

const struct _prng_descriptor yarrow_desc =

{

"yarrow",

&yarrow_start,

&yarrow_add_entropy,

&yarrow_ready,

&yarrow_read

};

int yarrow_start(prng_state *prng)

{

int err;

_ARGCHK(prng != NULL);

/* these are the default hash/cipher combo used */

#ifdef RIJNDAEL

#if YARROW_AES==0

prng->yarrow.cipher = register_cipher(&rijndael_enc_desc);

#elif YARROW_AES==1

prng->yarrow.cipher = register_cipher(&aes_enc_desc);

#elif YARROW_AES==2

prng->yarrow.cipher = register_cipher(&rijndael_desc);

#elif YARROW_AES==3

prng->yarrow.cipher = register_cipher(&aes_desc);

#endif

#elif defined(BLOWFISH)

prng->yarrow.cipher = register_cipher(&blowfish_desc);

#elif defined(TWOFISH)

prng->yarrow.cipher = register_cipher(&twofish_desc);

#elif defined(RC6)

prng->yarrow.cipher = register_cipher(&rc6_desc);

#elif defined(RC5)

prng->yarrow.cipher = register_cipher(&rc5_desc);

#elif defined(SAFERP)

prng->yarrow.cipher = register_cipher(&saferp_desc);

#elif defined(RC2)

prng->yarrow.cipher = register_cipher(&rc2_desc);

#elif defined(NOEKEON)

prng->yarrow.cipher = register_cipher(&noekeon_desc);

#elif defined(CAST5)

prng->yarrow.cipher = register_cipher(&cast5_desc);

#elif defined(XTEA)

prng->yarrow.cipher = register_cipher(&xtea_desc);

#elif defined(SAFER)

prng->yarrow.cipher = register_cipher(&safer_sk128_desc);

#elif defined(DES)

prng->yarrow.cipher = register_cipher(&des3_desc);

#elif

#error YARROW needs at least one CIPHER

#endif

if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {

return err;

}

#ifdef SHA256

prng->yarrow.hash = register_hash(&sha256_desc);

#elif defined(

1.古典密码算法之 替代算法 import java.io.*; public class Caesar { //向右加密方法 ming为明文 mi 为密文 yao为密钥 public static void encode(String ming, int yao) { String mi = ""; for(int i=0; i<ming.length(); i++){ if (ming.charAt(i) == 32) mi += (char)(32); else if (ming.charAt(i) >= 'a' && ming.charAt(i) <= 'z') mi += (char)('a' + ((ming.charAt(i) - 'a' + yao) % 26)); else if (ming.charAt(i) >= 'A' && ming.charAt(i) <= 'Z') mi += (char)('A' + ((ming.charAt(i) - 'A' + yao) % 26)); else if (ming.charAt(i) >= '0' && ming.charAt(i) <= '9') mi += (char)('0' + ((ming.charAt(i) - '0' + yao) % 10)); } System.out.println("密文为: " + mi); } /* * 2.古典密码算法之 置换算法 * */ public class Change { //当加密或者解密成功时返回CRYPT_OK,失败时返回CRYPT_ERROR private final static int CRYPT_OK = 1; private final static int CRYPT_ERROR = 0; /* * @param initCode 没有加密前的字符串 * @CRYPT_OK 加密成功 * @CRYPT_ERROR 加密失败 */ public static int encrypt(String initCode) throws Exception{ //用来输出加密后的字符 StringBuilder sb = new StringBuilder(); //获取initCode的字符串长度 int codeLength = initCode.length(); //根据这个codeLength来确定要几行6列 int rows = (int)Math.ceil(codeLength/6.0 ); //构造一个rows行6列的数组 char[][] initChar = new char[rows][6]; //存放initCode中的所有字符,包括空格 for(int i = 0; i < initChar.length; i++ ) for(int j=0; j < initChar[0].length; j++){ //将对应字符放入这个数组中 try{ initChar[i][j] = initCode.charAt( (i ) * initChar[0].length + j ); }catch(Exception e){
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值