164 lines
3.7 KiB
C++
Executable File
164 lines
3.7 KiB
C++
Executable File
/*
|
|
* This file is a part of Tito - a cryptography library
|
|
* and is distributed under the (new) BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2012, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
* project may be used to endorse or promote products derived
|
|
* from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "crypto.h"
|
|
|
|
// !! FIX ME
|
|
//#include "log.h"
|
|
|
|
#include "utf8/utf8.h"
|
|
#include "misc.h"
|
|
|
|
|
|
namespace Tito
|
|
{
|
|
|
|
|
|
|
|
bool Crypto::SetAESKey(unsigned char aes_key[32])
|
|
{
|
|
if( !aes.Key(aes_key, 32) )
|
|
{
|
|
//log << log1 << "problem with AES key" << logend;
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
void Crypto::Crypt(const std::wstring & in, std::wstring & out)
|
|
{
|
|
PT::WideToUTF8(in, utf8_str);
|
|
aes.Encode((const unsigned char*)utf8_str.c_str(), utf8_str.size(), aes_name, uint8_tab);
|
|
|
|
base64_str.clear();
|
|
|
|
if( !uint8_tab.empty() )
|
|
base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), base64_str);
|
|
|
|
AssignString(base64_str, out);
|
|
Clear();
|
|
}
|
|
|
|
|
|
void Crypto::Crypt(const std::vector<char> & in, std::string & out)
|
|
{
|
|
aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab);
|
|
base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out);
|
|
Clear();
|
|
}
|
|
|
|
|
|
|
|
bool Crypto::Decrypt(const std::wstring & in, std::wstring & out)
|
|
{
|
|
out.clear();
|
|
|
|
if( in.empty() )
|
|
return true;
|
|
|
|
AssignString(in, base64_str);
|
|
base64.Decode(base64_str, aes_str);
|
|
|
|
if( aes.Decode((const unsigned char *)aes_str.c_str(), aes_str.size(), aes_tmp_name, uint8_tab) )
|
|
{
|
|
if( !uint8_tab.empty() )
|
|
PT::UTF8ToWide((const char*)&uint8_tab[0], uint8_tab.size(), out);
|
|
|
|
Clear();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
//log << "problem with AES decoding (skipping)" << logend;
|
|
}
|
|
|
|
Clear();
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool Crypto::Decrypt(const std::string & in, std::vector<char> & out)
|
|
{
|
|
out.clear();
|
|
|
|
if( in.empty() )
|
|
return true;
|
|
|
|
base64.Decode(in, aes_str);
|
|
|
|
if( aes.Decode((const unsigned char *)aes_str.c_str(), aes_str.size(), aes_tmp_name, uint8_tab) )
|
|
{
|
|
out.resize(uint8_tab.size());
|
|
|
|
for(size_t i=0 ; i<uint8_tab.size() ; ++i)
|
|
out[i] = uint8_tab[i];
|
|
|
|
Clear();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
//log << "problem with AES decoding (skipping)" << logend;
|
|
}
|
|
|
|
Clear();
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
void Crypto::Clear()
|
|
{
|
|
Clear(uint8_tab);
|
|
Clear(aes_name);
|
|
Clear(aes_tmp_name);
|
|
Clear(utf8_str);
|
|
Clear(base64_str);
|
|
Clear(aes_str);
|
|
}
|
|
|
|
|
|
|
|
} // namespace Tito
|
|
|
|
|