douneiben2240 2016-04-28 15:01
浏览 111

使用php中的静态密钥解密CRC32(使用C#类生成CRC添加)

This is my class to create a CRC32 hash. Its not my class I found it on internet ofc.. But to the point I can't manage to get SSL working or AES because I have really only basic coding knowledge. I am making launcher for my little game nothing big I dont need super strong encryption I only need to hide client queries from casuals.

Somewhere on the internet people said CRC32 is one way hash but it obviously is not. Within the class is function for encrypt and decrypt both work very well.

I have tested sending hashed and unhashed In future I plan to create a hidden file on users harddrive with random text and take its checksum and use it as the key for CRC generation and also as identifier for player machine. The key will be send only at first launch of application. I know its not secure but its enough for me.

// Copyright (c) Damien Guard.  All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Originally published at http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Namechanger
{
/// <summary>
/// Implements a 32-bit CRC hash algorithm compatible with Zip etc.
/// </summary>
/// <remarks>
/// Crc32 should only be used for backward compatibility with older file formats
/// and algorithms. It is not secure enough for new applications.
/// If you need to call multiple times for the same data either use the HashAlgorithm
/// interface or remember that the result of one Compute call needs to be ~ (XOR) before
/// being passed in as the seed for the next Compute call.
/// </remarks>
public class StringEncryption
{
    private readonly Random random;
    private readonly byte[] key;
    private readonly RijndaelManaged rm;
    private readonly UTF8Encoding encoder;

    public object Assert { get; private set; }





    public StringEncryption()
    {
        this.random = new Random();
        this.rm = new RijndaelManaged();
        this.encoder = new UTF8Encoding();
        this.key = Convert.FromBase64String("cGFzc3dvcmRz");
    }

    public string Encrypt(string unencrypted)
    {
        var vector = new byte[16];
        this.random.NextBytes(vector);
        var cryptogram = vector.Concat(this.Encrypt(this.encoder.GetBytes(unencrypted), vector));
        return Convert.ToBase64String(cryptogram.ToArray());
    }

    public string Decrypt(string encrypted)
    {
        var cryptogram = Convert.FromBase64String(encrypted);
        if (cryptogram.Length < 17)
        {
            throw new ArgumentException("Not a valid encrypted string", "encrypted");
        }

        var vector = cryptogram.Take(16).ToArray();
        var buffer = cryptogram.Skip(16).ToArray();
        return this.encoder.GetString(this.Decrypt(buffer, vector));
    }

    private byte[] Encrypt(byte[] buffer, byte[] vector)
    {
        var encryptor = this.rm.CreateEncryptor(this.key, vector);
        return this.Transform(buffer, encryptor);
    }

    private byte[] Decrypt(byte[] buffer, byte[] vector)
    {
        var decryptor = this.rm.CreateDecryptor(this.key, vector);
        return this.Transform(buffer, decryptor);
    }

    private byte[] Transform(byte[] buffer, ICryptoTransform transform)
    {
        var stream = new MemoryStream();
        using (var cs = new CryptoStream(stream, transform, CryptoStreamMode.Write))
        {
            cs.Write(buffer, 0, buffer.Length);
        }

        return stream.ToArray();
    }

    public void EncryptDecrypt()
    {
        // Arrange
        var subject = new StringEncryption();
        var originalString = "Testing123!£$";

        // Act
        var encryptedString1 = subject.Encrypt(originalString);
        var encryptedString2 = subject.Encrypt(originalString);
        var decryptedString1 = subject.Decrypt(encryptedString1);
        var decryptedString2 = subject.Decrypt(encryptedString2);
    }
}

Mycode what I am sending to php

var subject = new StringEncryption();
var originalString = "" + getCheckSumFile(assemblypath) + " " + unixTimestamp + "";
var hashed = subject.Encrypt(originalString);
var unhashed = subject.Decrypt(hashed);

string str = string.Concat("http://127.0.0.1/here/?nick=", nick, "&server=", server, "&data=", hashed);
Network.getData(str);

So far everyting works for me only I dont know how to decrypt the CRC32 with a key using php? I havent find any info on this at all most php guides uses only crc hash with no intention to decrypt it with key.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值