Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Using Crypto++ library with Windows Phone 8

From Wiki
Jump to: navigation, search
Featured Article
12 Jan

This article explains how to use Crypto++, a C++ library that offers various cryptographic schemes on the Windows Phone platform.

WP Metro Icon Tools.png
WP Metro Icon WP8.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 8.0 SDK
Devices(s): Nokia Lumia 820
Windows Phone 8
Created: vineet.jain (19 Dec 2013)
Last edited: vineet.jain (11 Oct 2014)



There are several C# APIs which can be used by Windows Phone apps to encrypt and decrypt data. If you wish to use C++ instead, one alternative is the Crypto++ library by Wei Dai. Crypto++ implements a large number of cryptographic algorithms, provides support for different compilers such as GCC, C++, and MSVC, and has a permissive license.

This article provides a code example which uses the Crypto++ library to encrypt and decrypt a String. The Crypto++ library can be used in many other ways, for example, to encrypt/decrypt files (irrespective of their formats).


To use the Crypto++ library within our Windows Phone project, we first need to build it. You can download the library project from the Crypto++ homepage.

  • Go to the Download section on Crypto++
  • Download Crypto++ 5.6.2

Building the Crypto++ library project

  1. Unzip the downloaded folder (cryptopp562)
  2. Locate the Visual Studio solution file cryptest.sln and open it with VS2012 for Windows Phone
  3. Our solution involves four projects.[1]: cryptest, cryptlib, cryptopp and dlltest.
  4. Since Crypto++ is built with an older version of Visual Studio, Visual Studio will ask to convert all four projects to the current version.
  5. All four projects have different usage. We want to build a static library (.lib file), so we only need to build the Cryptlib project.
  6. You need to set some important project properties. To do this, right-click on the Cryptlib project and go to its properties. Update them as shown in the following screenshots.

To summarize the above properties:

  • Configuration Properties->General->Platform Toolset - Visual Studio 2012(v110)
  • Configuration Properties->General->Configuration Type - Static library(.lib)
  • Configuration Properties->C/C++->Code generation->Runtime library - Multi-threaded Debug (/MTd) (for Debug Config & Multi-threaded DLL (/MD) for Release Config)

Building for different configurations

To build the Cryptlib project for the emulator, select the Win32 target. To build the project for a device, select the ARM target.

Since the ARM target isn't in the platforms list by default, you must add it as follows:

  1. Right-click Project->Properties->Configuration Manager (upper right side of the window)
  2. Select Active solution platform (dropdown list) ->New
  3. Choose ARM (as new platform) and leave 'Copy Settings from' empty

This will add the ARM platform to the project.

Resolving problems that arise while building the library project

For ARM(device build) configuration :

When building the Cryptlib project for the first time, several build errors come up, shown in the image below:

Build Errors

To resolve these errors, navigate to the config.h header file in the Cryptlib project (Cryptlib->Header Files) and add the following macros at the top of the file:


This disables the assembly code which is present in the Cryptopp library project.


The SSE2 instruction set is available only for x86 type systems, thereby its necessary to disable it for non x86 systems which in our case is Windows Phone device which has the ARM architecture.
Recompiling the project after the above change should resolve the first set of errors.

Then another set of errors appears: __emulu undefined in two cpp files: Integer.cpp & VMAC.cpp. To resolve these, navigate to the error location in the files and make the following changes:

  • In Integer.cpp replace the following macro:
    #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
    //#define MultiplyWords(p, a, b) p = __emulu(a, b);
    #define MultiplyWords(p, a, b) p = (dword)a*b;
    #define MultiplyWords(p, a, b) p = (dword)a*b;
  • In VMAC.cpp replace the following macro:
    #if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
    //#define MUL32(a, b) __emulu(word32(a), word32(b))
    #define MUL32(a, b) ((word64)((word32)(a)) * (word32)(b))
    #define MUL32(a, b) ((word64)((word32)(a)) * (word32)(b))

For Win32(emulator build) configuration : No changes required

End Result

After performing the above steps, compiling the Cryptlib project will result in the creation of a Cryptlib.lib static library at something like the following location:

G:\Users\username\Downloads\cryptopp\ARM(or Win32)\Output\Debug\cryptlib.lib

