Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include "AffineCipher.h"
- // #decrypt()
- int AffineCipher::decrypt
- (
- const vector<byte>& cipher_text,
- const vector<byte>& key,
- vector<byte>& plain_text)
- {
- if(multInverse(key[0])==0 || (int)key[1]>25){
- cout<<"wrong key"<<endl;
- return 0;
- }
- for(vector<byte>::const_iterator it=cipher_text.begin(); it != cipher_text.end();++it){
- plain_text.push_back(multInverse(key[0])*((*it)-key[1])%26);
- }
- return plain_text.size();
- } // decrypt()
- // #encrypt()
- int AffineCipher::encrypt
- (
- const vector<byte>& plain_text,
- const vector<byte>& key,
- vector<byte>& cipher_text
- )
- {
- if(multInverse(key[0])==0 || (int)key[1]>25){
- cout<<"wrong key"<<endl;
- return 0;
- }
- for(vector<byte>::const_iterator it=plain_text.begin(); it != plain_text.end();++it){
- cipher_text.push_back((key[0]*(*it)+key[1]) % 26);
- }
- return cipher_text.size();
- //ax+b mod 26
- } // encrypt()
- // #multInverse()
- int AffineCipher::multInverse(byte a) const {
- if (a<26) {
- return inverse_table[a];
- } // if
- else {
- return 0;
- } // else
- } // multInverse()
- // #inverse_table
- const byte AffineCipher::inverse_table[26] = {
- 0, // 0
- 1, // 1
- 0, // 2
- 9, // 3
- 0, // 4
- 21, // 5
- 0, // 6
- 15, // 7
- 0, // 8
- 3, // 9
- 0, // 10
- 19, // 11
- 0, // 12
- 0, // 13
- 0, // 14
- 7, // 15
- 0, // 16
- 23, // 17
- 0, // 18
- 11, // 19
- 0, // 20
- 5, // 21
- 0, // 22
- 17, // 23
- 0, // 24
- 25 // 25
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement