mce encryption

This commit is contained in:
Mirek Kratochvil 2012-04-05 16:25:43 +02:00
parent 5ba94ca423
commit b715459593
2 changed files with 49 additions and 2 deletions

View file

@ -46,8 +46,25 @@ int ccr::mce::generate (pubkey&pub, privkey&priv, prng&rng, uint m, uint t)
int pubkey::encrypt (const bvector& in, bvector&out, prng&rng) int pubkey::encrypt (const bvector& in, bvector&out, prng&rng)
{ {
uint s = cipher_size();
if (t > s) return 1;
if (in.size() != plain_size() ) return 2;
return -1; //TODO //make a codeword
G.mult_vecT_left (in, out);
//add error vector
bvector e;
e.resize (s, 0);
for (uint n = t; n > 0;) {
uint p = rng.random (s);
if (!e[p]) {
e[p] = 1;
--n;
}
}
out.add (e);
return 0;
} }
int privkey::decrypt (const bvector&in, bvector&out) int privkey::decrypt (const bvector&in, bvector&out)

View file

@ -42,6 +42,14 @@ ostream& operator<< (ostream&o, ccr::matrix m)
return o; return o;
} }
ostream& operator<< (ostream&o, ccr::bvector v)
{
o << "vector of " << v.size() << " elements:" << endl;
for (uint i = 0, e = v.size(); i < e; ++i) cout << v[i];
cout << endl;
return o;
}
class primitiverng : public ccr::prng class primitiverng : public ccr::prng
{ {
public: public:
@ -61,7 +69,7 @@ int main()
ccr::mce::privkey priv; ccr::mce::privkey priv;
ccr::mce::pubkey pub; ccr::mce::pubkey pub;
ccr::mce::generate (pub, priv, r, 9, 15); ccr::mce::generate (pub, priv, r, 9, 9);
cout << "PRIVATE KEY" << endl; cout << "PRIVATE KEY" << endl;
cout << priv.fld; cout << priv.fld;
@ -73,6 +81,28 @@ int main()
cout << pub.t << endl; cout << pub.t << endl;
cout << pub.G; cout << pub.G;
ccr::bvector plain;
plain.resize (pub.plain_size() );
plain[0] = 1;
plain[1] = 1;
plain[2] = 1;
cout << "PLAINTEXT" << endl;
cout << plain;
ccr::bvector cipher;
pub.encrypt (plain, cipher, r);
cout << "CIPHERTEXT" << endl;
cout << cipher;
priv.prepare();
ccr::bvector result;
priv.decrypt (cipher, result);
cout << "DECRYPTED" << endl;
cout << result;
return 0; return 0;
} }