密码学原理精解【5】

news/2024/7/8 7:54:24 标签: 密码学

这里写目录标题

移位密码

概述

以 z 26 运算为例 , k 为密钥 加密: e k ( x ) = ( x + k ) m o d 26 解密: d k ( x ) = ( x − k ) m o d 26 以z_{26} 运算为例,k为密钥 \\加密:e_k(x)=(x+k) mod 26 \\解密:d_k(x)=(x-k) mod 26 z26运算为例,k为密钥加密:ek(x)=(x+k)mod26解密:dk(x)=(xk)mod26
实际中,我们使用 Z 256 Z_{256} Z256运算(群的加法运算)

代码

#include <iostream>
#include <fstream>
#include<cstring>

using namespace std;

int main(){
    //加密写入
    ofstream fileOut;
    char helloTxt[]{"&9*&((@#$)((%#^^hello,world!\n123456789"};
    int txtLen{strlen(helloTxt)};
    fileOut.open("hello.data",ios::binary);
    int k=77;
    for(int i=0;i<txtLen;i++){
        fileOut<<char((helloTxt[i]+k) %256);
    }
    fileOut.close();
    //解密读出
    ifstream fileIn;
    char helloChar;
    fileIn.open("hello.data",ios::binary);
    while (fileIn.get(helloChar)){
        cout<<char((helloChar-k) %256);
    }
    fileIn.close();

  }
