Homework1
1.Prove or refute: Every encryption scheme for which the size of the key space equals the size of the message space, and for which the key is chosen uniformly from the key space, is perfectly secret.
Solution:
The statement is false.
Consider an encryption scheme (Gen,Enc,Dec) whose key space and message space are both
M
, and for every message m
The equality means that we don’t use
k
to encrypt the message, so
2.Let
G
be a pseudorandom generator where
(a) Define
G′(s)=defG(s0|s|)
. Is
G′
necessarily a pseudorandom generator?
Solution:
Not necessarily.We can construct a special kind of pseudorandom generator
G
to solve the problem.
Let
Then we can get a
|G(s)|=|G0(s|s|2+1...s|s|)|⩾2∗|s|
. Apparently
G
is a pseudorandom generator.
However, if we consider
3. Write a program to achieve a one time pad.
Solution:
#include "randpool.h"
#include <iostream>
using namespace std;
using namespace CryptoPP;
#pragma comment(lib, "cryptlib.lib")
RandomPool & GlobalRNG();
int main()
{
const char* list = "helloworld";
int len = strlen(list);
int *lis = new int[len];
int *key = new int[len];
int *rst = new int[len];
char *keyc = new char[len + 1];
char *rstc = new char[len + 1];
for (int i = 0; i < len; i++)
{
lis[i] = list[i] - 'a';
}
for (int i = 0; i < len; i++)
{
int a = GlobalRNG().GenerateByte() % 26;
key[i] = a;
keyc[i] = (char)('a' + a);
int b = a^lis[i];
rst[i] = b;
rstc[i] = (char)('a' + b % 26);
}
keyc[len] = '\0';
rstc[len] = '\0';
cout << "The generated key is " << keyc << endl;
cout << "The encrypted string is " << rstc << endl;
char* dec = new char[len + 1];
for (int i = 0; i < len; i++)
{
int t = key[i] ^ rst[i];
dec[i] = (char)('a' + t);
}
dec[len] = '\0';
cout << "The decrypted message is " << dec << endl;
system("pause");
}
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}
The result is as following: