You are on page 1of 8

#include <iostream>

#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

all major search engines.


Make your content social and get distribution on Facebook, Twitter, and othe
r social networks.
Social networking gets bigger every year, and even written documents now nee
d to be social. Scribd socially optimizes all content to maximize social distrib
ution.
Make money by selling your content.
If you choose to, you can set a price for your content and make it for sale
through a simple link, and the earnings go right to your account.
Make your content readable on iPhone, iPad, Android, and other mobile device
s.
Scribd makes all content easily readable on the mobile web, and provides an
even better reading experience for people who download Scribd's apps.
Get detailed stats on readership of your content.
Scribd Stats gives you all the analytics you need on who is reading your con
tent.
What's published on Scribd?
Scribd supports just about any kind of written content. Here are a few examples
of the types of things we have seen published on Scribd.
Creative writing
Recipes
How-to-guides
Books
Presentations
School papers
Spreadsheets
Original essays
Travel guides
Legal documents
Business forms
Sheet music
Study guides
Academic papers
Poetry
Catalogs
Speeches
Letters
Historical documents
Scientific data
Infographics
Source documents
Magazines
Newspapers
Comics
Resumes
Get Started.
Select files from your computer or simply drag and drop.
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.
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,

20, 21, 22, 23, 24, 25,


24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1};
int S1[]={14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13};
int S2[]={15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9};
int S3[]={10, 0, 9, 14, 6, 3, 15,
13, 7, 0, 9, 3, 4, 6, 10,
13, 6, 4, 9, 8, 15, 3, 0,
1, 10, 13, 0, 6, 9, 8, 7,

5, 1, 13, 12, 7, 11,


2, 8, 5, 14, 12, 11,
11, 1, 2, 12, 5, 10,
4, 15, 14, 3, 11, 5,

4, 2, 8,
15, 1,
14, 7,
2, 12};

int S4[]={7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,


13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14};
int S5[]={2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0,

15, 13, 0, 14, 9,


10, 3, 9, 8, 6,
5, 6, 3, 0, 14,
9, 10, 4, 5, 3};

int S6[]={12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,


10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13};
int S7[]={4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12};
int S8[]={13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11};
int P[]={16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25};
int PC1[]={57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,

21, 13, 5, 28, 20, 12, 4};


int PC2[]={14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32};
bitset<28> circular_left_shift_one(bitset<28> sirul_de_biti){
bitset<28> temporal=sirul_de_biti;
for (int i=0;i<27;i++){
sirul_de_biti[i+1]=temporal[i];
}
sirul_de_biti[0]=temporal[27];
return sirul_de_biti;
}
bitset<28> circular_left_shift_two(bitset<28> sirul_de_biti){
bitset<28> temporal=sirul_de_biti;
for (int i=0;i<26;i++){
sirul_de_biti[i+2]=temporal[i];
}
sirul_de_biti[1]=temporal[27];
sirul_de_biti[0]=temporal[26];
return sirul_de_biti;
}
long S_box_value(long line,long column,int nr_cutie){
if ((nr_cutie+1)==1)
return (S1[line*16+column]);
if ((nr_cutie+1)==2)
return (S2[line*16+column]);
if ((nr_cutie+1)==3)
return (S3[line*16+column]);
if ((nr_cutie+1)==4)
return (S4[line*16+column]);
if ((nr_cutie+1)==5)
return (S5[line*16+column]);
if ((nr_cutie+1)==6)
return (S6[line*16+column]);
if ((nr_cutie+1)==7)
return (S7[line*16+column]);
if ((nr_cutie+1)==8)
return (S8[line*16+column]);
}
bitset<32> functia(bitset<32> R,int runda_de_cheie,bitset<48> final_keys){//f(R_
i-1,K_i)
bitset<48> R_expanded;
for (int i=0;i<48;i++){//Aplicam sirului de 32 biti R functia de extensi
e din tabelul E
R_expanded[47-i]=R[31-(E[i]-1)];
}
R_expanded^=(&final_keys)[runda_de_cheie];//R extins XOR cheia rundei cu
rente
bitset<6> before_S_boxes[8];

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();
}

You might also like