aboutsummaryrefslogtreecommitdiff
path: root/envelope.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'envelope.cpp')
-rw-r--r--envelope.cpp31
1 files changed, 16 insertions, 15 deletions
diff --git a/envelope.cpp b/envelope.cpp
index 9bb011e..4674121 100644
--- a/envelope.cpp
+++ b/envelope.cpp
@@ -24,28 +24,25 @@ namespace Envelope{
do {
for(int i=0;i<keylen;i++)*(uint32_t*)&key[4*i]=crng.get();
} while(!safeKey(key));
- //cerr<<"WARNING: using predetermined envelope key"<<endl;
- //key="kaasKAASkaasKAAShalloHALLOhallo!";
-
- cerr<<"decrkey="<<Base64::encode(key)<<endl;
+ // cerr<<"decrkey="<<Base64::encode(key)<<endl;
string payload(AES::encrypt(data,key,AES::AES_256_CBC));
- cerr<<"payload="<<Base64::encode(payload)<<endl;
+ // cerr<<"payload="<<Base64::encode(payload)<<endl;
Bigint rsadata;
for(int i=0;i<(int)key.size();i++){
if(i!=0)rsadata<<=8;
rsadata+=(uint8_t)key[i];
}
- cerr<<"rsadata="<<rsadata<<endl;
- // cerr<<"We encrypt:"<<endl<<" "<<rsadata<<endl;
+ // cerr<<"rsadata="<<rsadata<<endl;
Bigint res(RSA::encrypt(rsadata,pubkey));
- // cerr<<"to:"<<endl<<" "<<res<<endl;
+
vector<uint8_t> bytes; //bytes in little-endian order
while(res!=0){
bytes.push_back(res.lowdigits()&0xff);
res>>=8;
}
- cerr<<"encrkey="<<Base64::encode(string(bytes.rbegin(),bytes.rend()))<<endl;
+ // cerr<<"encrkey="<<Base64::encode(string(bytes.rbegin(),bytes.rend()))<<endl;
+
payload.reserve(payload.size()+bytes.size()+2);
for(int i=bytes.size()-1;i>=0;i--)payload.push_back(bytes[i]);
payload.push_back(bytes.size()>>8);
@@ -55,24 +52,27 @@ namespace Envelope{
}
string decrypt(const string &data,const RSA::PrivateKey &privkey){
- cerr<<"=== DECRYPT ==="<<endl;
+ // cerr<<"=== DECRYPT ==="<<endl;
if(data.size()<2)throw invalid_argument("Envelope data length invalid");
int encrkeylen=((uint16_t)(uint8_t)data[data.size()-2]<<8)+(uint8_t)data.back();
assert(encrkeylen<(1<<16));
- cerr<<"encrkeylen="<<encrkeylen<<endl;
+ // cerr<<"encrkeylen="<<encrkeylen<<endl;
if((int)data.size()<encrkeylen+2)throw invalid_argument("Envelope key format invalid");
+
string encrkey(encrkeylen,'\0');
for(int i=0;i<encrkeylen;i++){
encrkey[i]=data[data.size()-2-encrkeylen+i];
}
- cerr<<"encrkey="<<Base64::encode(encrkey)<<endl;
+ // cerr<<"encrkey="<<Base64::encode(encrkey)<<endl;
+
Bigint rsadata;
for(int i=0;i<encrkeylen;i++){
if(i!=0)rsadata<<=8;
rsadata+=(uint8_t)encrkey[i];
}
Bigint res(RSA::decrypt(rsadata,privkey));
- cerr<<"rsadata="<<res<<endl;
+ // cerr<<"rsadata="<<res<<endl;
+
vector<uint8_t> bytes; //bytes in little-endian order
while(res!=0){
bytes.push_back(res.lowdigits()&0xff);
@@ -80,8 +80,9 @@ namespace Envelope{
}
string decrkey(bytes.size(),'\0');
for(int i=0;i<(int)bytes.size();i++)decrkey[bytes.size()-1-i]=bytes[i];
- cerr<<"decrkey="<<Base64::encode(decrkey)<<endl;
- cerr<<"payload="<<Base64::encode(data.substr(0,data.size()-2-encrkeylen))<<endl;
+ // cerr<<"decrkey="<<Base64::encode(decrkey)<<endl;
+
+ // cerr<<"payload="<<Base64::encode(data.substr(0,data.size()-2-encrkeylen))<<endl;
return AES::decrypt(data.substr(0,data.size()-2-encrkeylen),decrkey,AES::AES_256_CBC);
}