OOP HomeWork
This commit is contained in:
61
oop_hw2/oop_hw2_1/Q1.cpp
Normal file
61
oop_hw2/oop_hw2_1/Q1.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD> rand <20><>Ϊ TRandom rand<6E><64><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>ᶪʧԭ״̬<D7B4><CCAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD>Ϊ const TRandom& rand<6E><64><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD>Ϊ NextByte <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> const
|
BIN
oop_hw2/oop_hw2_1/Q1.exe
Normal file
BIN
oop_hw2/oop_hw2_1/Q1.exe
Normal file
Binary file not shown.
BIN
oop_hw2/oop_hw2_1/测试截图1.png
Normal file
BIN
oop_hw2/oop_hw2_1/测试截图1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 101 KiB |
Reference in New Issue
Block a user