“将字节数组转换为十六进制字符串”显示错误

时间:2014-09-30 14:54:28

标签: java arrays encryption bytearray

我正在尝试计算邮件验证码(MAC)。

现在最后,当我尝试通过从字节数组转换MAC来显示MAC时,它会在该行中显示错误。

这就是我所做的:

    import com.sun.jmx.mbeanserver.Util;
import java.io.FileInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        Decrypt main = new Decrypt();
        main.decryption();

                //MAC calculation begins
        System.out.println();

        System.out.println("The message authentication code (MAC) is the following: ");
        System.out.println();
        String d_file = "lab1Store";
        KeyGenerator kg = KeyGenerator.getInstance("DES");
        kg.init(56);
        SecretKey key = kg.generateKey();
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(key);

        FileInputStream fis = new FileInputStream(d_file);
        byte[] data_byte = new byte[1024];
        int read = fis.read(data_byte);
        while (read > 0) {
            mac.update(data_byte, 0, read);
            read = fis.read(data_byte);

        };
        byte[] macbytes = mac.doFinal();
        System.out.println("MAC(in hex):: " +Util.byteArray2Hex(macbytes)); //Shows error in this line

    }
}

0 个答案:

没有答案