Professional Documents
Culture Documents
#include <bitset>
#include <string>
using namespace std;
bitset<48> final_keys[17];
int IP[]={58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7};
UploadLibrary
BooksAudiobooksComicsSheet Music
Don't want to upload?
Get unlimited access as a member
Sign up now
Once you upload an approved document, you will be able to read and download this
document
Manual LFA
nicutds
Upload a document for free access.
Select files from your computer or choose other ways to upload below.
Select files to upload
For multiple files hold down the shift key when selecting.
Drag files here from your computer.
Supported File Types: pdf, txt, ps, rtf, epub, key, odt, odp, ods, odg, odf, sxw
, sxc, sxi, sxd, doc, ppt, pps, xls, docx, pptx, ppsx, xlsx
By uploading, you agree to the Scribd Uploader Agreement.
Add files from Google Drive, Gmail, Dropbox and more with FilePicker.io.
Add files from multiple cloud and file storage services.
Pick Files
More reasons to publish on Scribd
Scribd's platform is designed to help you easily publish your content on the web
and mobile devices, distribute it to a wide and global audience, and potentiall
y make money from selling that content. Here's more on the benefits of publishin
g on Scribd.
Reach Scribd's audience of 90 million monthly readers.
By publishing on Scribd, your content can be seen by up to 90 million people
from all around the world who use Scribd.
Turn your content into a beautiful HTML5 webpage, that you can even embed on
another website.
Scribd's patent-pending conversion technology instantly turns documents into
beautifully formatted webpages.
Get your content indexed by Google and other search engines.
Scribd does SEO for you. Every word of your content will be fully indexed by
Supported File Types: pdf, txt, ps, rtf, epub, key, odt, odp, ods, odg, odf, sxw
, sxc, sxi, sxd, doc, ppt, pps, xls, docx, pptx, ppsx, xlsx
By uploading, you agree to the Scribd Uploader Agreement.
About
Browse books
Browse documents
About Scribd
Meet the team
Our blog
Join our team!
Contact Us
Support
Help
FAQ
Press
Purchase help
AdChoices
Partners
Publishers
Developers / API
Legal
Terms
Privacy
Copyright
Memberships
Join today
Invite Friends
Gifts
Stay Connected
Scribd on Appstore
Scribd on Google Play
Copyright 2015 Scribd Inc. .Terms of service.Privacy.Mobile Site.Site Language:E
nglish
scribd
int IP_1[]={40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25};
int E[]={32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
4, 2, 8,
15, 1,
14, 7,
2, 12};
bitset<4> after_S_boxes[8];
for (int i=0;i<48;i++)//Reprezentam stringul de la operatia de mai sus c
a concatenare a 8 siruri a cate 6 biti
before_S_boxes[7-i/6][5-i%6]=R_expanded[47-i];
for (int i=0;i<8;i++){//8 siruri cate 6 biti -> 8 siruri cate 4 biti cu
valori din S-boxes
bitset<2> linia;
linia[0]=before_S_boxes[7-i][0];
linia[1]=before_S_boxes[7-i][5];
bitset<4> coloana;
coloana[0]=before_S_boxes[7-i][1];
coloana[1]=before_S_boxes[7-i][2];
coloana[2]=before_S_boxes[7-i][3];
coloana[3]=before_S_boxes[7-i][4];
long line=linia.to_ulong(),column=coloana.to_ulong(),valoare_din
_box;
valoare_din_box=S_box_value(line,column,i);
after_S_boxes[7-i]=bitset<4>(valoare_din_box);
}
bitset<32> before_P;
for (int i=0;i<32;i++){
before_P[31-i]=after_S_boxes[7-i/4][3-i%4];
}
bitset<32> to_be_returned;
for (int i=0;i<32;i++){//Aplicam permutarile finale din tabelul P si pri
mim f(R_i-1,K_i)
to_be_returned[31-i]=before_P[31-(P[i]-1)];
}
return to_be_returned;
}
bitset<32> R_i_formare(bitset<32> R_precedent,bitset<32> L_precedent,int runda,b
itset<48> final_keys){//L_i-1 XOR f(R_i-1,K_i)
return (L_precedent^functia(R_precedent,runda,final_keys));
}
bitset<64> criptare_decriptare(bitset<64> sirul_biti,bool criptare,bitset<48> fi
nal_keys)
{
bitset<64>text_dupa_IP;
for (int i=0;i<64;i++){
text_dupa_IP[i]=sirul_biti[IP[i]-1];
}
bitset<32> L;
bitset<32> R;
//Impartim textul in L si R
for (int i=0;i<32;i++){
R[i]=text_dupa_IP[i];
L[i]=text_dupa_IP[i+32];
}
//Incep rundele de criptare
if(criptare==1)
for (int i=1;i<=16;i++){
bitset<32> L_temporar=L;
L=R;
R=R_i_formare(R,L_temporar,i,final_keys);
}
else
for (int i=16;i>=1;i--){
bitset<32> L_temporar=L;
L=R;
R=R_i_formare(R,L_temporar,i,final_keys);
}
bitset<64> criptotext_temporal;
for (int i=0;i<32;i++){//formam criptotext pentru IP^(-1) din do
ua jumatati criptate in 16 runde
criptotext_temporal[i]=L[i];
criptotext_temporal[i+32]=R[i];
}
bitset<64>criptotext;
for (int i=0;i<64;i++){//Aplicam tabelul de permutari, inversul
tabelei IP
criptotext[63-i]=criptotext_temporal[63-(IP_1[i]-1)];
}
return criptotext;
}
int main(){
//string text_initial;
bitset<64> text(0x0123456789ABCDEF);
bitset<64> key(0x11114444555598AF);
cout<<"Textul initial in binar este : "<<text<<endl;
//------------------------------------------------------generarea cheilo
r--------------------------------------------------------------//Aplicam cheii permutari din PC1 eliminad astfel si bitii de paritate
bitset<56>key_after_PC1;
for (int i=0;i<56;i++){
key_after_PC1[55-i]=key[63-(PC1[i]-1)];
}
//Impartim cheia in C_0 si D_0
bitset<28> C_0,D_0;
for (int i=0;i<28;i++){
C_0[i]=key_after_PC1[i];
D_0[i]=key_after_PC1[i+28];
}
//Generam cheile pentru 16 runde de criptare
for (int i=1;i<17;i++){
bitset<28> C,D;
bitset<56> temporary_key;
if (i==1 || i==2 || i==9 || i==16){//pentru i=1,2,9,16 siftam bi
tii din C si D cu o pozitie
C=circular_left_shift_one(C_0);
D=circular_left_shift_one(D_0);
C_0=C;
D_0=D;
for (int j=0;j<28;j++){
temporary_key[j]=C[j];
temporary_key[j+28]=D[j];
}
for (int j=0;j<48;j++){//Aplicam permutarile din tabelul
PC2 si plasam cheia in tabelul final de chei
final_keys[i][47-j]=temporary_key[55-(PC2[j]-1)]
;
}
}
else{//pentru toti ceilalti i siftam bitii din C si D cu doua po
zitii
C=circular_left_shift_two(C_0);
D=circular_left_shift_two(D_0);
C_0=C;
D_0=D;
for (int j=0;j<28;j++){
temporary_key[j]=C[j];
temporary_key[j+28]=D[j];
}
for (int j=0;j<48;j++){//Aplicam permutarile din tabelul
PC2 si plasam cheia in tabelul final de chei
final_keys[i][47-j]=temporary_key[55-(PC2[j]-1)]
;
}
}
}
//-------------------------criptare------------------------------------------------//pasul cu aplicarea tabelului de permutari IP pentru sirul de text de 6
4 de biti
bitset<64>criptotext=criptare_decriptare(text,1,*final_keys);
cout<<"Criptotextul este : "<<criptotext<<endl;
//------------------------------------decriptare--------------------------------------------------------//pasul cu aplicarea tabelului de permutari IP pentru sirul de criptotex
t de 64 de biti
bitset<64>text_decriptat=criptare_decriptare(criptotext,0,*final_keys);
cout<<"Textul decriptat in binar este : "<<text_decriptat<<endl;
std::cin.get();
}