導航:首頁 > 編程語言 > crc8java

crc8java

發布時間:2022-10-11 06:04:19

1. 如何把下面這段c語言的crc8加密寫成java

以下是我的分析,不知是否正確,你參考下

1、首先來看你打java代碼 :crc = (byte) ((crc >> 1) ^ 0x8c); 和crc = (byte) (crc >> 1);

packagecom.test;

publicclasstest{

publicstaticvoidmain(String[]args){
byte[]ptr={1,1,1,1,1,1};
byteres=getCrc(ptr);
System.out.println();
System.out.println((byte)((1>>1)^0x8c)+":"+((1>>1)^0x8c));
}

publicstaticbytegetCrc(byte[]ptr){
intcrc=0;
for(inti=0;i<ptr.length;i++){
crc^=ptr[i];
for(intj=0;j<8;j++){
if((crc&0x01)!=0){
crc=(crc>>1)^0x8c;
}else{
crc=crc>>1;
}
}
}
return(byte)crc;
}
}


2. java的crc8校驗,按指定多項式,求助

以下是我的分析,不知是否正確,你參考下1、首先來看你打java代碼:crc=(byte)((crc>>1)^0x8c);和 crc=(byte)(crc>>1); 導致這個問題是因為byte的最高位符號位,轉換的時候就出錯了2、示例代碼:package com.test;public class test {public static void main(String[] args) {byte[] ptr = { 1, 1, 1, 1, 1, 1 };byte res = getCrc(ptr);System.out.println();System.out.println((byte)( (1 >> 1) ^ 0x8c ) + ":" +( (1 >> 1) ^ 0x8c ) );}public static byte getCrc(byte[] ptr) {int crc = 0;for (int i = 0; i > 1) ^ 0x8c;} else {crc = crc >> 1;}}}return (byte) crc;}}

3. 如何用java實現一個加密演算法(現有的這個是C寫的,想要一個java的)菜鳥一個,求高手幫忙····

import java.io.*; //引入io包
public class TxtEncrypt
{
public static void main(String args[ ]) //主函數
{
System.out.println("輸入一個字元作為密鑰:");
char key = System.in.read();//等待用戶輸入一個字元作為密鑰
File fileOne=new File("hello.txt"), //要加密的文件
fileTwo=new File("hello.secret");//加密後的文件
char b[]=new char[100]; //緩沖字元數組
try{
FileReader in=new FileReader(fileOne); //創建讀文件流,也可以叫文件輸入流
FileWriter out=new FileWriter(fileTwo); //創建寫文件流,也可以叫文件輸出流
int n=-1; //定義變數,存放read讀的返回值
while((n=in.read(b))!=-1) //如果變數n為-1,則文件到達末尾
{
for(int i=0;i<n;i++) //循環,從字元數組b中讀取字元
{
b[i]=(char)(b[i]^key); //加密,講字元數組b中的每個字元跟a異或,然後放入b中
}
out.write(b,0,n); //然後把b中的所有字元寫入到writer流中,也就是hello.secret文件中
}
out.close(); //讀完之後,關閉輸出流
in=new FileReader(fileTwo); //創建hello.secret文件的讀文件流
System.out.println("加密後的文件內容:"); //輸出字元串
while((n=in.read(b))!=-1) //當文件沒有到達末尾,一直執行下面的循環體
{
String str=new String(b,0,n); //把b中的字元放入字元串變數str中
System.out.println(str); //輸出str中的值 (整個循環的意思是把加密後的文件輸出到屏幕上)
}
in=new FileReader(fileTwo); //創建hello.secret文件的讀文件流
System.out.println("解密後的文件內容:"); //輸出字元串
while((n=in.read(b))!=-1) //當文件沒有到達末尾,一直執行下面的循環體
{
for(int i=0;i<n;i++) //for循環
{
b[i]=(char)(b[i]^key); //把b字元數組中的每個字元跟a異或(同一個字元異或兩次之後,得到原來的字元)
}
System.out.println(new String(b,0,n)); //輸出b中的所有字元
}
in.close(); //關閉輸入流
}
catch(IOException e)
{
System.out.println(e); //捕獲io異常
}
}
}

4. 求java的 crc8演算法方法

你的意思就翻譯一下,是嗎:
static char crc8fun ( char in, char prest)
{
int loop;
char out;
char crc_pol=0xb8; /*多項式*/
out = in^prest;
for(loop=0;loop<8;loop++){
if(out&0x01){
out=(out>>1)^crc_pol;
}else{
out=(out>>1);
}
return out;
}
}

