密码学基础(对称加密和非对称加密)

本文介绍了对称加密和非对称加密两种加密方式,对称加密如DES、AES,效率高但安全性较低;非对称加密如RSA、ECC,安全性好但效率低。在实际应用中,常通过非对称加密传输对称密钥,结合两者的优点。此外,还展示了凯撒密码的对称加密实现,包括加密和解密的Java代码,并给出了文件加密的示例。
摘要由CSDN通过智能技术生成

分类

一般分为:

  • 对称加密
  • 非对称加密

对称加密

加密和解密使用的是同一个密钥或者,两者可以互相推导得出,则认为是对称加密,如DES,AES,3DES

在传输数据时用密钥将数据加密,然后将密文发给接收方,接收方再使用该密钥解密数据。这样就要求接收方需要知道密钥,如果接收方需要接受1万个用户的数据的话就需要知道1万个密钥,并且密钥容易泄漏。

非对称加密

加密和解密的密钥不同,且知道其中一个密钥不能得知另一个密钥,一般用来加密的密钥称作公钥,用来解密的密钥称作私钥,如RSA,ECC

在需要传输数据给某用户时,就使用该用户的公钥对数据加密得到密文发给用户,用户用自己的私钥来解密密文得到数据 。

两类加密的特点

  • 对称加密安全性差,但是效率高
  • 非对称加密安全性好,但是加密效率低

结合使用

在传输一个比较大的数据时,可以先用对称加密来加密数据,对称加密的密钥假如为X,再用非对称加密将X加密。

举例

凯撒密码

凯撒密码是对称密码,其密钥是0-26的一个整数key,加密是将每个字母往后推移key个字母
比如密钥为3,明文为a,则密文就是d

代码实现-加密

//加密
    public static String encryption(String txt,int key){
        if (key<0||key>26)
            return null;
        char[] result=txt.toCharArray();
        int intA=65;
        int inta=97;
        for (int i=0;i<result.length;i++){
            if (result[i]>'A'&&result[i]<'Z') {
                result[i] =(char)(((int)result[i]+key-intA)%26+intA);
            }else if (result[i]>'a'&&result[i]<'z'){
                result[i] =(char)(((int)result[i]+key-inta)%26+inta);
            }else;
        }
        return new String(result);
    }

代码实现-解密

//解密,调用加密的函数
    public static String decrypting(String txt,int key){
        if (key<0||key>26)
            return null;
        return encryption(txt,26-key);
    }

main函数及文件加密

public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int choose=0;
        System.out.println("加密输入1,解密输入2:");
        choose=scanner.nextInt();
        if (choose==1){
            System.out.println("输入密钥(0-26):");
            int key=scanner.nextInt();
            String pathname="/new/IdeaProjects/StudyTest/test.txt";
            String oriText=getFileText(pathname);
            String ciphertext=encryption(oriText,key);
            System.out.println(pathname+"加密完成!");
            System.out.println("使用密钥:"+key+"加密后得到密文为:");
            System.out.println(ciphertext);
            String pathname2="/new/IdeaProjects/StudyTest/ciphertext.txt";
            if (printToFile(ciphertext,pathname2)){
                System.out.println("密文保存在:"+pathname2);
                return;
            }else {
                System.out.println("密文保存失败!");
                return;
            }
        }else if (choose==2){
            System.out.println("输入密钥(0-26):");
            int key=scanner.nextInt();
            String pathname2="/new/IdeaProjects/StudyTest/ciphertext.txt";
            String cipherText=getFileText(pathname2);
            String oriText=decrypting(cipherText,key);
            System.out.println("使用密钥:"+key+"解密完成,明文为:");
            System.out.println(oriText);

        }else {
            return;
        }
    }

//读文件
    public static String getFileText(String pathname){
        File file=new File(pathname);
        String str="";
        try {
            Scanner scanner = new Scanner(file);
            while(scanner.hasNext())
            {
                str=str+" "+scanner.next();
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("file not found.");
        }
        return  str;
    }

    //写文件
    public static boolean printToFile(String str,String pathname){
        File file = new File(pathname);// 要写入的文件路径
        if (!file.exists()) {// 判断文件是否存在
            try {
                file.createNewFile();// 如果文件不存在创建文件
                System.out.println("文件"+file.getName()+"已为您创建!");
            } catch (IOException e) {
                System.out.println("创建文件异常!");
                e.printStackTrace();
                return false;
            }
        } else {
            System.out.println("文件"+file.getName()+"已存在!");
        }


        FileOutputStream fos = null;
        PrintStream ps = null;
        try {
            fos = new FileOutputStream(file,true);// 文件输出流	追加
            ps = new PrintStream(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String string  = str + "\r\n";// +换行
        ps.print(string); // 执行写操作
        ps.close();	// 关闭流

        return true;
    }
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值