我如何在 C# 中使此代码从加密到解密我使用 Xor 和 Or

发布于 2025-01-08 13:43:29 字数 2510 浏览 0 评论 0原文

我尝试使用此代码对其进行解密,但有问题我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2025-01-15 13:43:29

解密应该是加密的逆过程,在这种情况下(可逆操作的管道),只需反转操作即可。像这样(未测试):

public static void Decrypt(byte[] buffer)
{
    for (int i = 0; i < buffer.Length; i++)
    {
        int temp = buffer[i] ^ _cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1];
        temp = (temp >> 4) | (temp << 4);
        temp ^= 0xAB;
        buffer[i] = (byte)temp;
        _decryptCounter.Increment();
    }
}

这相当于:

public static void Decrypt(byte[] buffer)
{
    for (int i = 0; i < buffer.Length; i++)
    {
        int temp = buffer[i] ^ 0xBA ^ _cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1];
        temp = (temp >> 4) | (temp << 4);
        buffer[i] = (byte)temp;
        _decryptCounter.Increment();
    }
}

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):

public static void Decrypt(byte[] buffer)
{
    for (int i = 0; i < buffer.Length; i++)
    {
        int temp = buffer[i] ^ _cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1];
        temp = (temp >> 4) | (temp << 4);
        temp ^= 0xAB;
        buffer[i] = (byte)temp;
        _decryptCounter.Increment();
    }
}

That's equivalent to:

public static void Decrypt(byte[] buffer)
{
    for (int i = 0; i < buffer.Length; i++)
    {
        int temp = buffer[i] ^ 0xBA ^ _cryptKey2[_decryptCounter.Key2] ^ _cryptKey1[_decryptCounter.Key1];
        temp = (temp >> 4) | (temp << 4);
        buffer[i] = (byte)temp;
        _decryptCounter.Increment();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文