密码学原理精解【5】

这里写目录标题

移位密码

概述

以 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)=(x−k)mod26

实际中,我们使用 Z 256 Z_{256} Z256运算(群的加法运算)

代码

cpp 复制代码
#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();

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

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

待加密长度被3整除

cpp 复制代码
#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的倍数。

cpp 复制代码
#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();
  }
bash 复制代码
123456789p
Process returned 0 (0x0)   execution time : 0.376 s
Press any key to continue.

加解密文件

cpp 复制代码
#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;
  }
相关推荐
新时代的弩力11 小时前
哈希,对称加密,非对称加密
密码学
橘子真甜~20 小时前
C/C++ Linux网络编程11 - 数据加密与https协议
linux·服务器·网络·http·https·密码学·加密解密
岁岁的O泡奶2 天前
NSSCTF_crypto_[MTCTF 2021 final]ezRSA
经验分享·python·算法·密码学·crypto
傻小胖6 天前
第2讲:BTC-密码学原理 北大肖臻老师客堂笔记
笔记·web3·密码学
courniche9 天前
金融数据密码机、服务器密码机、签名验签服务器、时间戳服务器4款相比较
密码学
漏洞文库-Web安全10 天前
CTF密码学之SM4
安全·web安全·网络安全·密码学·ctf
HaiLang_IT10 天前
2026年密码学专业选题全攻略:从数字签名到漏洞挖掘的精选选题指南
密码学
搬砖魁首10 天前
密码学系列 - ECDSA vs. EdDSA:主要区别
密码学·签名·ecdsa·eddsa
C++ 老炮儿的技术栈10 天前
用密码学安全随机数生成256位密钥
c语言·开发语言·c++·windows·安全·密码学·visual studio
聊询QQ:6882388610 天前
顶刊复现基于球形向量改进的粒子群算法PSO的无人机3D路径规划,spherical vecto...
密码学