如何将十六进制转换为base64

发布于 2024-12-04 10:21:46 字数 260 浏览 1 评论 0原文

如何将十六进制字符串转换为base64?我找到了这个页面 http://home2.paulschou.net/tools/xlate/ 但我需要java中的一些函数: String base64 = ...decoder(String hex); 我在互联网上找到了一些东西,但它们很难并且使用字节数组。我正在寻找更简单的东西 非常感谢

how to convert hex string into base64 ? I found this page http://home2.paulschou.net/tools/xlate/ but I need some function in java: String base64 = ...decoder(String hex); I found something in the Internet but they are to difficult and use byte array. I am looking for something simplier
thx a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

不语却知心 2024-12-11 10:21:46

看看 Commons Codec

 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;

byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.encodeBase64(decodedHex);

Have a look at Commons Codec :

 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.codec.binary.Hex;

byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.encodeBase64(decodedHex);
梦醒灬来后我 2024-12-11 10:21:46

您不太可能找到直接从十六进制转换为 base-64 的东西。您需要找到或编写一个十六进制解码器和一个 base-64 编码器,并使用 byte[] 作为中间形式。

将现实与您的要求进行比较:

String b64 = encoder.encode(decoder.decode(hex)); /* Too difficult :( !!! */

String b64 = transcoder.transcode(hex);           /* So much simpler! */

You are not likely to find something that converts directly from hex to base-64. You'll need to find or write a hex decoder and a base-64 encoder, and use a byte[] as an intermediate form.

Compare reality with what you are asking for:

String b64 = encoder.encode(decoder.decode(hex)); /* Too difficult :( !!! */

versus

String b64 = transcoder.transcode(hex);           /* So much simpler! */
蓦然回首 2024-12-11 10:21:46

