仿射密码加密解密文件流

#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()
    {
        ifstream init(inpatch, ios::binary);
        ofstream outit(outpatch, ios::binary);
        if (!init || !outit)
        {
            cout << "IN ERROR OR OUT ERROR!" << endl;
        }
        int *initp = new int();
        int *outitp = new int();
        while (init.read((char*)initp, 1)){
            //cout << *initp << endl;
            *outitp = (*initp * 177 + 135) % 311;
            //cout << "out--"<<*outitp << endl;

            //一个字节8位,最大256;这里的 "*outitp mod 311" 数值最大311,如果以1个字读进来运算以后write装不下。
            outit.write((char*)outitp, 2);
        }
        cout << "encrypt success!\n";
        init.close();
        outit.close();
        //delete original data

        string temp = "del ";
        temp += inpatch;
        //system命令只接受一个参数,以下代码实现system("del 变量")。
        const char *link = temp.data();
        system(link);
    }
    void decrypt()
    {
        ofstream init(inpatch, ios::binary);
        ifstream outit(outpatch, ios::binary);
        if (!init || !outit)
        {
            cout << "IN ERROR OR OUT ERROR!" << endl;
        }
        int *ini = new int();
        int *outi = new int();
        while (outit.read((char*)outi, 2)){
            //cout << *outi << endl;
            *ini = ((*outi - 135 + 311) * 123) % 311;
            //cout <<"out"<< *ini <<endl;

            //
            init.write((char*)ini, 1);
        }
        cout << "decrypt success!\n";
        init.close();
        outit.close();
    }
};
int main()
{

    string inpatch, outpatch, str;
    cout << "input InPtach: ";
    cin >> inpatch;
    cout << "input OutPatch: ";
    cin >> outpatch;
    PWoper pwoper(inpatch, outpatch);
    pwoper.encrypt();
    cout << "input ok to decrypt: ";
    cin >> str;
    //system("cls");
    if (str == "ok")
        pwoper.decrypt();
    else {
        cout << "input error!";
        system("pause");
    }
    system("pause");

}
时间: 2024-08-26 12:13:04

仿射密码加密解密文件流的相关文章

常用密码加密解密操作相关类

/// <summary> /// 密码加密解密操作相关类 /// </summary> public static class EncryptHelper { #region MD5 加密 /// <summary> /// MD5加密 /// </summary> public static string Md532(this string source) { if (string.IsNullOrEmpty(source)) return null;

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

本文利用仿射密码,对一个只含可打印字符的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

base64加密解密文件

1 //字符串加密 2 -(void)demo1 { 3 //普通的 8 bit二进制数据 4 NSString *str = @"hello world!"; 5 //将字符串转换成二进制数据 6 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; 7 8 //base64 加密后的二进制数据 9 NSData *base64Data = [data base64EncodedDataWithOptions:0];

凯撒密码加密解密--JAVA实现(基础)

凯撒密码一种代换密码,据说凯撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码.凯撒密码的基本思想是:通过把字母移动一定的位数来实现加密和解密.明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文. 使用JAVA程序实现对明文的加密: package com.qikeyishu.www; public class Kaisapwd { public static void main(String[] args) { char str[]={'a','b'

Openfire用户密码加密解密

需求要求审核过程中都用匿名进行用户注册登录,注册用户审核通过后才使用openfire内置表 如何做到用户密码统一 Openfire是通过org.jivesoftware.util.Blowfish.java实现的加密. 密钥:在openfire的数据库中有表ofproperty,其中就有passwordKey的值 Blowfish blow=new Blowfish(passwordKey); //加密 String encrypt=blow.encryptString("123");

vue 登录 + 记住密码 + 密码加密解密

<template> <el-form :model="ruleForm"> <h3 class="title">系统登录</h3> <el-form-item prop="mobile"> <el-input type="text" v-model="ruleForm.mobile" auto-complete="off"

希尔密码加密解密

这个我是从百科中看到的,但是有些地方看不懂,在查阅资料后,进行了修改,希望对大家有帮助加密例如:密钥(密码学中好象没有密匙一词)矩阵 1 3 0 2 这个是加密的密钥明文:HI THERE去空格,2个字母一组,根据字母表顺序(如a就是1)换成矩阵数值如下,末尾的E为填充字元:HI TH ER EE8   20  5   59    8  18  5HI 经过矩阵运算转换为 IS,具体算法参考下面的说明(mod是求余运算符):|1 3| 8 :1*8+3*9=35 MOD26=9 =I|0 2|

C#调用GPG命令进行加密解密文件操作

public void GPG() { string password = "1234567890"; System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe"); psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardInput = true;

数据库密码加密公用秘要生成器,数据库密码加密解密入口

public class Encryptor { public static final String HUNDSUN_VERSION="@system 管理平台 @version 2.0.1 @lastModiDate @describe "; protected static final int BLOCK_SIZE = 8; private int[] workingKey = null; private byte[] userkey = null; /** * standard