主题:[原创]加密算法系列
zouyu1983
[专家分:1670] 发布于 2006-06-21 22:43:00
好久没来了,最近的安全课,编了不少加密算法,放上来与大家共同切磋^_^
之下去的一系列加密算法都继承与一下基类
//Encrypt system
//author: smallrain
//2006.3.22 in fzu
//MyCipherSystem.h
#ifndef ENCRYPT_SYSTEM
#define ENCRYPT_SYSTEM
#include <string>
using std::string;
typedef char Byte;
#endif //ENCRYPT_SYSTEM
回复列表 (共43个回复)
沙发
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:19:00
//MyCipherSystem.cpp
#include "MyCipherSystem.h"
//////////////////////////////////////////////////////////////////////////////////////
//define my class
void EncryptSystem::GetData(const string& str)
{
text = "";
int i;
for (i=0; i<(int)str.length(); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z') text += str[i] + 32;
else if (str[i] >= 'a' && str[i] <= 'z') text += str[i];
}
return;
}
板凳
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:20:00
//恺撒密码
//CaeserCipherSystem.h
#ifndef CAESER_CIPHER_SYSTEM_H
#define CAESER_CIPHER_SYSTEM_H
#include "MyCipherSystem.h"
class CaeserEncryptSystem : public EncryptSystem
{
public:
bool GetKey(int shift);
void Encrypt();
void Decrypt();
private:
int shiftKey;
};
#endif//CAESER_CIPHER_SYSTEM_H
3 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:22:00
//CaeserCipherSystem.cpp
#include "CaeserCipherSystem.h"
//////////////////////////////////////////////////////////////////////////////////////
//////////////// Implement class of CaeserEncryptSystem //////////////////
//////////////////////////////////////////////////////////////////////////////////////
bool CaeserEncryptSystem::GetKey(int key)
{
this->shiftKey = key;
return true;
}
void CaeserEncryptSystem::Encrypt()
{
int i;
for (i=0; i<(int)text.length(); i++)
{
text[i] = char((int(text[i] - 97) + shiftKey) % 26 + 97);
}
return;
}
void CaeserEncryptSystem::Decrypt()
{
int i;
for (i=0; i<(int)text.length(); i++)
{
text[i] = char((int(text[i] - 97) + 26 - shiftKey) % 26 + 97);
}
return;
}
4 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:24:00
//KeyWord密码
//KeyWordCipherSystem.h
#ifndef KEYWORD_CIPHER_SYSTEM_H
#define KEYWORD_CIPHER_SYSTEM_H
#include "MyCipherSystem.h"
class KeyWordEncryptSystem : public EncryptSystem
{
public:
KeyWordEncryptSystem();
bool GetKey(std::string& str, char start = 'a');
void Encrypt();
void Decrypt();
char encryptArray[26];
private:
bool markArray[26];
char decryptArray[26];
};///:p
#endif//KEYWORD_CIPHER_SYSTEM_H
5 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:25:00
//KeyWordCipherSystem.cpp
#include "KeyWordCipherSystem.h"
//////////////////////////////////////////////////////////////////////////////////////
//////////////// Implement class of KeyWordEncryptSystem //////////////////
//////////////////////////////////////////////////////////////////////////////////////
KeyWordEncryptSystem::KeyWordEncryptSystem()
{
int i;
for(i=0; i<26; i++) markArray[i] = false;
}
bool KeyWordEncryptSystem::GetKey(string& keyWord, char start)
{
if (start >= 'A' && start <= 'Z') start += 32;
if (start >= 'a' && start <= 'z')
{
int i = int(start - 97);
int j;
for (j=0; j<(int)keyWord.length(); j++)
{
if (keyWord[j] >= 'A' && keyWord[j] <= 'Z') keyWord[j] += 32;
if (keyWord[j] >= 'a' && keyWord[j] <= 'z' && !markArray[keyWord[j] - 97])
{
int temp = i % 26;
encryptArray[temp] = keyWord[j];
markArray[keyWord[j] - 97] = true;
decryptArray[keyWord[j] - 97] = char(temp + 97);
i++;
}
}
for (j=0; j<26; j++)
{
if (!markArray[j])
{
int temp = i % 26;
encryptArray[temp] = char(j + 97);
markArray[j] = true;
decryptArray[j] = char(temp + 97);
i++;
}
}
return true;
}
else return false;
}
void KeyWordEncryptSystem::Encrypt()
{
int j;
for (j=0; j<(int)text.length(); j++) text[j] = encryptArray[text[j] - 97];
}
void KeyWordEncryptSystem::Decrypt()
{
int i;
for (i=0; i<(int)text.length(); i++) text[i] = decryptArray[text[i] - 97];
}
//////////////////////////////////////////////////////////////////////////////////////
6 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:26:00
//PlayFair
//PlayFairCipherSystem.h
#ifndef PLAY_FAIR_CIPHER_SYSTEM_H
#define PLAY_FAIR_CIPHER_SYSTEM_H
#include "MyCipherSystem.h"
class PlayFairEncryptSystem : public EncryptSystem
{
public:
PlayFairEncryptSystem();
void GetData(const std::string& str);
bool GetKey(std::string& str);
void Encrypt();
void Decrypt();
char encryptMatrix[5][5];
private:
const char NO_USE_CHAR;
bool markArray[26];
int charRow[26];
int charCol[26];
};
#endif//PLAY_FAIR_CIPHER_SYSTEM_H
7 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:28:00
回不了了?
8 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:29:00
//PlayFairCipherSystem.cpp
#include "PlayFairCipherSystem.h"
//////////////////////////////////////////////////////////////////////////////////////
//////////////// Implement class of PlayFairEncryptSystem //////////////////
//////////////////////////////////////////////////////////////////////////////////////
PlayFairEncryptSystem::PlayFairEncryptSystem() : NO_USE_CHAR('x')
{
int i;
for(i=0; i<26; i++) markArray[i] = false;
markArray[int('j' - 97)] = true;
}
void PlayFairEncryptSystem::GetData(const string& str)
{
text = "";
string temp;
int i;
for (i=0; i<(int)str.length(); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z') temp += str[i] + 32;
else if (str[i] >= 'a' && str[i] <= 'z')
{
if (str[i] == 'j') temp += 'i';
else temp += str[i];
}
}
if (temp.length() == 0) return;
text += temp[0];
for (i=1; i<(int)temp.length(); i++)
{
if (text[text.length() - 1] == temp[i]) text += NO_USE_CHAR;
text += temp[i];
}
if (text.length() % 2 != 0) text += NO_USE_CHAR;
return;
}
bool PlayFairEncryptSystem::GetKey(string& keyWord)
{
int row = 0;
int col = 0;
int j;
for (j=0; j<(int)keyWord.length(); j++)
{
if (keyWord[j] >= 'A' && keyWord[j] <= 'Z') keyWord[j] += 32;
if (keyWord[j] == 'j') keyWord[j] = 'i';
if (keyWord[j] >= 'a' && keyWord[j] <= 'z' && !markArray[keyWord[j] - 97])
{
encryptMatrix[row][col] = keyWord[j];
charRow[keyWord[j] - 97] = row;
charCol[keyWord[j] - 97] = col;
col++;
if (col >= 5)
{
col = 0;
row++;
}
markArray[keyWord[j] - 97] = true;
}
}
for (j=0; j<26; j++)
{
if (!markArray[j])
{
encryptMatrix[row][col] = char(j + 97);
charRow[j] = row;
charCol[j] = col;
col++;
if (col >= 5)
{
col = 0;
row++;
}
markArray[j] = true;
}
}
return true;
}
void PlayFairEncryptSystem::Encrypt()
{
if (text.length() == 0) return;
int forward = 1;
int follow = 0;
while (forward < (int)text.length())
{
int m1Row = charRow[int(text[follow] - 97)];
int m1Col = charCol[int(text[follow] - 97)];
int m2Row = charRow[int(text[forward] - 97)];
int m2Col = charCol[int(text[forward] - 97)];
if (m1Row == m2Row)
{
text[follow] = encryptMatrix[m1Row][(m1Col + 1) % 5];
text[forward] = encryptMatrix[m2Row][(m2Col + 1) % 5];
}
else if (m1Col == m2Col)
{
text[follow] = encryptMatrix[(m1Row + 1) % 5][m1Col];
text[forward] = encryptMatrix[(m2Row + 1) % 5][m2Col];
}
else
{
text[follow] = encryptMatrix[m1Row][m2Col];
text[forward] = encryptMatrix[m2Row][m1Col];
}
forward += 2;
follow += 2;
}
return;
}
//////////////////////////////////////////////////////////////////////////////////////
9 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:30:00
//PlayFairCipherSystem.cpp 续
void PlayFairEncryptSystem::Decrypt()
{
int forward = 1;
int follow = 0;
while (forward < (int)text.length())
{
int m1Row = charRow[int(text[follow] - 97)];
int m1Col = charCol[int(text[follow] - 97)];
int m2Row = charRow[int(text[forward] - 97)];
int m2Col = charCol[int(text[forward] - 97)];
if (m1Row == m2Row)
{
text[follow] = encryptMatrix[m1Row][(m1Col + 5 - 1) % 5];
text[forward] = encryptMatrix[m2Row][(m2Col + 5 - 1) % 5];
}
else if (m1Col == m2Col)
{
text[follow] = encryptMatrix[(m1Row + 5 - 1) % 5][m1Col];
text[forward] = encryptMatrix[(m2Row + 5 - 1) % 5][m2Col];
}
else
{
text[follow] = encryptMatrix[m1Row][m2Col];
text[forward] = encryptMatrix[m2Row][m1Col];
}
forward += 2;
follow += 2;
}
return;
}
10 楼
zouyu1983 [专家分:1670] 发布于 2006-04-26 12:34:00
//Vigenere
//VigenereCipherSystem.h
#ifndef VIGENERE_CIPHER_SYSGEM_H
#define VIGENERE_CIPHER_SYSGEM_H
#include "MyCipherSystem.h"
class VigenereEncryptSystem : public EncryptSystem
{
public:
bool GetKey(const std::string& str);
void Encrypt();
void Decrypt();
private:
std::string keyWord;
};
#endif//VIGENERE_CIPHER_SYSGEM_H
我来回复