Beispiel #1
0
Blob
RsaAlgorithm::encrypt
  (const Blob& keyBits, const Blob& plainData, const EncryptParams& params)
{
  RsaPublicKeyLite publicKey;
  if (publicKey.decode(keyBits) != NDN_ERROR_success)
    throw UnrecognizedKeyFormatException
      ("RsaAlgorithm: Error decoding public key");

  // TODO: use RSA_size, etc. to get the proper size of the output buffer.
  ptr_lib::shared_ptr<vector<uint8_t> > encryptedData(new vector<uint8_t>(1000));
  size_t encryptedDataLength;
  ndn_Error error;
  if ((error = publicKey.encrypt
       (plainData, params.getAlgorithmType(), &encryptedData->front(),
        encryptedDataLength))) {
    if (error == NDN_ERROR_Unsupported_algorithm_type)
      throw runtime_error("RsaAlgorithm: Unsupported padding scheme");
    else
      throw SecurityException(string("RsaAlgorithm: ") + ndn_getErrorString(error));
  }

  encryptedData->resize(encryptedDataLength);
  return Blob(encryptedData, false);
}
        //--------------------------------------------------------------
        //--------------------------------------------------------------
		void AppDataStore::RefreshFromFile()
        {
            FileSystem* pFileSystem = Application::Get()->GetFileSystem();
            if(pFileSystem->DoesFileExist(StorageLocation::k_saveData, k_filename) == true)
            {
				FileStreamSPtr fileStream = pFileSystem->CreateFileStream(StorageLocation::k_saveData, k_filename, FileMode::k_readBinary);
				if (fileStream != nullptr)
                {
					fileStream->SeekG(0, SeekDir::k_end);
					u32 encryptedDataSize = fileStream->TellG();
					fileStream->SeekG(0, SeekDir::k_beginning);
                    
                    std::unique_ptr<s8[]> encryptedData(new s8[encryptedDataSize]);
					fileStream->Read(encryptedData.get(), encryptedDataSize);
					fileStream.reset();
                    
                    std::string decrypted = AESEncrypt::DecryptString(reinterpret_cast<const u8*>(encryptedData.get()), encryptedDataSize, k_privateKey);

                    XMLUPtr xml = XMLUtils::ParseDocument(decrypted);
                    XML::Node* root = XMLUtils::GetFirstChildElement(xml->GetDocument());
                    if(nullptr != root)
                    {
                        m_dictionary = ParamDictionarySerialiser::FromXml(root);
                    }
                }
            }
            
            m_needsSynchonised = false;
        }