Homework1

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值