DALGLI0/Program.c

179 lines
4.6 KiB
C
Raw Normal View History

2018-06-30 11:52:36 +02:00
#include <iostream>
#include <string>
#include <sstream>
#include<unistd.h>
using namespace std;
#define POLY 0x1021
string t;
int a,e,c;
unsigned short EncodeCRT(char *ptr, int count)
{
int crc;
char i;
crc = 0xffff;
while (--count >= 0)
{
crc = crc ^ (int) *ptr++ << 8;
i = 8;
do
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
} while(--i);
}
return (crc);
}
string CheckCrc(string message)
{
string crc="1000000100001";
for(int i=0;i<=message.length()-crc.length();)
{
for(int j=0;j<crc.length();j++)
message[i+j]=message[i+j]==crc[j]? '0' : '1';
for( ; i<message.length() && message[i]!='1';i++);
}
for(char i: message.substr(message.length()-crc.length()))
if(i!='0')
{
return "FALSE";
}
return "TRUE";
}
const char* hex_char_to_bin(char c)
{
// TODO handle default / error
switch(toupper(c))
{
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case 'A': return "1010";
case 'B': return "1011";
case 'C': return "1100";
case 'D': return "1101";
case 'E': return "1110";
case 'F': return "1111";
}
}
std::string hexToBin(string &hex)
{
// TODO use a loop from <algorithm> or smth
std::string bin;
for(unsigned i = 0; i != hex.length(); ++i)
bin += hex_char_to_bin(hex[i]);
return bin;
}
void two(string &in)
{
}
int main()
{
char* input;
cout<<("1. Zakoduj wiadomosc");
cout<<("2. Odkoduj wiadomosc");
cout<<("3. Wyjscie");
int choice=0;
cin>>choice;
cin.ignore();
if(choice==1)
{
int cc=0;
cout<<("Podaj dlugosc wiadomosci.(ilosc liter ze spacjami wlacznie)");
cin>>cc;
input=new char[cc];
cout<<("Podaj wiadomosc do zaszyfrowania.");
cin.ignore();
cin.getline(input,cc+1);
unsigned short result =EncodeCRT(input,cc);
for(int i=0;i<cc;i++){
printf("%x",input[i]);
printf(" ");
}
printf("%x",result);
printf(" ");
}
if(choice==2)
{
string input3;
cout<<"Podaj wiadomosc do odkodowania w systemie szesnastkowym, bez spacji.";
cin>>input3;
string xd;
std::stringstream ss;
std::string bin;
string hex=input3;
for(unsigned i = 0; i != hex.length(); ++i)
{
switch(toupper(hex[i]))
{
case '0': bin+= "0000"; break;
case '1': bin+= "0001"; break;
case '2': bin+= "0010"; break;
case '3': bin+= "0011"; break;
case '4': bin+= "0100";break;
case '5': bin+= "0101";break;
case '6': bin+= "0110";break;
case '7': bin+= "0111";break;
case '8': bin+= "1000";break;
case '9': bin+= "1001";break;
case 'A': bin+= "1010";break;
case 'B': bin+= "1011";break;
case 'C': bin+= "1100";break;
case 'D': bin+= "1101";break;
case 'E': bin+= "1110";break;
case 'F': bin+= "1111";break;
}
}
string crc="1000000100001";
string message=bin;
int l=message.length();
int cl=crc.length();
int ii=0;
int k=0;
l=message.length();
while(true)
{
if(ii>l-cl) break;
for(int j=0;j<cl;j++)
{
message[ii+j]=message[ii+j]==crc[j]? '0' : '1';
}
while(ii<l && message[ii]!='1')
{
ii++;
}
}
string x=message.substr(message.length()-crc.length());
cout<<x;
for(char i: x)
if(i=='1')
{
cout<<"FALSE";
return 0;
}
cout<<"TRUE";
return 0;
}
if(choice==3)
{
return 0;
}
return 0;
}