Шифрование файлов. Переписать с С++ - C#
Формулировка задачи:
Всем добрый день !
Братья программисты, помогите новичку в освоении шифрования данных
на просторах этого сайта я нашел код написанный на c++
помогите мне его перевести или расскажите как написать новый на c#
буду очень благодарен !
char CodChar(char ch)
{
return ~(((ch & 0xE0) >> 1) | ((ch & 0x10)<<3) | ((ch & 0x8)>>3) | ((ch & 0x07) <<1));
}
char EnCodChar(char ch)
{
return ~(((ch & 0x70) << 1) | ((ch & 0x80)>>3) | ((ch & 0x1)<<3) | ((ch & 0xE) >>1));
}
void foo(const char * inputfile,const char * outputfile,bool encode=false)
{
fstream file(inputfile,ios::in | ios::binary);
file.seekg(0,std::ios::end);
vector<char> vec(file.tellp().seekpos());
file.seekg(0,ios::beg);
file.read(&vec[0],vec.size());
file.close();
file.open(outputfile,ios::out | ios::binary);
transform(vec.begin(),vec.end(),vec.begin(),encode?EnCodChar:CodChar);
file.write(&vec[0],vec.size());
}Решение задачи: «Шифрование файлов. Переписать с С++»
textual
Листинг программы
static void foo(string inputfile, string outputfile, bool encode=false)
{
byte[] buf = File.ReadAllBytes(inputfile);
if (encode)
{
for (int i=0; i<buf.Length; i++)
{
byte ch = buf[i];
buf[i] = (byte)~(((ch & 0x70) << 1) | ((ch & 0x80)>>3) | ((ch & 0x1)<<3) | ((ch & 0xE) >>1));
}
}
else
{
for (int i=0; i<buf.Length; i++)
{
byte ch = buf[i];
buf[i] = (byte)~(((ch & 0xE0) >> 1) | ((ch & 0x10)<<3) | ((ch & 0x8)>>3) | ((ch & 0x07) <<1));
}
}
File.WriteAllBytes(outputfile, buf);
}