&9*&((@#$)((%#^^hello,world!
123456789
Process returned 0 (0x0)   execution time : 0.120 s
Press any key to continue.

希尔密码( Z 256 Z_{256} Z256)

待加密长度被3整除

#include <iostream>
#include <fstream>
#include<cstring>
#include "e:/eigen/Eigen/Dense"

using namespace std;
using namespace Eigen;

int main(){
    //加密写入
    ofstream fileOut;
    char helloTxt[]{"123456789$#%(&Yoiu9"};
    int txtLen{strlen(helloTxt)};

    fileOut.open("hello.data",ios::binary);
    for(int i=0;i<txtLen;i+=3){
        Matrix<int,1,3> x{helloTxt[i],helloTxt[i+1],helloTxt[i+2]};
        Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
        Matrix<int,1,3>  e_data=x*k;
        for (int j=0;j<3;j++){
            fileOut<<char(int(e_data[j]) % 256);
        }
    }
    fileOut.close();

    //解密读出
    ifstream fileIn;
    char helloChar;
    fileIn.open("hello.data",ios::binary);
    char txtBuffer[4];
    for(int i=0;i<txtLen;i+=3){
        fileIn.read(txtBuffer,3);
        Matrix<int,1,3> y{txtBuffer[0],txtBuffer[1],txtBuffer[2]};
        Matrix<int,3,3> k_ni{{9,11, 227},{101 ,152 ,37},{134 ,248 ,127}};
        Matrix<int,1,3> e_data=y*k_ni;
        for (int j=0;j<3;j++){
            cout<<char(int(e_data[j]) % 256);
        }
    }

    fileIn.close();
  }


待加密长度不一定被3整除

继续完善程序,针对加密长度不一定是3的倍数。

#include <iostream>
#include <fstream>
#include<cstring>
#include "e:/eigen/Eigen/Dense"

using namespace std;
using namespace Eigen;

int main(){
    //加密写入
    ofstream fileOut;
    char helloTxt[]{"123456789p"};
    int txtLen{strlen(helloTxt)};


    fileOut.open("hello.data",ios::binary);
    for(int i=0;i<txtLen;i+=3){
        if ((txtLen-i)==1){
            Matrix<int,1,3> x{helloTxt[i],0,0};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
        else if ((txtLen-i)==2){
            Matrix<int,1,3> x{helloTxt[i],helloTxt[i+1],0};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
        else{
            Matrix<int,1,3> x{helloTxt[i],helloTxt[i+1],helloTxt[i+2]};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
    }
    fileOut.close();

    //解密读出
    ifstream fileIn;
    char helloChar;
    fileIn.open("hello.data",ios::binary);
    char txtBuffer[4];
    for(int i=0;i<txtLen;i+=3){
        fileIn.read(txtBuffer,3);
        Matrix<int,1,3> y{txtBuffer[0],txtBuffer[1],txtBuffer[2]};
        Matrix<int,3,3> k_ni{{9,11, 227},{101 ,152 ,37},{134 ,248 ,127}};
        Matrix<int,1,3> e_data=y*k_ni;
        char d_data[3];
        for (int j=0;j<3;j++){
           d_data[j]=char(int(e_data[j]) % 256);
        }

        if ((txtLen-i)==2 ){
              cout<<d_data[0]<<d_data[1];
        }
        else if((txtLen-i)==1 ){
             cout<<d_data[0];
        }
        else{
            for (int j=0;j<3;j++){
              cout<<d_data[j];
            }
        }
    }

    fileIn.close();
  }
123456789p
Process returned 0 (0x0)   execution time : 0.376 s
Press any key to continue.

加解密文件

#include <iostream>
#include <fstream>
#include<cstring>
#include "e:/eigen/Eigen/Dense"

using namespace std;
using namespace Eigen;

int main(){
    //加密写入
    ifstream  picfs("test.png", ios::ate|ios::binary);
    // 获取文件大小
    std::streampos picSize = picfs.tellg();
    picfs.close(); // 关闭文件
    std::cout << "文件长度: " << picSize << " 字节" << std::endl;

    ifstream  ifs("test.png", ios::in|ios::binary);
    char *picData=new char[picSize+1];
    for (int i=0;i<picSize;i++){
         ifs.get(picData[i]);
    }
    ifs.close();


    int txtLen{picSize};

    ofstream  fileOut;
    fileOut.open("pic.dat",ios::binary);
    for(int i=0;i<txtLen;i+=3){
        if ((txtLen-i)==1){
            Matrix<int,1,3> x{picData[i],0,0};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
        else if ((txtLen-i)==2){
            Matrix<int,1,3> x{picData[i],picData[i+1],0};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
        else{
            Matrix<int,1,3> x{picData[i],picData[i+1],picData[i+2]};
            Matrix<int,3,3> k{{16,91,215}, {171,253,50}, {8,186,121}};
            Matrix<int,1,3>  e_data=x*k;
            for (int j=0;j<3;j++){
                fileOut<<char(int(e_data[j]) % 256);
            }
        }
    }
    fileOut.close();

    //解密读出
    ofstream deFileOut;
    deFileOut.open("test_d.png",ios::binary);
    ifstream fileIn;
    char helloChar;
    fileIn.open("pic.dat",ios::binary);
    char txtBuffer[4];
    for(int i=0;i<txtLen;i+=3){
        fileIn.read(txtBuffer,3);
        Matrix<int,1,3> y{txtBuffer[0],txtBuffer[1],txtBuffer[2]};
        Matrix<int,3,3> k_ni{{9,11, 227},{101 ,152 ,37},{134 ,248 ,127}};
        Matrix<int,1,3> e_data=y*k_ni;
        char d_data[3];
        for (int j=0;j<3;j++){
           d_data[j]=char(int(e_data[j]) % 256);
        }

        if ((txtLen-i)==2 ){
              deFileOut<<d_data[0]<<d_data[1];
        }
        else if((txtLen-i)==1 ){
             deFileOut<<d_data[0];
        }
        else{
            for (int j=0;j<3;j++){
              deFileOut<<d_data[j];
            }
        }
    }
    deFileOut.close();
    fileIn.close();
    delete[] picData;
  }



http://www.niftyadmin.cn/n/5536865.html

相关文章

C#Winform窗体中嵌入exe文件

1&#xff0c;效果以嵌入Modbus Slave为例&#xff1a; 2&#xff0c;代码&#xff1a; public partial class Form1 : Form{//设置嵌入exe的常量private const int nIndex -16;private const int dwNewLong 0x10000000;Process m_AppProcess;public Form1(){InitializeCompo…

c进阶篇(四):内存函数

内存函数以字节为单位更改 1.memcpy memcpy 是 C/C 中的一个标准库函数&#xff0c;用于内存拷贝操作。它的原型通常定义在 <cstring> 头文件中&#xff0c;其作用是将一块内存中的数据复制到另一块内存中。 函数原型&#xff1a;void *memcpy(void *dest, const void…

ffmpeg使用mjpeg把yuvj420p编码为jpg图像

version #define LIBAVCODEC_VERSION_MAJOR 60 #define LIBAVCODEC_VERSION_MINOR 15 #define LIBAVCODEC_VERSION_MICRO 100 note 1. 通过*.jpg推测时&#xff0c;out_fmt为image2&#xff0c;打开*.jpg文件时&#xff0c;in_fmt为image2 但是out_fmt为image2时&…

中英双语介绍美国的州:阿肯色州(Arkansas)

中文版 阿肯色州简介 阿肯色州&#xff08;Arkansas&#xff09;位于美国南部&#xff0c;以其多样的自然景观、丰富的文化遗产和不断发展的经济而闻名。以下是对阿肯色州的详细介绍&#xff0c;包括其地理位置、人口、经济、教育、文化和主要城市。 地理位置 阿肯色州东临…

【经验总结】Springboot打印指定类的日志到指定文件中

原文地址&#xff1a;https://www.cnblogs.com/zeng1994/p/f9bff238b13a0bf8fb8bf88c41db7a34.html 以下内容已经过实践&#xff0c;勘误&#xff0c;总结 环境&#xff1a;Springboot2.5.2 公司有个项目&#xff0c;需要和几个第三方系统对接。这种项目&#xff0c;日志一定要…

C++ | Leetcode C++题解之第214题最短回文串

题目&#xff1a; 题解&#xff1a; class Solution { public:string shortestPalindrome(string s) {int n s.size();vector<int> fail(n, -1);for (int i 1; i < n; i) {int j fail[i - 1];while (j ! -1 && s[j 1] ! s[i]) {j fail[j];}if (s[j 1] …

Vbus 和 Vbat

在嵌入式系统开发中&#xff0c;Vbus 和 Vbat 是两个不同的电源相关术语&#xff0c;它们的区别主要在于它们的用途和连接的电源类型。 Vbus 定义: Vbus 通常是指 USB 总线电压。在 USB 2.0 中&#xff0c;Vbus 通常为 5V 电源。用途: Vbus 提供电源给 USB 设备&#xff0c;确…

3.用户程序与驱动交互

驱动程序请使用第二章https://blog.csdn.net/chenhequanlalala/article/details/140034424 用户app与驱动交互最常见的做法是insmod驱动后&#xff0c;生成一个设备节点&#xff0c;app通过open&#xff0c;read等系统调用去操作这个设备节点&#xff0c;这里先用mknode命令调…