5. 怎麼用java實現crc8校驗

查一下API 有類可以直接調用

6. 如何用java實現CRC8驗證演算法

/*http://www.koders.com/java/.aspx?s=Address#L34
*---------------------------------------------------------------------------
* Copyright (C) 1999,2000 Dallas Semiconctor Corporation, All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, , modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above right notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Dallas Semiconctor
* shall not be used except as stated in the Dallas Semiconctor
* Branding Policy.
*---------------------------------------------------------------------------
*/

package com.dalsemi.onewire.utils;

/**
* CRC8 is a class to contain an implementation of the
* Cyclic-Rendency-Check CRC8 for the iButton. The CRC8 is used
* in the 1-Wire Network address of all iButtons and 1-Wire
* devices.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @version 0.00, 28 Aug 2000
* @author DS
*
*/
public class CRC8
{

//--------
//-------- Variables
//--------

/**
* CRC 8 lookup table
*/
private static byte dscrc_table [];

/*
* Create the lookup table
*/
static
{

//Translated from the assembly code in iButton Standards, page 129.
dscrc_table = new byte [256];

int acc;
int crc;

for (int i = 0; i < 256; i++)
{
acc = i;
crc = 0;

for (int j = 0; j < 8; j++)
{
if (((acc ^ crc) & 0x01) == 0x01)
{
crc = ((crc ^ 0x18) >> 1) | 0x80;
}
else
crc = crc >> 1;

acc = acc >> 1;
}

dscrc_table [i] = ( byte ) crc;
}
}

//--------
//-------- Constructor
//--------

/**
* Private constructor to prevent instantiation.
*/
private CRC8 ()
{
}

//--------
//-------- Methods
//--------

/**
* Perform the CRC8 on the data element based on the provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc data element on which to perform the CRC8
* @param seed seed the CRC8 with this value
* @return CRC8 value
*/
public static int compute (int dataToCRC, int seed)
{
return (dscrc_table [(seed ^ dataToCRC) & 0x0FF] & 0x0FF);
}

/**
* Perform the CRC8 on the data element based on a zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc data element on which to perform the CRC8
* @return CRC8 value
*/
public static int compute (int dataToCRC)
{
return (dscrc_table [dataToCRC & 0x0FF] & 0x0FF);
}

/**
* Perform the CRC8 on an array of data elements based on a
* zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [])
{
return compute(dataToCrc, 0, dataToCrc.length);
}

/**
* Perform the CRC8 on an array of data elements based on a
* zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param off offset into array
* @param len length of data to crc
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int off, int len)
{
return compute(dataToCrc, off, len, 0);
}

/**
* Perform the CRC8 on an array of data elements based on the
* provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param off offset into array
* @param len length of data to crc
* @param seed seed to use for CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int off, int len, int seed)
{

// loop to do the crc on each data element
int CRC8 = seed;

for (int i = 0; i < len; i++)
CRC8 = dscrc_table [(CRC8 ^ dataToCrc [i + off]) & 0x0FF];

return (CRC8 & 0x0FF);
}

/**
* Perform the CRC8 on an array of data elements based on the
* provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param seed seed to use for CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int seed)
{
return compute(dataToCrc, 0, dataToCrc.length, seed);
}
}

閱讀全文

與crc8java相關的資料

熱點內容
android平滑滾動效果 瀏覽:841
什麼是編譯器指令 瀏覽:219
微控制器邏輯命令使用什麼匯流排 瀏覽:885
程序員在學校里是學什麼的 瀏覽:601
oraclejava數據類型 瀏覽:890
程序員考注冊會計師 瀏覽:957
怎麼使用access的命令按鈕 瀏覽:899
有點錢app在哪裡下載 瀏覽:832
博途v15解壓後無法安裝 瀏覽:205
什麼是根伺服器主機 瀏覽:438
安卓手游怎麼申請退款 瀏覽:555
安卓系統如何分享網頁 瀏覽:278
ad如何編譯pcb工程 瀏覽:414
除了滴滴app哪裡還能用滴滴 瀏覽:399
截圖怎麼保存文件夾然後壓縮 瀏覽:8
幻影伺服器怎麼樣 瀏覽:28
具體哪些廣東公司招程序員 瀏覽:870
嵌入式編譯器教程 瀏覽:307
ssl數據加密傳輸 瀏覽:87
51單片機定時器方式2 瀏覽:332