【实验】设计一个加密算法(英文、中文)

本文通过两个实验详细介绍了如何设计一个加密算法,分别针对英文和中文。实验一采用ASCII编码,对英文字符进行基于字符的分组加密;实验二则利用UTF-8编码,进行基于字节的分组加密。加密和解密过程均使用JAVA实现,且测试了不同密钥的效果。在实验中,作者发现只有特定密钥(如0、1)才能完整解密所有字符,同时指出在处理UTF-8编码时需要注意的问题。
摘要由CSDN通过智能技术生成

实验一、英文加解密

设计思路:

现有的ASCII编码方式可以表示全部的英文字符和常用的标点符号,于是选用ASCII编码,可见字符的范围是32~126。
基于字符的分组加密:
输入一串数字作为密钥K,循环使用该数字序列对明文进行加密。
对于每一个可见字符Pi,将其ASCII码的值增加Ki得到Ci,若大于126,则将其数值对126取模再加31,使得最终得到的Ci仍然是可见字符,所有的Ci按顺序输出便得到了密文。
对于不可见字符和ASCII码表示范围之外的字符,不作处理,直接作为密文。
解密:
输入加密时的数字序列作为密钥K。
对于每一个可见字符Ci,将其ASCII码的值减去Ki得到Pi,若小于32,则将其数值减去31再加上126,使得最终得到的Pi仍然是可见字符,所有的Pi按顺序输出便得到了明文。
对于不可见字符和ASCII码表示范围之外的字符,不作处理,直接作为明文。

程序流程:


语言:java
基于字符的分组加密:
1.提示输入密钥K,并存储在ArrayList中;
2.使用BufferedInputStream按字节从桌面文件plainText.txt中读入明文(文件使用默认的ANSI编码格式创建并保存),若读取失败抛出异常;
3.将明文按照设计的算法转换为密文;
4.使用BufferedOutputStream按字节将密文输出到桌面文件cipherText.txt,若写入失败抛出异常;
5.循环执行2~4,直到明文读取完毕;
6.将BufferedInputStream和BufferedOutputStream关闭,若关闭失败抛出异常;
解密:
1.提示输入密钥K,并存储在ArrayList中;
2.使用BufferedInputStream按字节从桌面文件cipherText.txt中读入密文,若读取失败抛出异常;
3.将密文按照设计的算法转换为明文;
4.使用BufferedOutputStream按字节将明文输出到桌面文件decryptedText.txt,若写入失败抛出异常;
5.循环执行2~4,直到明文读取完毕;
6.将BufferedInputStream和BufferedOutputStream关闭,若关闭失败抛出异常;

3、测试结果:

所有的ASCII可见字符和汉字等特殊字符——密钥为:22
这里写图片描述

4、源代码:

【加密】

package cryptography;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class EncryptionMainOfEnglish {
   

	//此处为明文文件路径、密文文件路径
	private static final String inFilePath = "C:\\Users\\dell\\Desktop\\plainText.txt";
	private static final String outFilePath = "C:\\Users\\dell\\Desktop\\cipherText.txt";
	private static BufferedInputStream bufferedInput = null;
	private static BufferedOutputStream bufferedOutput = null;
	
	//密钥储存list
	private static ArrayList<Integer> keyList = new ArrayList<Integer>();

	public static void main(String[] args) throws IOException {
   
		// TODO Auto-generated method stub
		EncryptionMainOfEnglish mm = new EncryptionMainOfEnglish();
		String clearString="",cipherString="";
		int bytesRead = 0;
		byte[] bufferIn = new byte[1024];
		Scanner scanner = new Scanner(System.in);
		System.out.print("[encrypted English] Please enter the key sequence(only number):");
		String keyString = scanner.next();
		scanner.close();
		for( int i=0;i<keyString.length();i++ ){
   
			keyList.add( keyString.charAt(i)-'0' );
		}
		
		try {
   
			bufferedInput = new BufferedInputStream( new FileInputStream( inFilePath ) );
			bufferedOutput = new BufferedOutputStream( new FileOutputStream( outFilePath ) );
			
            //从文件中按字节读取内容,到文件尾部时read方法将返回-1
            while ( (bytesRead = bufferedInput.read(bufferIn) ) != -1) {
   
                //将读取的字节转为字符串对象
            	clearString = new String(bufferIn, 0, bytesRead);
                System.out.println( "[plainText:]"+clearString );
                
                cipherString = mm.getCipherString( clearString );
                
                System.out.println( "[cipherText:]"+cipherString );
                bufferedOutput.write( cipherString.getBytes() );
            }
		}catch(Exception e){
   
			System.err.println("文件读取出现异常!");
			e.printStackTrace();
		}finally {
   
			try {
   
				bufferedInput.close();
				bufferedOutput.close();
			} catch (Exception e2) {
   
				// TODO: handle exception
				System.err.println("缓冲区关闭异常!");
				e2.printStackTrace();
			}
		}
	}
	
	public String getCipherString( String clearString 
  • 11
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值