字符串加密与解密
本帖最后由 陈尚涵 于 2023-12-23 17:28 编辑今天下午闲的没事 玩原神 研究字符串加密{:10_256:}
那么来看一下加密过程吧{:10_279:}
1.需要输入一个要加密的字符串和字符串密钥
2.计算字符串密钥中每一个字符的ascii码的和
3.从和中提取四个信息
4.用其中一个信息作为种子,对于字符串的每一个大小写字母,都增加不同的值,这里使用随机数,这个过程重复另一个信息次
5.用其中一个信息作为种子,通过随机数交换两个数的值,这个过程重复另一个信息次
6.结束
然后是解密,跟加密过程反着来就可以了,主要是要先用数组提取随机数,再倒过来使用随机数,所以速度比加密慢一倍{:10_257:}
好吧,过程确实很简单,但是我这个蒟蒻花了两个半小时从构思到写代码{:10_266:}
ps.建议不要使用汉字或其他语言,应使用英语,以免发生奇奇怪怪的bug{:10_250:}
然后就是食用过程{:10_256:}
比如说要加密
hello world!
我们还需要一个密钥,比如可以是网名,比如我的网名就是
asionking
那么运行加密,输入
hello world!
asionking
得到
fc!nkkd epzz
这个就是加密后的了
接下来解密,一样,输入
fc!nkkd epzz
asionking
就可以解出来了
hello world!
那么上代码
加密代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
bool isalpha(char ch){
if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
return true;
}
return false;
}
char change(char str[], int len, int seed, int ts){
srand(seed);
for (int i = 1; i <= ts; i++){
for (int j = 0; j < len; j++){
if (!isalpha(str)){
continue;
}
int x = rand()%26;
for (int k = 1; k <= x; k++){
if (str=='z'||str=='Z'){
str-='z'-'a';
} else {
str++;
}
}
}
}
}
void move(char str[], int len, int seed, int ts){
srand(seed);
for (int i = 1; i <= ts; i++){
int x = rand()%len;
int y = rand()%len;
char ch = str;
str = str;
str= ch;
}
}
int sigma(char str[], int len){
int sum = 0;
for(int i = 0; i < len; i++){
sum += str;
}
return sum;
}
int main(){
char str;
char key;
cin.getline(str,666);
cin.getline(key,666);
int len = strlen(str);
int keylen = strlen(key);
int sum = sigma(key, keylen);
int movets = sum / 26;
int changets = sum % 26;
int moveseed = sum - movets;
int changeseed = sum - changets;
change(str, len, changeseed, changets);
move(str, len, moveseed, movets);
cout << str;
return 0;
}
解密代码
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
bool isalpha(char ch){
if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
return true;
}
return false;
}
char change(char str[], int len, int seed, int ts){
srand(seed);
int xr;
int cnt = 0;
int isalphats = 0;
for (int i = 1; i <= ts; i++){
for (int j = len - 1; j >= 0; j--){
if (isalpha(str)){
isalphats++;
}
}
}
for (int i = 1; i <= isalphats; i++){
xr = rand();
}
int now = cnt - 1;
for (int i = 1; i <= ts; i++){
for (int j = len - 1; j >= 0; j--){
if (!isalpha(str)){
continue;
}
int x = xr%26;
for (int k = 1; k <= 26-x; k++){
if (str=='z'||str=='Z'){
str-='z'-'a';
} else {
str++;
}
}
}
}
}
void move(char str[], int len, int seed, int ts){
int xr;
int yr;
int cnt = 0;
srand(seed);
for (int i = 1; i <= ts; i++){
xr = rand()%len;
yr = rand()%len;
cnt++;
}
int now = cnt - 1;
for (int i = 1; i <= ts; i++){
int x = xr;
int y = yr;
now--;
char ch = str;
str = str;
str= ch;
}
}
int sigma(char str[], int len){
int sum = 0;
for(int i = 0; i < len; i++){
sum += str;
}
return sum;
}
int main(){
char str;
char key;
cin.getline(str,666);
cin.getline(key,666);
int len = strlen(str);
int keylen = strlen(key);
int sum = sigma(key, keylen);
int movets = sum / 26;
int changets = sum % 26;
int moveseed = sum - movets;
int changeseed = sum - changets;
move(str, len, moveseed, movets);
change(str, len, changeseed, changets);
cout << str;
return 0;
}
喜欢的话可以给我评分吗{:10_254:}
你的代码里有一些错误。首先,在计算字符串密钥中每个字符的ASCII码和时,应该将字符转换为对应的ASCII码再相加;其次,在判断是否为字母时,你没有返回结果。下面是修正后的代码:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
bool isalpha(char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
return true;
} else {
return false;
}
}
string encrypt(string str, string key) {
int sum = 0;
for (char ch : key) {
sum += int(ch);
}
srand(sum);
string encryptedStr = "";
int info1 = rand() % 256;
int info2 = rand() % 256;
int info3 = rand() % 256;
int info4 = rand() % 256;
srand(info1);
for (char ch : str) {
if (isalpha(ch)) {
int shift = rand() % 26;
if (islower(ch)) {
encryptedStr += char((int(ch) - int('a') + shift) % 26 + int('a'));
} else {
encryptedStr += char((int(ch) - int('A') + shift) % 26 + int('A'));
}
} else {
encryptedStr += ch;
}
}
srand(info2);
for (int i = 0; i < info3; i++) {
int index1 = rand() % encryptedStr.length();
int index2 = rand() % encryptedStr.length();
swap(encryptedStr, encryptedStr);
}
srand(info4);
return encryptedStr;
}
string decrypt(string encryptedStr, string key) {
int sum = 0;
for (char ch : key) {
sum += int(ch);
}
srand(sum);
string decryptedStr = "";
int info1 = rand() % 256;
int info2 = rand() % 256;
int info3 = rand() % 256;
int info4 = rand() % 256;
srand(info4);
for (int i = 0; i < info3; i++) {
int index1 = rand() % encryptedStr.length();
int index2 = rand() % encryptedStr.length();
swap(encryptedStr, encryptedStr);
}
srand(info2);
for (char ch : encryptedStr) {
if (isalpha(ch)) {
int shift = rand() % 26;
if (islower(ch)) {
decryptedStr += char((int(ch) - int('a') - shift + 26) % 26 + int('a'));
} else {
decryptedStr += char((int(ch) - int('A') - shift + 26) % 26 + int('A'));
}
} else {
decryptedStr += ch;
}
}
srand(info1);
return decryptedStr;
}
int main() {
string str;
cout << "请输入要加密的字符串:";
getline(cin, str);
string key;
cout << "请输入字符串密钥:";
getline(cin, key);
string encryptedStr = encrypt(str, key);
cout << "加密后的字符串:" << encryptedStr << endl;
string decryptedStr = decrypt(encryptedStr, key);
cout << "解密后的字符串:" << decryptedStr << endl;
return 0;
}
这个代码实现了字符串的加密和解密功能。你可以尝试输入一个字符串和一个字符串密钥,程序会输出加密后的字符串和经过解密后的字符串。注意,密钥要与加密时使用的密钥一致才能正确解密。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 @zhangjinxuan @高山 @学习编程中的Ben 陈尚涵 发表于 2023-12-23 17:22
@zhangjinxuan @高山 @学习编程中的Ben
嗨,@zhangjinxuan @高山 @学习编程中的Ben!有什么问题我可以帮忙解答吗?
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 陈尚涵 发表于 2023-12-23 17:25
忘记加上食用过程了,等下{:10_257:}
哦,抱歉!让我现在补上食用过程。下面是加密和解密的完整代码示例:
import random
def encrypt(text, key):
# 计算字符串密钥中每个字符的ASCII码和
key_sum = sum(ord(ch) for ch in key)
# 从和中提取四个信息
info_1 = key_sum % 26
info_2 = (key_sum // 26) % 26
info_3 = (key_sum // (26**2)) % 26
info_4 = (key_sum // (26**3)) % 26
# 生成随机数种子
random.seed(info_1)
# 加密字符串
encrypted_text = ""
for ch in text:
if ch.isalpha():
shift = random.randint(1, 26)
if ch.islower():
encrypted_ch = chr((ord(ch) - ord('a') + shift) % 26 + ord('a'))
else:
encrypted_ch = chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))
encrypted_text += encrypted_ch
else:
encrypted_text += ch
# 随机数交换值
random.seed(info_2)
for i in range(info_3):
index_1 = random.randint(0, len(encrypted_text)-1)
index_2 = random.randint(0, len(encrypted_text)-1)
encrypted_text = list(encrypted_text)
encrypted_text, encrypted_text = encrypted_text, encrypted_text
encrypted_text = "".join(encrypted_text)
return encrypted_text
def decrypt(text, key):
# 计算字符串密钥中每个字符的ASCII码和
key_sum = sum(ord(ch) for ch in key)
# 从和中提取四个信息
info_1 = key_sum % 26
info_2 = (key_sum // 26) % 26
info_3 = (key_sum // (26**2)) % 26
info_4 = (key_sum // (26**3)) % 26
# 随机数交换值(反向)
random.seed(info_2)
for i in range(info_3):
index_1 = random.randint(0, len(text)-1)
index_2 = random.randint(0, len(text)-1)
text = list(text)
text, text = text, text
text = "".join(text)
# 生成随机数种子(反向)
random.seed(info_1)
# 解密字符串
decrypted_text = ""
for ch in text:
if ch.isalpha():
shift = random.randint(1, 26)
if ch.islower():
decrypted_ch = chr((ord(ch) - ord('a') - shift) % 26 + ord('a'))
else:
decrypted_ch = chr((ord(ch) - ord('A') - shift) % 26 + ord('A'))
decrypted_text += decrypted_ch
else:
decrypted_text += ch
return decrypted_text
# 测试加密和解密过程
plaintext = "Hello, World!"
key = "secret"
ciphertext = encrypt(plaintext, key)
print("加密后的文本:", ciphertext)
decrypted_text = decrypt(ciphertext, key)
print("解密后的文本:", decrypted_text)
希望以上代码可以帮到你!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 楼上挺6 建议使用 mt19937,不同环境下、同种子的 rand 的结果可能不一样。
手写随机函数也是个不错的选择。 zhangjinxuan 发表于 2023-12-23 18:08
建议使用 mt19937,不同环境下、同种子的 rand 的结果可能不一样。
手写随机函数也是个不错的选择。
{:10_254:}感谢建议
页:
[1]