Menu

[r399]: / phprpc_2.1 / dotNET / XXTEA.cs  Maximize  Restore  History

Download this file

142 lines (138 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* XXTEA.cs
*
* Author: Ma Bingyao <andot@ujn.edu.cn>
* Copyright: CoolCode.CN
* Version: 1.4
* LastModified: 2006-07-23
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=163
*/
using System;
public class XXTEAException : Exception
{
public XXTEAException() { }
public XXTEAException(string message) : base(message) { }
public XXTEAException(string message, Exception inner) : base(message, inner) { }
}
public class XXTEA
{
private XXTEA()
{
}
public static Byte[] Encrypt(Byte[] Data, Byte[] Key)
{
if (Data.Length == 0)
{
return Data;
}
return ToByteArray(Encrypt(ToUInt32Array(Data, true), ToUInt32Array(Key, false)), false);
}
public static Byte[] Decrypt(Byte[] Data, Byte[] Key)
{
if (Data.Length == 0)
{
return Data;
}
return ToByteArray(Decrypt(ToUInt32Array(Data, false), ToUInt32Array(Key, false)), true);
}
public static UInt32[] Encrypt(UInt32[] v, UInt32[] k)
{
Int32 n = v.Length - 1;
if (n < 1)
{
return v;
}
if (k.Length < 4)
{
UInt32[] Key = new UInt32[4];
k.CopyTo(Key, 0);
k = Key;
}
UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum = 0, e;
Int32 p, q = 6 + 52 / (n + 1);
while (0 < q--)
{
sum = unchecked(sum + delta);
e = sum >> 2 & 3;
for (p = 0; p < n; p++)
{
y = v[p + 1];
z = unchecked(v[p] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
y = v[0];
z = unchecked(v[n] += (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
return v;
}
public static UInt32[] Decrypt(UInt32[] v, UInt32[] k)
{
Int32 n = v.Length - 1;
if (n < 1)
{
return v;
}
if (k.Length < 4)
{
UInt32[] Key = new UInt32[4];
k.CopyTo(Key, 0);
k = Key;
}
UInt32 z = v[n], y = v[0], delta = 0x9E3779B9, sum, e;
Int32 p, q = 6 + 52 / (n + 1);
sum = unchecked((UInt32)(q * delta));
while (sum != 0)
{
e = sum >> 2 & 3;
for (p = n; p > 0; p--)
{
z = v[p - 1];
y = unchecked(v[p] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
}
z = v[n];
y = unchecked(v[0] -= (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z));
sum = unchecked(sum - delta);
}
return v;
}
private static UInt32[] ToUInt32Array(Byte[] Data, Boolean IncludeLength)
{
Int32 n = (((Data.Length & 3) == 0) ? (Data.Length >> 2) : ((Data.Length >> 2) + 1));
UInt32[] Result;
if (IncludeLength)
{
Result = new UInt32[n + 1];
Result[n] = (UInt32)Data.Length;
}
else
{
Result = new UInt32[n];
}
n = Data.Length;
for (Int32 i = 0; i < n; i++)
{
Result[i >> 2] |= (UInt32)Data[i] << ((i & 3) << 3);
}
return Result;
}
private static Byte[] ToByteArray(UInt32[] Data, Boolean IncludeLength)
{
Int32 n = Data.Length << 2;
if (IncludeLength)
{
Int32 m = (Int32)Data[Data.Length - 1];
if (m > n)
{
throw new XXTEAException("XXTEA Decrypt Error: Wrong input data.");
}
else {
n = m;
}
}
Byte[] Result = new Byte[n];
for (Int32 i = 0; i < n; i++)
{
Result[i] = (Byte)(Data[i >> 2] >> ((i & 3) << 3));
}
return Result;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.