├── Example ├── .#Example.csproj ├── Example.userprefs ├── Example │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Example.csproj └── Example.sln ├── .gitignore ├── README_zh_CN.md ├── LICENSE.md ├── README.md └── XXTEA └── XXTEA.cs /Example/.#Example.csproj: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | Example/Example/obj 3 | Example/Example/bin 4 | -------------------------------------------------------------------------------- /Example/Example.userprefs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Example/Example/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using Xxtea; 4 | 5 | namespace Example { 6 | class MainClass { 7 | public static void Main (string[] args) { 8 | String str = "Hello World! 你好,中国!"; 9 | String key = "1234567890"; 10 | String encrypt_data = XXTEA.EncryptToBase64String(str, key); 11 | Console.WriteLine(encrypt_data); 12 | Debug.Assert("QncB1C0rHQoZ1eRiPM4dsZtRi9pNrp7sqvX76cFXvrrIHXL6" == encrypt_data); 13 | String decrypt_data = XXTEA.DecryptBase64StringToString(encrypt_data, key); 14 | Debug.Assert(str == decrypt_data); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Example/Example.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{3E0F6C07-EAA5-4C07-B283-703AEB33E09D}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {3E0F6C07-EAA5-4C07-B283-703AEB33E09D}.Debug|x86.ActiveCfg = Debug|x86 13 | {3E0F6C07-EAA5-4C07-B283-703AEB33E09D}.Debug|x86.Build.0 = Debug|x86 14 | {3E0F6C07-EAA5-4C07-B283-703AEB33E09D}.Release|x86.ActiveCfg = Release|x86 15 | {3E0F6C07-EAA5-4C07-B283-703AEB33E09D}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | EndGlobal 18 | -------------------------------------------------------------------------------- /README_zh_CN.md: -------------------------------------------------------------------------------- 1 | # XXTEA 加密算法的 .NET 实现 2 | 3 | 4 | XXTEA logo 5 | 6 | 7 | ## 简介 8 | 9 | XXTEA 是一个快速安全的加密算法。本项目是 XXTEA 加密算法的 .NET 实现。 10 | 11 | 它不同于原始的 XXTEA 加密算法。它是针对 Byte[] 类型数据进行加密的,而不是针对 32 位整型数组。同样,密钥也是 Byte[]。 12 | 13 | 为了用户使用方便,除了提供对 Byte[] 进行加解密的 API 之外,还提供了一些辅助方法来处理字符串和 Base64 编码。 14 | 15 | ## 安装 16 | 17 | ```sh 18 | git clone https://github.com/xxtea/xxtea-dotnet.git 19 | ``` 20 | 21 | ## 使用 22 | 23 | ```csharp 24 | using System; 25 | using System.Diagnostics; 26 | using Xxtea; 27 | 28 | namespace Example { 29 | class MainClass { 30 | public static void Main (string[] args) { 31 | String str = "Hello World! 你好,中国!"; 32 | String key = "1234567890"; 33 | Byte[] encrypt_data = XXTEA.Encrypt(str, key); 34 | String decrypt_data = XXTEA.DecryptToString(encrypt_data, key); 35 | Debug.Assert(str == decrypt_data); 36 | } 37 | } 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /Example/Example/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | 4 | // Information about this assembly is defined by the following attributes. 5 | // Change them to the values specific to your project. 6 | 7 | [assembly: AssemblyTitle ("Example")] 8 | [assembly: AssemblyDescription ("")] 9 | [assembly: AssemblyConfiguration ("")] 10 | [assembly: AssemblyCompany ("")] 11 | [assembly: AssemblyProduct ("")] 12 | [assembly: AssemblyCopyright ("andot")] 13 | [assembly: AssemblyTrademark ("")] 14 | [assembly: AssemblyCulture ("")] 15 | 16 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". 17 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, 18 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. 19 | 20 | [assembly: AssemblyVersion ("1.0.*")] 21 | 22 | // The following attributes are used to specify the signing key for the assembly, 23 | // if desired. See the Mono documentation for more information about signing. 24 | 25 | //[assembly: AssemblyDelaySign(false)] 26 | //[assembly: AssemblyKeyFile("")] 27 | 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2008-2016 Ma Bingyao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XXTEA for .NET 2 | 3 | 4 | XXTEA logo 5 | 6 | 7 | ## Introduction 8 | 9 | XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for .NET. 10 | 11 | It is different from the original XXTEA encryption algorithm. It encrypts and decrypts Byte[] instead of 32bit integer array, and the key is also the Byte[]. 12 | 13 | In addition to providing the API of Byte[] encryption and decryption, it also provides some methods to handle String and Base64 encode. 14 | 15 | ## Installation 16 | 17 | ```sh 18 | git clone https://github.com/xxtea/xxtea-dotnet.git 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```csharp 24 | using System; 25 | using System.Diagnostics; 26 | using Xxtea; 27 | 28 | namespace Example { 29 | class MainClass { 30 | public static void Main (string[] args) { 31 | String str = "Hello World! 你好,中国!"; 32 | String key = "1234567890"; 33 | Byte[] encrypt_data = XXTEA.Encrypt(str, key); 34 | String decrypt_data = XXTEA.DecryptToString(encrypt_data, key); 35 | Debug.Assert(str == decrypt_data); 36 | } 37 | } 38 | } 39 | 40 | 41 | ``` 42 | -------------------------------------------------------------------------------- /Example/Example/Example.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | {3E0F6C07-EAA5-4C07-B283-703AEB33E09D} 7 | Exe 8 | Example 9 | Example 10 | v4.5 11 | 12 | 13 | true 14 | full 15 | false 16 | bin\Debug 17 | DEBUG; 18 | prompt 19 | 4 20 | true 21 | x86 22 | 23 | 24 | full 25 | true 26 | bin\Release 27 | prompt 28 | 4 29 | true 30 | x86 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /XXTEA/XXTEA.cs: -------------------------------------------------------------------------------- 1 | /**********************************************************\ 2 | | | 3 | | XXTEA.cs | 4 | | | 5 | | XXTEA encryption algorithm library for .NET. | 6 | | | 7 | | Encryption Algorithm Authors: | 8 | | David J. Wheeler | 9 | | Roger M. Needham | 10 | | | 11 | | Code Author: Ma Bingyao | 12 | | LastModified: Mar 10, 2015 | 13 | | | 14 | \**********************************************************/ 15 | 16 | namespace Xxtea { 17 | using System; 18 | using System.Text; 19 | 20 | public sealed class XXTEA { 21 | private static readonly UTF8Encoding utf8 = new UTF8Encoding(); 22 | 23 | private const UInt32 delta = 0x9E3779B9; 24 | 25 | private static UInt32 MX(UInt32 sum, UInt32 y, UInt32 z, Int32 p, UInt32 e, UInt32[] k) { 26 | return (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); 27 | } 28 | 29 | private XXTEA() { 30 | } 31 | 32 | public static Byte[] Encrypt(Byte[] data, Byte[] key) { 33 | if (data.Length == 0) { 34 | return data; 35 | } 36 | return ToByteArray(Encrypt(ToUInt32Array(data, true), ToUInt32Array(FixKey(key), false)), false); 37 | } 38 | 39 | public static Byte[] Encrypt(String data, Byte[] key) { 40 | return Encrypt(utf8.GetBytes(data), key); 41 | } 42 | 43 | public static Byte[] Encrypt(Byte[] data, String key) { 44 | return Encrypt(data, utf8.GetBytes(key)); 45 | } 46 | 47 | public static Byte[] Encrypt(String data, String key) { 48 | return Encrypt(utf8.GetBytes(data), utf8.GetBytes(key)); 49 | } 50 | 51 | public static String EncryptToBase64String(Byte[] data, Byte[] key) { 52 | return Convert.ToBase64String(Encrypt(data, key)); 53 | } 54 | 55 | public static String EncryptToBase64String(String data, Byte[] key) { 56 | return Convert.ToBase64String(Encrypt(data, key)); 57 | } 58 | 59 | public static String EncryptToBase64String(Byte[] data, String key) { 60 | return Convert.ToBase64String(Encrypt(data, key)); 61 | } 62 | 63 | public static String EncryptToBase64String(String data, String key) { 64 | return Convert.ToBase64String(Encrypt(data, key)); 65 | } 66 | 67 | public static Byte[] Decrypt(Byte[] data, Byte[] key) { 68 | if (data.Length == 0) { 69 | return data; 70 | } 71 | return ToByteArray(Decrypt(ToUInt32Array(data, false), ToUInt32Array(FixKey(key), false)), true); 72 | } 73 | 74 | public static Byte[] Decrypt(Byte[] data, String key) { 75 | return Decrypt(data, utf8.GetBytes(key)); 76 | } 77 | 78 | public static Byte[] DecryptBase64String(String data, Byte[] key) { 79 | return Decrypt(Convert.FromBase64String(data), key); 80 | } 81 | 82 | public static Byte[] DecryptBase64String(String data, String key) { 83 | return Decrypt(Convert.FromBase64String(data), key); 84 | } 85 | 86 | public static String DecryptToString(Byte[] data, Byte[] key) { 87 | return utf8.GetString(Decrypt(data, key)); 88 | } 89 | 90 | public static String DecryptToString(Byte[] data, String key) { 91 | return utf8.GetString(Decrypt(data, key)); 92 | } 93 | 94 | public static String DecryptBase64StringToString(String data, Byte[] key) { 95 | return utf8.GetString(DecryptBase64String(data, key)); 96 | } 97 | 98 | public static String DecryptBase64StringToString(String data, String key) { 99 | return utf8.GetString(DecryptBase64String(data, key)); 100 | } 101 | 102 | private static UInt32[] Encrypt(UInt32[] v, UInt32[] k) { 103 | Int32 n = v.Length - 1; 104 | if (n < 1) { 105 | return v; 106 | } 107 | UInt32 z = v[n], y, sum = 0, e; 108 | Int32 p, q = 6 + 52 / (n + 1); 109 | unchecked { 110 | while (0 < q--) { 111 | sum += delta; 112 | e = sum >> 2 & 3; 113 | for (p = 0; p < n; p++) { 114 | y = v[p + 1]; 115 | z = v[p] += MX(sum, y, z, p, e, k); 116 | } 117 | y = v[0]; 118 | z = v[n] += MX(sum, y, z, p, e, k); 119 | } 120 | } 121 | return v; 122 | } 123 | 124 | private static UInt32[] Decrypt(UInt32[] v, UInt32[] k) { 125 | Int32 n = v.Length - 1; 126 | if (n < 1) { 127 | return v; 128 | } 129 | UInt32 z, y = v[0], sum, e; 130 | Int32 p, q = 6 + 52 / (n + 1); 131 | unchecked { 132 | sum = (UInt32)(q * delta); 133 | while (sum != 0) { 134 | e = sum >> 2 & 3; 135 | for (p = n; p > 0; p--) { 136 | z = v[p - 1]; 137 | y = v[p] -= MX(sum, y, z, p, e, k); 138 | } 139 | z = v[n]; 140 | y = v[0] -= MX(sum, y, z, p, e, k); 141 | sum -= delta; 142 | } 143 | } 144 | return v; 145 | } 146 | 147 | private static Byte[] FixKey(Byte[] key) { 148 | if (key.Length == 16) return key; 149 | Byte[] fixedkey = new Byte[16]; 150 | if (key.Length < 16) { 151 | key.CopyTo(fixedkey, 0); 152 | } 153 | else { 154 | Array.Copy(key, 0, fixedkey, 0, 16); 155 | } 156 | return fixedkey; 157 | } 158 | 159 | private static UInt32[] ToUInt32Array(Byte[] data, Boolean includeLength) { 160 | Int32 length = data.Length; 161 | Int32 n = (((length & 3) == 0) ? (length >> 2) : ((length >> 2) + 1)); 162 | UInt32[] result; 163 | if (includeLength) { 164 | result = new UInt32[n + 1]; 165 | result[n] = (UInt32)length; 166 | } 167 | else { 168 | result = new UInt32[n]; 169 | } 170 | for (Int32 i = 0; i < length; i++) { 171 | result[i >> 2] |= (UInt32)data[i] << ((i & 3) << 3); 172 | } 173 | return result; 174 | } 175 | 176 | private static Byte[] ToByteArray(UInt32[] data, Boolean includeLength) { 177 | Int32 n = data.Length << 2; 178 | if (includeLength) { 179 | Int32 m = (Int32)data[data.Length - 1]; 180 | n -= 4; 181 | if ((m < n - 3) || (m > n)) { 182 | return null; 183 | } 184 | n = m; 185 | } 186 | Byte[] result = new Byte[n]; 187 | for (Int32 i = 0; i < n; i++) { 188 | result[i] = (Byte)(data[i >> 2] >> ((i & 3) << 3)); 189 | } 190 | return result; 191 | } 192 | } 193 | } 194 | --------------------------------------------------------------------------------