CRC32 JAVA C#实现

项目中用到CRC32进行校验得地方,需要用到C#和java进行对比:

一、C#实现:

class CRC32Cls
    {
        protected ulong[] Crc32Table;
        //生成CRC32码表
        public void GetCRC32Table()
        {
            ulong Crc;
            Crc32Table = new ulong[256];
            int i, j;
            for (i = 0; i < 256; i++)
            {
                Crc = (ulong)i;
                for (j = 8; j > 0; j--)
                {
                    if ((Crc & 1) == 1)
                        Crc = (Crc >> 1) ^ 0xEDB88320;
                    else
                        Crc >>= 1;
                }
                Crc32Table[i] = Crc;
            }
        }

        //获取字符串的CRC32校验值
        public ulong GetCRC32Str(string sInputString)
        {
            //生成码表
            GetCRC32Table();
            //byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(sInputString);//根据自己不同得需求,跟java同步用即可保证统一
            byte[] buffer = System.Text.Encoding.Default.GetBytes(sInputString);
            //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sInputString);
            ulong value = 0xffffffff;
            int len = buffer.Length;
            for (int i = 0; i < len; i++)
            {
                value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ buffer[i]];
            }
            return value ^ 0xffffffff;
        }
    }

调用代码:

string value = String.Format("{0:X00000000}", CRC32Cls.GetCRC32Str(cont));//内容不会加0

string value = String.Format("{0:X8}", CRC32Cls.GetCRC32Str(cont)); //内容中最前面会加入0

二、JAVA实现:

package CRCTest;
import java.io.UnsupportedEncodingException;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class test {

    public static void main(String[] args) throws UnsupportedEncodingException {
        
        long con= crc32("测试123ABC!@#$%^&*()");
        System.out.print(Long.toHexString(con).toUpperCase());// TODO Auto-generated method stub

    }
    public static long crc32(String data) throws UnsupportedEncodingException {
        if (data == null) {
            return -1;
        }

        // get bytes from string
        byte bytes[] = data.getBytes("UTF-8");//这里编码需要跟C# 保持同步
        //byte bytes[] = data.getBytes();
        Checksum checksum = new CRC32();
        // update the current checksum with the specified array of bytes
        checksum.update(bytes, 0, bytes.length);
        // get the current checksum value
        return checksum.getValue();
    }
}

CRC32验证:

CRC(循环冗余校验)在线计算_ip33.com

CRC32 JAVA C#实现:

C# 和 Java 中CRC32 对应校验 - w_junguang - 博客园 (cnblogs.com)

用Java的CRC32类来计算CRC32校验和_java crc32-CSDN博客

C# BC库 AES DES MD5 SHA1 SHA256 实现:

C#中免费密码库BouncyCastle的使用详解_C#教程_脚本之家 (jb51.net)

C#版开源免费的Bouncy Castle密码库 - 追逐时光者 - 博客园 (cnblogs.com)
 

BigInteger详解

BigInteger详解 - AntzUhl - 博客园 (cnblogs.com)

  • 23
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值