Files
OOP-Cpp/oop_hw2/oop_hw2_1/Q1.cpp
2025-08-11 00:01:30 +08:00

62 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <cstdlib>
class TRandom {
private:
unsigned long seed;
public:
TRandom(unsigned long s) : seed(s) {}
unsigned char NextByte() {
seed = seed * 1103515245 + 12345;
return static_cast<unsigned char>((seed / 65536) % 256);
}
};
void Coder(unsigned char data[], int len, unsigned long key) {
TRandom rand(key);
for (int i = 0; i < len; ++i) {
data[i] ^= rand.NextByte();
}
}
void Coder(unsigned char data[], int len, TRandom& rand, unsigned long key) {
for (int i = 0; i < len; ++i) {
data[i] ^= rand.NextByte();
}
}
class Crypter {
private:
TRandom rand;
public:
Crypter(unsigned long seed) : rand(seed) {}
void Process(unsigned char data[], int len) {
for (int i = 0; i < len; ++i) {
data[i] ^= rand.NextByte();
}
}
};
void Test() {
unsigned char msg[] = "Hello World";
int len = sizeof(msg);
unsigned long key = 12345;
std::cout << "Original: " << msg << std::endl;
Coder(msg, len, key);
std::cout << "Encrypted: "; for (int i = 0; i < len; ++i) std::cout << msg[i]; std::cout << std::endl;
Coder(msg, len, key);
std::cout << "Decrypted: " << msg << std::endl;
}
int main(){
Test();
return 0;
}
// 问题3将 rand 改为 TRandom rand传值会丢失原状态重复初始化种子不可行
// 问题4改为 const TRandom& rand不行因为 NextByte 改变对象状态,不能用于 const