我如何在 C# 中使此代码从加密到解密我使用 Xor 和 Or
我尝试使用此代码对其进行解密,但有问题我使用 CryperCounter 在首先使用 Make PrepareAuthCryptography 中使用 make counter,然后使用 authCryptography key2 使用 >> 8和Key1使用& 0xff 看代码你就明白了
public class AuthCryptography
{
class CryptCounter
{
public CryptCounter()
{
}
public CryptCounter(ushort with)
{
m_Counter = with;
}
UInt16 m_Counter = 0;
public byte Key2
{
get { return (byte)(m_Counter >> 8); }
}
public byte Key1
{
get { return (byte)(m_Counter & 0xFF); }
}
public void Increment()
{
m_Counter++;
}
}
private CryptCounter _decryptCounter;
private CryptCounter _encryptCounter;
private static byte[] _cryptKey1;
private static byte[] _cryptKey2;
public static void PrepareAuthCryptography()
{
if (_cryptKey1 != null)
{
if (_cryptKey1.Length != 0)
return;
}
_cryptKey1 = new byte[0x100];
_cryptKey2 = new byte[0x100];
byte i_key1 = 0x9D;
byte i_key2 = 0x62;
for (int i = 0; i < 0x100; i++)
{
_cryptKey1[i] = i_key1;
_cryptKey2[i] = i_key2;
i_key1 = (byte)((0x0F + (byte)(i_key1 * 0xFA)) * i_key1 + 0x13);
i_key2 = (byte)((0x79 - (byte)(i_key2 * 0x5C)) * i_key2 + 0x6D);
}
}
public AuthCryptography()
{
_encryptCounter = new CryptCounter();
_decryptCounter = new CryptCounter();
}
public void Encrypt(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
_encryptCounter.Increment();
}
}
public void Decrypt(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
_decryptCounter.Increment();
}
}
public void Decrypt4(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
buffer[i] = (byte)(buffer[i] << 4 | buffer[i] >> 4);
buffer[i] ^= (byte)0xAB;
_encryptCounter.Increment();
}
}
}
i try use this code to make decrypt it but it's have problem i use CryperCounter to use make counter in first make PrepareAuthCryptography then use authCryptography key2 use >> 8 and Key1 use & 0xff see the code and you will understand it
public class AuthCryptography
{
class CryptCounter
{
public CryptCounter()
{
}
public CryptCounter(ushort with)
{
m_Counter = with;
}
UInt16 m_Counter = 0;
public byte Key2
{
get { return (byte)(m_Counter >> 8); }
}
public byte Key1
{
get { return (byte)(m_Counter & 0xFF); }
}
public void Increment()
{
m_Counter++;
}
}
private CryptCounter _decryptCounter;
private CryptCounter _encryptCounter;
private static byte[] _cryptKey1;
private static byte[] _cryptKey2;
public static void PrepareAuthCryptography()
{
if (_cryptKey1 != null)
{
if (_cryptKey1.Length != 0)
return;
}
_cryptKey1 = new byte[0x100];
_cryptKey2 = new byte[0x100];
byte i_key1 = 0x9D;
byte i_key2 = 0x62;
for (int i = 0; i < 0x100; i++)
{
_cryptKey1[i] = i_key1;
_cryptKey2[i] = i_key2;
i_key1 = (byte)((0x0F + (byte)(i_key1 * 0xFA)) * i_key1 + 0x13);
i_key2 = (byte)((0x79 - (byte)(i_key2 * 0x5C)) * i_key2 + 0x6D);
}
}
public AuthCryptography()
{
_encryptCounter = new CryptCounter();
_decryptCounter = new CryptCounter();
}
public void Encrypt(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
_encryptCounter.Increment();
}
}
public void Decrypt(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)0xAB;
buffer[i] = (byte)(buffer[i] >> 4 | buffer[i] << 4);
buffer[i] ^= (byte)(_cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1]);
_decryptCounter.Increment();
}
}
public void Decrypt4(byte[] buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] ^= (byte)(_cryptKey1[_encryptCounter.Key1] ^ _cryptKey2[_encryptCounter.Key2]);
buffer[i] = (byte)(buffer[i] << 4 | buffer[i] >> 4);
buffer[i] ^= (byte)0xAB;
_encryptCounter.Increment();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解密应该是加密的逆过程,在这种情况下(可逆操作的管道),只需反转操作即可。像这样(未测试):
这相当于:
The decrypt should be the inverse of the encrypt, in this case (a pipeline of reversible operations), just reverse the operations. Like this (not tested):
That's equivalent to: