aboutsummaryrefslogtreecommitdiff
path: root/envelope.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'envelope.cpp')
-rw-r--r--envelope.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/envelope.cpp b/envelope.cpp
index 4674121..5270dae 100644
--- a/envelope.cpp
+++ b/envelope.cpp
@@ -17,24 +17,25 @@ namespace Envelope{
return true; //fine
}
- string encrypt(const string &data,const RSA::PublicKey &pubkey){
+ string encrypt(const string &data,const RSA::Key &pubkey){
const int keylen=8; //256-bit
CryptoRng crng;
- string key(4*keylen,'\0');
+ string aeskey(4*keylen,'\0');
do {
- for(int i=0;i<keylen;i++)*(uint32_t*)&key[4*i]=crng.get();
- } while(!safeKey(key));
- // cerr<<"decrkey="<<Base64::encode(key)<<endl;
+ for(int i=0;i<keylen;i++)*(uint32_t*)&aeskey[4*i]=crng.get();
+ } while(!safeKey(aeskey));
+ // cerr<<"decrkey="<<Base64::encode(aeskey)<<endl;
- string payload(AES::encrypt(data,key,AES::AES_256_CBC));
+ string payload(AES::encrypt(data,aeskey,AES::AES_256_CBC));
// cerr<<"payload="<<Base64::encode(payload)<<endl;
Bigint rsadata;
- for(int i=0;i<(int)key.size();i++){
+ for(int i=0;i<(int)aeskey.size();i++){
if(i!=0)rsadata<<=8;
- rsadata+=(uint8_t)key[i];
+ rsadata+=(uint8_t)aeskey[i];
}
// cerr<<"rsadata="<<rsadata<<endl;
Bigint res(RSA::encrypt(rsadata,pubkey));
+ // cerr<<"rsaencr="<<res<<endl;
vector<uint8_t> bytes; //bytes in little-endian order
while(res!=0){
@@ -44,14 +45,14 @@ namespace Envelope{
// 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]);
+ for(int i=bytes.size()-1;i>=0;i--)payload.push_back(bytes[i]); //append in big-endian order
payload.push_back(bytes.size()>>8);
payload.push_back((uint8_t)bytes.size()&0xff);
return payload;
}
- string decrypt(const string &data,const RSA::PrivateKey &privkey){
+ string decrypt(const string &data,const RSA::Key &privkey){
// 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();
@@ -59,7 +60,7 @@ namespace Envelope{
// cerr<<"encrkeylen="<<encrkeylen<<endl;
if((int)data.size()<encrkeylen+2)throw invalid_argument("Envelope key format invalid");
- string encrkey(encrkeylen,'\0');
+ string encrkey(encrkeylen,'\0'); //in big-endian
for(int i=0;i<encrkeylen;i++){
encrkey[i]=data[data.size()-2-encrkeylen+i];
}
@@ -70,6 +71,7 @@ namespace Envelope{
if(i!=0)rsadata<<=8;
rsadata+=(uint8_t)encrkey[i];
}
+ // cerr<<"rsaencr="<<rsadata<<endl;
Bigint res(RSA::decrypt(rsadata,privkey));
// cerr<<"rsadata="<<res<<endl;