Convert.FromBase64String 方法的 Java 等效项

发布于 2024-08-22 11:51:21 字数 705 浏览 6 评论 0原文

是否有一些相当于 Convert.FromBase64String 的 Java

将指定的字符串(将二进制数据编码为 base-64 数字)转换为等效的 8 位无符号整数数组。

我尝试过:

如有任何建议,将不胜感激!

Is there some Java equivalent of Convert.FromBase64String which

Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.

I have tried:

Will be grateful for any suggestions!

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

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

发布评论

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

评论(2

夜无邪 2024-08-29 11:51:21

一般来说,如果标准 Java 库没有您需要的东西,Apache Commons 框架很可能有。在这种情况下,您需要 decodeBase64 来自 commons.codec.binary.Base64

In general, chances are good that if the standard Java libraries don't have something you need, the Apache Commons framework does. In this case, you want the decodeBase64 method from commons.codec.binary.Base64.

烟火散人牵绊 2024-08-29 11:51:21

密钥和初始向量使用 C#AES 加密中的 Convert.FromBase64String 进行转换。

在java中,您可以使用DatatypeConverter.parseBase64Binary(string)方法。为了使用它 import import javax.xml.bind.*;

这是示例程序

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.*;

class AESEncryption {

    private static final String key = "type your key here.(base64)";
    private static final String initVector = "type your initial vector here.(base64)";

    static byte[] encodedKey = DatatypeConverter.parseBase64Binary(key);
    static byte[] encodedIV = DatatypeConverter.parseBase64Binary(initVector);

    public static String encrypt(final String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());

            return Base64.getEncoder().encodeToString(encrypted);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String decrypt(final String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));

            return new String(original);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static void main(final String[] args) {

        String originalString = "Here is your text to be encrypted.";
        System.out.println("Original String to encrypt - " + originalString);

        String encryptedString = encrypt(originalString);
        System.out.println("Encrypted String - " + encryptedString);

        String decryptedString = decrypt(encryptedString);
        System.out.println("After decryption - " + decryptedString);
    }
}

The key and the initial vector are converted using Convert.FromBase64String inAES encryption in C#.

In java, you can use DatatypeConverter.parseBase64Binary(string) method. In order to use it import import javax.xml.bind.*;

Here is the example program

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.*;

class AESEncryption {

    private static final String key = "type your key here.(base64)";
    private static final String initVector = "type your initial vector here.(base64)";

    static byte[] encodedKey = DatatypeConverter.parseBase64Binary(key);
    static byte[] encodedIV = DatatypeConverter.parseBase64Binary(initVector);

    public static String encrypt(final String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());

            return Base64.getEncoder().encodeToString(encrypted);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String decrypt(final String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));

            return new String(original);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static void main(final String[] args) {

        String originalString = "Here is your text to be encrypted.";
        System.out.println("Original String to encrypt - " + originalString);

        String encryptedString = encrypt(originalString);
        System.out.println("Encrypted String - " + encryptedString);

        String decryptedString = decrypt(encryptedString);
        System.out.println("After decryption - " + decryptedString);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文