We will now use this file in the Windows Phone demo project.

Integrating Crypto++ with the Windows Phone C++ project

To make a new C++ project with Windows Phone 8, select File->New Project->Installed->Visual C++->Windows Phone ->Windows Phone Runtime Component:

New Project

Now copy the Cryptopp folder which contains the Cryptopp project you just compiled. Paste it to the location of the Windows Phone project:

CryptoPP location

Just as you set the project properties for the Cryptopp project, you need to set the properties for the Windows Phone C++ project:

  • Configuration Properties->General->Platform Toolset - Windows Phone 8.0(v110)
  • Configuration Properties->General->Configuration Type - Dynamic Library(.dll)
  • Configuration Properties->C/C++->Code generation->Runtime library - Multi-threaded Debug (/MTd) for Debug Config & Multi-threaded DLL (/MD) for Release Config
  • Configuration Properties->Linker->General->Additional Library Directories->path to lib folder under Microsoft SDK(e.g.: G:\Program Files %28x86%29\Microsoft SDKs\Windows\v7.1A\Lib\x64), for ARM this should be : G:\Program Files (x86)\Windows Kits\8.0\Lib\Win8\um\arm
  • Configuration Properties->Linker->Input->Additional Dependencies -> path to previously compiled Cryptlib.lib (e.g. :\Documents\Visual Studio 2012\Projects\WindowsPhoneRuntimeComponent1\cryptopp28oct\Win32(or ARM)\Output\Release\cryptlib.lib

Recompile the project for both the Win32 & ARM platforms after performing above steps and the build should succeed.

Compiling Crypto++ with VS2013

When Crypto++ is complied for ARM target using VS2013, an error is thrown - "Compiling Desktop applications for the ARM platform is not supported". To overcome this error, a workaround(or say hack) is available which is as follows:

  1. Navigate to the location - C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Platforms\ARM
  2. Open the file named - Platform.Common, inside the <PropertyGroup> tag , add the subtag - <WindowsSDKDesktopARMSupport>true</WindowsSDKDesktopARMSupport>

Here's how the 'Platform.Common' file would look after editing it:

<!--<DebuggerFlavor Condition="'$(DebuggerFlavor)'==''">WindowsLocalDebugger</DebuggerFlavor>-->
<DebuggerFlavor Condition="'$(WindowsAppContainer)' == 'true' and '$(AppxPackage)' == 'true'">AppHostRemoteDebugger</DebuggerFlavor>
<DebuggerFlavor Condition="'$(WindowsAppContainer)' != 'true' or '$(AppxPackage)' != 'true'">WindowsRemoteDebugger</DebuggerFlavor>

Note.pngNote: Its advisable to take a copy of file 'Platform.Common' before editing it, so it can be replaced in future in case any issue arises

The Cryptlib project properties would remain same, just it would be as per the VS 2013. Here's what need to be set:

  • Configuration Properties->General->Platform Toolset - Visual Studio 2013(v120)
  • Configuration Properties->General->Project Defaults->Configuration Type - Static library(.lib)

After performing above changes, compiling the Cryptlib project would result in generation of cryptlib.lib file, which now needs to be linked with Windows Phone runtime project as done earlier.

Using Crypto++ classes

Now can use the Crypto++ classes and functions in your Windows Phone C++ project. Include the following headers and macros in your Windows Phone C++ project class:

#include "../cryptopp28oct/dll.h"   // as Cryoptopp project folder is located inside the main Windows Phone C++ project folder
#include "../cryptopp28oct/default.h"
#include <windows.h>
using namespace WindowsPhoneRuntimeComponent1;
using namespace Platform;
using namespace Windows::Storage;
using namespace Windows::Foundation::Collections;

Crypto++ is a huge library that implements a number of cryptographic algorithms. The following example shows how to encrypt and decrypt a string.

Platform::String^ WindowsPhoneRuntimeComponentEx::EncryptString(Platform::String^EncrytStr)
// convert the string to char* (as accepted by the C++ function from Crypto++)
Platform::String^ outstrtemp;
std::wstring fooW(EncrytStr->Begin());
std::string fooA(fooW.begin(), fooW.end());
const char* charStr = fooA.c_str(); // converted string
string outstr;
char*EncryKey = "12345";
DefaultEncryptorWithMAC encryptor(EncryKey, new HexEncoder(new StringSink(outstr)));
encryptor.Put((byte *)charStr , strlen(charStr));
// convert the resulting C++ type string to a Windows Phone runtime style string
const char*iStr = outstr.c_str();
std::string s_str = std::string(iStr);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
Platform::String^ p_string = ref new Platform::String(w_char);
return p_string ;
Platform::String^ WindowsPhoneRuntimeComponentEx::DecryptString(Platform::String^EncryptdStr)
Platform::String^ outstrtemp;
string outstr;
std::wstring fooW(EncryptdStr->Begin());
std::string fooA(fooW.begin(), fooW.end());
const char* charStr = fooA.c_str();
HexDecoder decryptor(new DefaultDecryptorWithMAC("12345", new StringSink(outstr)));
decryptor.Put((byte *)charStr, strlen(charStr));
// convert the resulting C++ type string to a Windows Phone runtime style string
const char*iStr = outstr.c_str();
std::string s_str = std::string(iStr);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
Platform::String^ p_string = ref new Platform::String(w_char);
//const wchar_t* w_char = p_string->Data();
return p_string;

Header file:

Platform::String^ EncryptString();

Linking Windows Phone C++ & Windows Phone C# project

The next step is to construct a user interface for the C++ project you created above. For example, you might do this to perform encryption/decryption on a button press.

To create a user interface, add a new Windows Phone (Visual C#) project to the Windows Phone C++ project you are using as follows:

  1. Choose File->New Project->Installed->Templates->Visual C#->Windows Phone ->Windows Phone App
  2. Add a reference to the Windows Phone C++ project (Windows Phone runtime) by right-clicking on the project and selecting Add Reference->Solution->path to Windows Phone C++(runtime) project.vcxproj
  3. Add two buttons: Encrypt and Decrypt, as well as a text box for an input string, to MainPage.xaml:
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Margin="14,151,10,10" Grid.RowSpan="2">
<Button Content="Encrypt" HorizontalAlignment="Left" Margin="42,289,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<TextBox HorizontalAlignment="Left" Name="TextBox1" Height="86" Margin="96,169,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="208"/>
<Button Content="Decrypt" HorizontalAlignment="Left" Margin="219,289,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>

Write the implementation code for MainPage.xaml:

using Microsoft.Phone.Tasks;
using Windows.Storage;
using WindowsPhoneRuntimeComponent1; // c++ project name
public partial class MainPage : PhoneApplicationPage
string _encryptedStr;
//To encrypt a string
private void Button_Click(object sender, RoutedEventArgs e)
var Extlib = new WindowsPhoneRuntimeComponentEx();
_encryptedStr= Extlib.EncryptString(TextBox1.Text); /// calls the C++ function defined inside the Windows Phone Runtime project
TextBox1.Text = "";
//To decrypt a string
private void Button_Click_1(object sender, RoutedEventArgs e)
var Extlib = new WindowsPhoneRuntimeComponentEx();
MessageBox.Show(Extlib.DecryptString(_encryptedStr ))); // passes the encrypted string

License information

The compiled library is licensed under the Boost Software License 1.0 - for the full terms see the project License.txt. The project folder (Cryptopp) also contains a Readme.txt which includes all of the project build info as well as additional licensing information.

Example Project

An example project is attached. This project contains two projects:

  • WindowsPhoneRuntimeComponent: This is the Windows Phone C++ project which connects with the Cryptlib.lib library and implements the functions responsible for encryption/decryption.
  • LibTest : This is the Windows Phone C# App project that implements the user interface and interacts with the Windows Phone C++ project. In other words, the functions in the C++ project are called based on user interaction in LibTest.

The Crypto++ project involving Cryptlib that is responsible for the creation of .lib files, handles all of the encryption/decryption, and is linked with the Windows Phone C++ project is NOT attached. You should download it yourself from the Cryptopp home page and use it as described in the steps above.

Note.pngNote: The attached example project will only compile once the Cryptlib.lib library is linked with the project.

External links


  1. Please read the Readme.txt document included inside cryptopp562 folder carefully to understand what each of four projects does and other relevant things
This page was last modified on 11 October 2014, at 07:00.
387 page views in the last 30 days.