信息安全之仿射密码加密和解密

本文利用仿射密码,对一个只含可打印字符的txt文件进行加密和解密。

//加解密时,文件内容为所有可打印字符,即ASCII码从32-126的95个字符
#include<iostream>
#include<fstream>
using namespace std;

//加密类
class Encrypt {
public:
    void set_AB(int a, int b) {A = a; B = b;}  //设置加密密钥

    int gcd(int a, int b) {
        while(b) {
            int r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

    bool Judge(int a, int b) {  //判断密钥是否合法
        if(gcd(a, 95) == 1) {
            set_AB(a, b);
            return true;
        }
        else {
            cout << "密钥不合法,请重新输入!" << endl;
            return false;
        }
    }

    int get_A() {return A;}  //获取加密密钥
    int get_B() {return B;}

    int encrypt(char ch) {  //对单个字符进行加密
        int plaintext_id = ch - ' ';
        int A = get_A(), B = get_B();
        int ciphertext_id = (A * plaintext_id + B) % 95;
        return ciphertext_id + 32;
    }

private:
    int A, B;  //加密密钥
};

//解密类
class Decrypt {
public:

    int x, y;  //a*x + b*y = gcd(a, b)
    int extend_eulid(int a, int b) {
        if(b == 0) {
            x = 1;
            y = 0;
        }
        else {
            extend_eulid(b, a%b);
            int temp = x;
            x = y;
            y = temp - a / b * y;
        }
    }

    int get_inv(int a) { //求A的乘法逆元
        extend_eulid(a, 95);
        int inv = x;
        if(inv < 0) inv += 95;
        return inv;
    }

    void set_A(int a) {A = get_inv(a);}
    void set_B(int b) {B = b;}

    int get_A() {return A;}  //获取解密密钥
    int get_B() {return B;}

    int decrypt(char ch) {  //对单个字符进行解密
        int ciphertext_id = ch - ' ';
        int A = get_A(), B = get_B();
        int plaintext_id = A * (ciphertext_id - B + 95) % 95;
        return plaintext_id + 32;
    }

private:
    int A, B; //解密密钥
};

int main()
{
    Encrypt en;
    Decrypt de;
    int A, B;
    char ch;

    while(1) {  //输入加密密钥
        cin >> A >> B;
        if(en.Judge(A, B) == true) break;
    }
    de.set_A(A), de.set_B(B);  //设置解密密钥

    ifstream fin("F:text.txt", ios::in);   //以输入方式打开明文文件"text.txt"
    ofstream fout("F:ciphertext.txt", ios::out);  //以输出方式打开保存密文的文件"ciphertext.txt",并将其原有的内容清除
    if(!fin) {
        cout << "Can not open the plaintext file!" << endl;
    }
    while(fin.get(ch) != NULL) {
        int ciphertext_id = en.encrypt(ch);
        char ciphertext_ch = char(ciphertext_id);
        fout << ciphertext_ch;
    }  //从明文文件"F:test.txt"中一次读取一个字符进行加密,将加密后的文件保存为 "ciphertext.txt"
    fout.close();

    ifstream fin1("F:ciphertext.txt", ios::in); //以输入方式打开密文文件"ciphertext.txt"
    ofstream fout1("F:plaintext.txt", ios::out); //以输出方式打开保存解密后的文件"plaintext.txt",并将其原有的内容清除
    if(!fin1) {
        cout << "Can not open the ciphertext file!" << endl;
    }
    while(fin1.get(ch) != NULL) {
        int  plaintext_id = de.decrypt(ch);
        char plaintext_ch = char(plaintext_id);
        fout1 << plaintext_ch;
    } //从密文文件"ciphertext.txt"中一次读取一个字符进行解密,将解密后的文件保存为"plaintext.txt"
    fout1.close();

    return 0;
}
时间: 2025-01-01 21:24:11

信息安全之仿射密码加密和解密的相关文章

Java密码加密与解密

Java密码加密与解密 Java中对代码进行加密与解密,其中用MD5方式的是不可逆的.   import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import

Vigen&amp;#232;re 密码加密及解密

定义:戳这里 加密: #include<bits/stdc++.h> using namespace std; char a[1200],b[1200],tmp,ans[1200]; int main() { printf(" Vigenère加密\n\n" ); printf("输入秘钥:");scanf("%s",b);puts(""); printf("输入明文:");scanf(&quo

C#:使用MD5对用户密码加密与解密

C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1)16位的MD5加密 /// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns&

转 C#:使用MD5对用户密码加密与解密

C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1)16位的MD5加密 /// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns&

MySQL密码加密与解密

MySQL加密和解密实例详解 有多种前端加密算法可用于数据加密.解密,这是一种简单的数据库级别的数据加密.解密解决方案. 以MySQL数据库为例,它内建了相应的加密函数(AES_ENCRYPT() )和解密函数(AES_DECRYPT()). 1.建表:建表时注意数据的类型 CREATE TABLE users( username VARCHAR(10), PASSWORD VARCHAR(10), testpswd VARBINARY(20) ); 此表有三个字段,'用户名' ,'密码' ,'

仿射密码加密解密文件流

#include<iostream> #include<string> #include<fstream> #include<Windows.h> using namespace std; class PWoper { string inpatch; string outpatch; public: PWoper(string in, string out) { inpatch = in; outpatch = out; } void encrypt() {

维吉尼亚密码加密、解密算法(破解还不会);

#include <iostream> #include<cstdio> using namespace std; void init_pass_table(char (&code_table)[27][27]){//制密码对照表;(不能当做一维数组来看会有别的问题) code_table[0][0]='0'; for(int i=0;i<26;i++){ code_table[0][1+i]='a'+i; } for(int i=0;i<26;i++){ co

关于加密和解密的方法

最近在做一个关于密码加密和解密的winform,要求对密码的TextBox输入的字符串进行加密,同时传值得时候也要解析出来,于是就写了一个demo,仅供参考. EncryptOrDecryptPassword类包含加密和解密两个简单的方法,这个方法是根据字符的Encoding来加密的,然后在main函数中调用. EncryptOrDecryptPassword类如下: 1 public class EncryptOrDecryptPassword 2 { 3 public string Decr

简单的加密与解密的实现---仿射密码

#include <iostream> #include <string.h> using namespace std; int main() {     void encrytion_decrypt(char input[],int len);     char input[10];     cout<<"Please input password:";     cin>>input;     int len;     len=strl