Homework1

来源:互联网 发布:mac销量 编辑:程序博客网 时间:2024/06/07 04:01

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 mM, Enck(m)=m.
The equality means that we don’t use k to encrypt the message, so Pr[M=m|C=c]=1, which shows that this scheme is not perfectly secret.

2.Let G be a pseudorandom generator where |G(s)|2|s|.
(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 G0 be a pseudorandom generator where |G0(s)|4|s|
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 G(s)=defG(s0|s|), we can easily find that if s has the format s1s2...s|s|20|s|2, the output of G is always equal with G0(0|s|2), which is not pseudorandom.

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:
运行结果

0 0
原创粉丝点击