mirror of
https://github.com/crskycode/GARbro.git
synced 2024-10-23 07:38:16 +08:00
(Blowfish): moved to GameRes.Cryptography namespace.
(BlowfishDecryptor): ICryptoTransform interface implementation. (BlowfishStream): removed as unused.
This commit is contained in:
parent
837c203e4b
commit
db0d39f863
@ -25,10 +25,11 @@
|
|||||||
|***************************************************************************/
|
|***************************************************************************/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using GameRes.Utility;
|
||||||
|
|
||||||
namespace Simias.Encryption
|
namespace GameRes.Cryptography
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class that provides blowfish encryption.
|
/// Class that provides blowfish encryption.
|
||||||
@ -285,11 +286,6 @@ namespace Simias.Encryption
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private uint F(uint x)
|
private uint F(uint x)
|
||||||
{
|
{
|
||||||
ushort a;
|
ushort a;
|
||||||
@ -443,235 +439,61 @@ namespace Simias.Encryption
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BlowfishStream : Stream
|
/// <summary>
|
||||||
{
|
/// ICryptoTransform implementation for use with CryptoStream.
|
||||||
class CBState : IAsyncResult
|
/// </summary>
|
||||||
{
|
public sealed class BlowfishDecryptor : ICryptoTransform
|
||||||
internal AsyncCallback callback;
|
{
|
||||||
internal object state;
|
Blowfish m_bf;
|
||||||
internal byte[] buffer;
|
|
||||||
internal IAsyncResult result;
|
public const int BlockSize = 8;
|
||||||
internal CBState(AsyncCallback callback, object state, byte[] buffer)
|
|
||||||
|
public bool CanTransformMultipleBlocks { get { return true; } }
|
||||||
|
public bool CanReuseTransform { get { return true; } }
|
||||||
|
public int InputBlockSize { get { return BlockSize; } }
|
||||||
|
public int OutputBlockSize { get { return BlockSize; } }
|
||||||
|
|
||||||
|
public BlowfishDecryptor (Blowfish bf)
|
||||||
|
{
|
||||||
|
m_bf = bf;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int TransformBlock (byte[] inBuffer, int offset, int count, byte[] outBuffer, int outOffset)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < count; i += BlockSize)
|
||||||
{
|
{
|
||||||
this.callback = callback;
|
uint xl = LittleEndian.ToUInt32 (inBuffer, offset+i);
|
||||||
this.state = state;
|
uint xr = LittleEndian.ToUInt32 (inBuffer, offset+i+4);
|
||||||
this.buffer = buffer;
|
m_bf.Decipher (ref xl, ref xr);
|
||||||
}
|
LittleEndian.Pack (xl, outBuffer, outOffset+i);
|
||||||
#region IAsyncResult Members
|
LittleEndian.Pack (xr, outBuffer, outOffset+i+4);
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
public object AsyncState
|
static readonly byte[] EmptyArray = new byte[0];
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool CompletedSynchronously
|
public byte[] TransformFinalBlock (byte[] inBuffer, int offset, int count)
|
||||||
{
|
{
|
||||||
get
|
if (0 == count)
|
||||||
{
|
return EmptyArray;
|
||||||
return result.CompletedSynchronously;
|
if (0 != count % BlockSize)
|
||||||
}
|
throw new InvalidDataException ("Non-padded block in Blowfish encrypted stream");
|
||||||
}
|
var output = new byte[count];
|
||||||
|
TransformBlock (inBuffer, offset, count, output, 0);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
public System.Threading.WaitHandle AsyncWaitHandle
|
#region IDisposable implementation
|
||||||
{
|
bool _disposed = false;
|
||||||
get
|
public void Dispose ()
|
||||||
{
|
{
|
||||||
return result.AsyncWaitHandle;
|
if (!_disposed)
|
||||||
}
|
{
|
||||||
}
|
_disposed = true;
|
||||||
|
}
|
||||||
public bool IsCompleted
|
GC.SuppressFinalize (this);
|
||||||
{
|
}
|
||||||
get
|
#endregion
|
||||||
{
|
}
|
||||||
return result.IsCompleted;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum Target
|
|
||||||
{
|
|
||||||
Encrypted,
|
|
||||||
Normal
|
|
||||||
};
|
|
||||||
|
|
||||||
Stream stream;
|
|
||||||
Blowfish bf;
|
|
||||||
Target target;
|
|
||||||
|
|
||||||
BlowfishStream(Stream stream, Blowfish bf, Target target)
|
|
||||||
{
|
|
||||||
this.stream = stream;
|
|
||||||
this.bf = bf;
|
|
||||||
this.target = target;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the stream support reads.
|
|
||||||
/// </summary>
|
|
||||||
public override bool CanRead
|
|
||||||
{
|
|
||||||
get {return stream.CanRead;}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true is the stream supports seeks.
|
|
||||||
/// </summary>
|
|
||||||
public override bool CanSeek
|
|
||||||
{
|
|
||||||
get {return stream.CanSeek;}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns true if the stream supports writes.
|
|
||||||
/// </summary>
|
|
||||||
public override bool CanWrite
|
|
||||||
{
|
|
||||||
get {return stream.CanWrite;}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the length of the stream.
|
|
||||||
/// </summary>
|
|
||||||
public override long Length
|
|
||||||
{
|
|
||||||
get {return stream.Length;}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or Sets the posistion of the stream.
|
|
||||||
/// </summary>
|
|
||||||
public override long Position
|
|
||||||
{
|
|
||||||
get {return stream.Position;}
|
|
||||||
set {stream.Position = value;}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Flushes the stream.
|
|
||||||
/// </summary>
|
|
||||||
public override void Flush()
|
|
||||||
{
|
|
||||||
stream.Flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read data from the stream and encrypt it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="buffer">The buffer to read into.</param>
|
|
||||||
/// <param name="offset">The offset in the buffer to begin storing data.</param>
|
|
||||||
/// <param name="count">The number of bytes to read.</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override int Read(byte[] buffer, int offset, int count)
|
|
||||||
{
|
|
||||||
int bytesRead = stream.Read(buffer, offset, count);
|
|
||||||
if (target == Target.Normal)
|
|
||||||
bf.Encipher(buffer, bytesRead);
|
|
||||||
else
|
|
||||||
bf.Decipher(buffer, bytesRead);
|
|
||||||
return bytesRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write data to the stream after decrypting it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="buffer">The buffer containing the data to write.</param>
|
|
||||||
/// <param name="offset">The offset in the buffer where the data begins.</param>
|
|
||||||
/// <param name="count">The number of bytes to write.</param>
|
|
||||||
public override void Write(byte[] buffer, int offset, int count)
|
|
||||||
{
|
|
||||||
if (target == Target.Normal)
|
|
||||||
bf.Decipher(buffer, count);
|
|
||||||
else
|
|
||||||
bf.Encipher(buffer, count);
|
|
||||||
stream.Write(buffer, offset, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="buffer"></param>
|
|
||||||
/// <param name="offset"></param>
|
|
||||||
/// <param name="count"></param>
|
|
||||||
/// <param name="callback"></param>
|
|
||||||
/// <param name="state"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
|
|
||||||
{
|
|
||||||
CBState cbs = new CBState(callback, state, buffer);
|
|
||||||
cbs.result = base.BeginRead (buffer, offset, count, new AsyncCallback(ReadComplete), cbs);
|
|
||||||
return cbs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="asyncResult"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override int EndRead(IAsyncResult asyncResult)
|
|
||||||
{
|
|
||||||
CBState cbs = (CBState)asyncResult.AsyncState;
|
|
||||||
int bytesRead = base.EndRead (cbs.result);
|
|
||||||
if (target == Target.Normal)
|
|
||||||
bf.Encipher(cbs.buffer, bytesRead);
|
|
||||||
else
|
|
||||||
bf.Decipher(cbs.buffer, bytesRead);
|
|
||||||
return bytesRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The Read has completed.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="result">The result of the async write.</param>
|
|
||||||
private void ReadComplete(IAsyncResult result)
|
|
||||||
{
|
|
||||||
CBState cbs = (CBState)result.AsyncState;
|
|
||||||
cbs.callback(cbs);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="buffer"></param>
|
|
||||||
/// <param name="offset"></param>
|
|
||||||
/// <param name="count"></param>
|
|
||||||
/// <param name="callback"></param>
|
|
||||||
/// <param name="state"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
|
|
||||||
{
|
|
||||||
if (target == Target.Normal)
|
|
||||||
bf.Decipher(buffer, count);
|
|
||||||
else
|
|
||||||
bf.Encipher(buffer, count);
|
|
||||||
return base.BeginWrite (buffer, offset, count, callback, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Move the current stream posistion to the specified location.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="offset">The offset from the origin to seek.</param>
|
|
||||||
/// <param name="origin">The origin to seek from.</param>
|
|
||||||
/// <returns>The new position.</returns>
|
|
||||||
public override long Seek(long offset, SeekOrigin origin)
|
|
||||||
{
|
|
||||||
return stream.Seek(offset, origin);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set the stream length.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The length to set.</param>
|
|
||||||
public override void SetLength(long value)
|
|
||||||
{
|
|
||||||
stream.SetLength(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ using System.Text;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.ComponentModel.Composition;
|
using System.ComponentModel.Composition;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Simias.Encryption;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using GameRes.Formats.Strings;
|
using GameRes.Formats.Strings;
|
||||||
using GameRes.Formats.Properties;
|
using GameRes.Formats.Properties;
|
||||||
|
Loading…
Reference in New Issue
Block a user