Commit 2b651e0f authored by yiwenshao's avatar yiwenshao

modify ASHE

parent bcef3229
#include"crypto/ASHE.hh" #include"crypto/ASHE.hh"
const unsigned long long ASHE::ASHE_MAX = 0xffffffffffffffff; const unsigned long ASHE::ASHE_MAX = 0xffffffffffffffff;
ASHE::ASHE(std::string s,int i):key(s),bf(s),IV(i){ ASHE::ASHE(std::string s,int i):key(s),bf(s),IV(i){
} }
long ASHE::encrypt(unsigned long long plaintext){ std::pair<long,uint64_t> ASHE::encrypt(unsigned long plaintext){
return (plaintext - bf.encrypt(IV) + bf.encrypt(IV-1))%ASHE_MAX; return std::make_pair((plaintext - bf.encrypt(IV) + bf.encrypt(IV-1))%ASHE_MAX,IV);
} }
unsigned long long ASHE::decrypt(long ciphertext){ unsigned long ASHE::decrypt(long ciphertext){
return (ciphertext + bf.encrypt(IV) - bf.encrypt(IV-1))%ASHE_MAX; return (ciphertext + bf.encrypt(IV) - bf.encrypt(IV-1))%ASHE_MAX;
} }
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
#include <string> #include <string>
#include "crypto/blowfish.hh" #include "crypto/blowfish.hh"
class ASHE{ class ASHE{
static const unsigned long long ASHE_MAX; static const unsigned long ASHE_MAX;
std::string key; std::string key;
blowfish bf; blowfish bf;
uint64_t IV; uint64_t IV;
public: public:
ASHE(std::string s,int i); ASHE(std::string s,int i);
long encrypt(unsigned long long plaintext); std::pair<long,uint64_t> encrypt(unsigned long plaintext);
int getIV(); int getIV();
unsigned long long decrypt(long ciphertext); unsigned long decrypt(long ciphertext);
}; };
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
int main(){ int main(){
std::vector<unsigned long long > plain{1u,2u,3u,4u,5u,6u,7u,8u,9u,10u}; std::vector<unsigned long long > plain{1u,2u,3u,4u,5u,6u,7u,8u,9u,10u};
std::vector<long> enc; std::vector<std::pair<long,uint64_t>> enc;
std::vector<ASHE*> ass; std::vector<ASHE*> ass;
for(auto item:plain){ for(auto item:plain){
uint64_t IV = randomValue(); uint64_t IV = randomValue();
...@@ -15,7 +15,7 @@ int main(){ ...@@ -15,7 +15,7 @@ int main(){
} }
std::cout<<"encs:plains"<<std::endl; std::cout<<"encs:plains"<<std::endl;
for(auto i=0u;i<enc.size();++i){ for(auto i=0u;i<enc.size();++i){
std::cout<<"enc:"<<enc[i]<<"dec:"<<ass[i]->decrypt(enc[i])<<std::endl; std::cout<<"enc:"<<enc[i].first<<"dec:"<<ass[i]->decrypt(enc[i].first)<<std::endl;
} }
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment