Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Encrypt addin #90

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions Tomboy.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26403.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tomboy", "Tomboy.csproj", "{315DBB30-1461-4A41-A23F-A888D84E1EA0}"
EndProject
Expand All @@ -25,7 +25,9 @@ Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Setup", "Setup\Setup.wixpro
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintNotes", "Tomboy\Addins\PrintNotes\PrintNotes.csproj", "{A13FBE2F-C5FF-4E8D-B687-4A309D3FE08F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Underline", "Tomboy\Addins\Underline\Underline.csproj", "{15A0F2C4-9BAD-11DE-B022-C19356D89593}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Underline", "Tomboy\Addins\Underline\Underline.csproj", "{15A0F2C4-C5FF-11CF-B022-C19356D89593}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PassEncrypt", "Tomboy\Addins\PassEncrypt\PassEncrypt.csproj", "{15AC9F12-9BAD-11DE-B022-C97A56D78593}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSyncService", "Tomboy\Addins\WebSyncService\WebSyncService.csproj", "{A207AD10-9C5E-495F-9DEF-E7321A6FCB8F}"
EndProject
Expand Down Expand Up @@ -104,12 +106,18 @@ Global
{A13FBE2F-C5FF-4E8D-B687-4A309D3FE08F}.Mac|Any CPU.Build.0 = Mac|Any CPU
{A13FBE2F-C5FF-4E8D-B687-4A309D3FE08F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A13FBE2F-C5FF-4E8D-B687-4A309D3FE08F}.Release|Any CPU.Build.0 = Release|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Mac|Any CPU.ActiveCfg = Mac|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Mac|Any CPU.Build.0 = Mac|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15A0F2C4-9BAD-11DE-B022-C19356D89593}.Release|Any CPU.Build.0 = Release|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Mac|Any CPU.ActiveCfg = Mac|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Mac|Any CPU.Build.0 = Mac|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15A0F2C4-C5FF-11CF-B022-C19356D89593}.Release|Any CPU.Build.0 = Release|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Mac|Any CPU.ActiveCfg = Mac|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Mac|Any CPU.Build.0 = Mac|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15AC9F12-9BAD-11DE-B022-C97A56D78593}.Release|Any CPU.Build.0 = Release|Any CPU
{A207AD10-9C5E-495F-9DEF-E7321A6FCB8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A207AD10-9C5E-495F-9DEF-E7321A6FCB8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A207AD10-9C5E-495F-9DEF-E7321A6FCB8F}.Mac|Any CPU.ActiveCfg = Mac|Any CPU
Expand Down
112 changes: 112 additions & 0 deletions Tomboy/Addins/PassEncrypt/Encrypter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//According to https://stackoverflow.com/questions/10168240/encrypting-decrypting-a-string-in-c-sharp CraigTP answer.
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;

namespace Tomboy.PassEncrypt
{
public static class Encrypter
{
// This constant is used to determine the keysize of the encryption algorithm in bits.
// We divide this by 8 within the code below to get the equivalent number of bytes.
private const int Keysize = 256;

// This constant determines the number of iterations for the password bytes generation function.
private const int DerivationIterations = 1000;

public static string Encrypt(string plainText, string passPhrase)
{
// Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
// so that the same Salt and IV values can be used when decrypting.
var saltStringBytes = Generate256BitsOfRandomEntropy();
var ivStringBytes = Generate256BitsOfRandomEntropy();
var plainTextBytes = Encoding.Unicode.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = 256;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
// Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
var cipherTextBytes = saltStringBytes;
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}

public static string Decrypt(string cipherText, string passPhrase)
{
try
{
// Get the complete stream of bytes that represent:
// [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
// Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
// Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
// Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();

using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = 256;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream(cipherTextBytes))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.Unicode.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
catch (CryptographicException)
{
return Encrypt(cipherText, passPhrase);
}
}

private static byte[] Generate256BitsOfRandomEntropy()
{
var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
using (var rngCsp = new RNGCryptoServiceProvider())
{
// Fill the array with cryptographically secure random bytes.
rngCsp.GetBytes(randomBytes);
}
return randomBytes;
}
}
}
41 changes: 41 additions & 0 deletions Tomboy/Addins/PassEncrypt/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
include $(top_srcdir)/Makefile.include

CSFLAGS = \
-debug \
-define:DEBUG \
-target:library

ASSEMBLIES = \
$(LINK_TOMBOY_EXE) \
$(GTKSHARP_LIBS) \
$(LINK_MONO_ADDINS) \
-r:Mono.Posix

ADDIN_NAME = PassEncrypt
TARGET = $(top_builddir)/bin/addins/$(ADDIN_NAME).dll
CSFILES = \
$(srcdir)/PassEncryptMenuItem.cs \
$(srcdir)/PassEncryptNoteAddin.cs \
$(srcdir)/PassEncryptTag.cs
RESOURCES = \
-resource:$(srcdir)/$(ADDIN_NAME).addin.xml

$(TARGET).mdb: $(TARGET)

$(TARGET): $(CSFILES) $(TOMBOY_EXE_PATH)
mkdir -p `dirname $(TARGET)` && $(CSC) -out:$@ $(CSFLAGS) $(ASSEMBLIES) $(CSFILES) $(RESOURCES)


addinsdir = $(pkglibdir)/addins
addins_DATA = \
$(TARGET) \
$(TARGET).mdb

EXTRA_DIST = \
$(CSFILES) \
$(srcdir)/$(ADDIN_NAME).addin.xml

CLEANFILES = \
$(TARGET).mdb \
$(TARGET)

95 changes: 95 additions & 0 deletions Tomboy/Addins/PassEncrypt/PassEncrypt-mac.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{15A0F2C4-9BAD-11DE-B022-C19356D89593}</ProjectGuid>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<OutputType>Library</OutputType>
<NoStandardLibraries>false</NoStandardLibraries>
<AssemblyName>PassEncrypt</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<RootNamespace>PassEncrypt</RootNamespace>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\..\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\Current\lib\mono\gac\gtk-sharp\2.12.0.0__35e10195dab3c99f\gtk-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="glib-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\Current\lib\mono\gac\glib-sharp\2.12.0.0__35e10195dab3c99f\glib-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="gdk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\Current\lib\mono\gac\gdk-sharp\2.12.0.0__35e10195dab3c99f\gdk-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="atk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\Current\lib\mono\gac\atk-sharp\2.12.0.0__35e10195dab3c99f\atk-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="pango-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\Current\lib\mono\gac\pango-sharp\2.12.0.0__35e10195dab3c99f\pango-sharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Mono.Posix" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="PassEncrypt.addin.xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PassEncryptMenuItem.cs" />
<Compile Include="PassEncryptNoteAddin.cs" />
<Compile Include="PassEncryptTag.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Makefile.am" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Tomboy-mac.csproj">
<Project>{315DBB30-1461-4A41-A23F-A888D84E1EA0}</Project>
<Name>Tomboy-mac</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio AllowExistingFolder="true" />
</ProjectExtensions>
</Project>
21 changes: 21 additions & 0 deletions Tomboy/Addins/PassEncrypt/PassEncrypt.addin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Addin id="PassEncryptAddin"
namespace="Tomboy"
name="PassEncrypt"
author="Tomboy Project"
description="Adds ability to Encrypt text. Just select text and press ctrl+e to encrypt some text. To decrypt it just click link and enter password. Decrypted text is shown and copied to clipboard."
category="Formatting"
defaultEnabled="false"
version="0.1">

<Runtime>
<Import assembly="PassEncrypt.dll" />
</Runtime>

<Dependencies>
<Addin id="Tomboy" version="0.10" />
</Dependencies>

<Extension path="/Tomboy/NoteAddins">
<NoteAddin type="Tomboy.PassEncrypt.PassEncryptNoteAddin" />
</Extension>
</Addin>
Loading