您可能想查看此代码。我在谷歌上找到了它

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class MD5 {

    static char[] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String getBase64FromHEX(String input) {

        byte barr[] = new byte[16];
        int bcnt = 0;
        for (int i = 0; i < 32; i += 2) {
            char c1 = input.charAt(i);
            char c2 = input.charAt(i + 1);
            int i1 = intFromChar(c1);
            int i2 = intFromChar(c2);

            barr[bcnt] = 0;
            barr[bcnt] |= (byte) ((i1 & 0x0F) << 4);
            barr[bcnt] |= (byte) (i2 & 0x0F);
            bcnt++;
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(barr);
    }

    public static synchronized String getMD5_Base64(String input) {
        // please note that we dont use digest, because if we
        // cannot get digest, then the second time we have to call it
        // again, which will fail again
        MessageDigest digest = null;

        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (digest == null)
            return input;

        // now everything is ok, go ahead
        try {
            digest.update(input.getBytes("UTF-8"));
        } catch (java.io.UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        byte[] rawData = digest.digest();
        BASE64Encoder bencoder = new BASE64Encoder();
        return bencoder.encode(rawData);
    }

    private static int intFromChar(char c) {
        char clower = Character.toLowerCase(c);
        for (int i = 0; i < carr.length; i++) {
            if (clower == carr[i]) {
                return i;
            }
        }

        return 0;
    }

    public static void main(String[] args) {

        //String password = args[0];
        String password = "test";

        MessageDigest digest = null;

        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        try {
            digest.update(password.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        byte[] rawData = digest.digest();
        StringBuffer printable = new StringBuffer();

        for (int i = 0; i < rawData.length; i++) {
            printable.append(carr[((rawData[i] & 0xF0) >> 4)]);
            printable.append(carr[(rawData[i] & 0x0F)]);
        }
        String phpbbPassword = printable.toString();

        System.out.println("PHPBB           : " + phpbbPassword);
        System.out.println("MVNFORUM        : " + getMD5_Base64(password));
        System.out.println("PHPBB->MVNFORUM : " + getBase64FromHEX(phpbbPassword));
    }

}

You might want to check out this code. I found it on google

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class MD5 {

    static char[] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String getBase64FromHEX(String input) {

        byte barr[] = new byte[16];
        int bcnt = 0;
        for (int i = 0; i < 32; i += 2) {
            char c1 = input.charAt(i);
            char c2 = input.charAt(i + 1);
            int i1 = intFromChar(c1);
            int i2 = intFromChar(c2);

            barr[bcnt] = 0;
            barr[bcnt] |= (byte) ((i1 & 0x0F) << 4);
            barr[bcnt] |= (byte) (i2 & 0x0F);
            bcnt++;
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(barr);
    }

    public static synchronized String getMD5_Base64(String input) {
        // please note that we dont use digest, because if we
        // cannot get digest, then the second time we have to call it
        // again, which will fail again
        MessageDigest digest = null;

        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (digest == null)
            return input;

        // now everything is ok, go ahead
        try {
            digest.update(input.getBytes("UTF-8"));
        } catch (java.io.UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        byte[] rawData = digest.digest();
        BASE64Encoder bencoder = new BASE64Encoder();
        return bencoder.encode(rawData);
    }

    private static int intFromChar(char c) {
        char clower = Character.toLowerCase(c);
        for (int i = 0; i < carr.length; i++) {
            if (clower == carr[i]) {
                return i;
            }
        }

        return 0;
    }

    public static void main(String[] args) {

        //String password = args[0];
        String password = "test";

        MessageDigest digest = null;

        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        try {
            digest.update(password.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        byte[] rawData = digest.digest();
        StringBuffer printable = new StringBuffer();

        for (int i = 0; i < rawData.length; i++) {
            printable.append(carr[((rawData[i] & 0xF0) >> 4)]);
            printable.append(carr[(rawData[i] & 0x0F)]);
        }
        String phpbbPassword = printable.toString();

        System.out.println("PHPBB           : " + phpbbPassword);
        System.out.println("MVNFORUM        : " + getMD5_Base64(password));
        System.out.println("PHPBB->MVNFORUM : " + getBase64FromHEX(phpbbPassword));
    }

}
云之铃。 2024-12-11 10:21:46

首先,您必须导入 Apache Commons Codec 库!
https://commons.apache.org/proper/commons- codec/archives/1.9/index.html

这里是1.9版本的API
http://commons.apache.org/proper/ commons-codec/archives/1.9/apidocs/index.html

那么你必须遵循以下3个步骤

       //convert String to char array (1st step)
      char[] charArray = myhexString.toCharArray();

      // decode the char array to byte[] (2nd step)
      byte[] decodedHex = Hex.decodeHex(charArray);

    // The String decoded to Base64 (3rd step)
    String result= Base64.encodeBase64String(decodedHex);

At first you have to import the Apache Commons Codec library!
https://commons.apache.org/proper/commons-codec/archives/1.9/index.html

Here is the API for 1.9 version
http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/index.html

Then you have to follow these 3 steps

       //convert String to char array (1st step)
      char[] charArray = myhexString.toCharArray();

      // decode the char array to byte[] (2nd step)
      byte[] decodedHex = Hex.decodeHex(charArray);

    // The String decoded to Base64 (3rd step)
    String result= Base64.encodeBase64String(decodedHex);
在你怀里撒娇 2024-12-11 10:21:46

我在 Scala 中的实现

import java.math.BigInteger
import org.apache.commons.codec.digest.MessageDigestAlgorithms
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.codec.binary.Base64

object HashBase64 {

  import MessageDigestAlgorithms._

  def main(args: Array[String]): Unit = {
    println(hash64("my-value")) // print("CCipycIAdVxRK2IG2tFHow==")
  }

  protected def hash64(input: String) = {
    val hashBase16 = new DigestUtils(MD5).digestAsHex(input) // some hex string
    val base10 = new BigInteger(hashBase16, 16) // base 10 int
    val base64 = new String(Base64.encodeInteger(base10)) // base64 string
    base64
  }
}

My implementation in Scala

import java.math.BigInteger
import org.apache.commons.codec.digest.MessageDigestAlgorithms
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.codec.binary.Base64

object HashBase64 {

  import MessageDigestAlgorithms._

  def main(args: Array[String]): Unit = {
    println(hash64("my-value")) // print("CCipycIAdVxRK2IG2tFHow==")
  }

  protected def hash64(input: String) = {
    val hashBase16 = new DigestUtils(MD5).digestAsHex(input) // some hex string
    val base10 = new BigInteger(hashBase16, 16) // base 10 int
    val base64 = new String(Base64.encodeInteger(base10)) // base64 string
    base64
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文