java base64 密钥_JAVA JDK(8+)实现Base64加密

/*

base64算法是基于64个字符的一种替换算法。base64加密的产生式电子邮件的“历史问题”——邮件只能传输ASCII码。

base64加密的应用场景:email、密钥、证书文件。该算法可以由3种方式实现:JDK、Bouncy Castle、Commons Codec。

*/

package Base64;

import java.util.Base64;

import java.util.Base64.Encoder;

import java.util.Base64.Decoder;

public class Base64Test {

// 需要加密的原始字符串

private static Stringtext ="面向对象";

public static void main(String[] args) {

System.out.println("原始字符串:" +text);

jdkBase64();

}

/** JDK实现Base64编码 :适用于JDK8及以后的版本*/

public static void jdkBase64() {

Encoder encoder = Base64.getEncoder();

byte[] textEncoder = encoder.encode(text.getBytes());

System.out.println("JDK实现的base64解码:" +new String(textEncoder));

Decoder decoder = Base64.getDecoder();

byte[] textDecoder = decoder.decode(textEncoder);

System.out.println("JDK实现的base64解码:" +new String(textDecoder));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在你的 Maven 项目中添加以下依赖项: ```xml <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.14</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.68</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>1.68</version> </dependency> </dependencies> ``` 其中,`commons-codec` 用于 Base64 编码,`bcprov-jdk15on` 和 `bcpkix-jdk15on` 用于 RSA 加密和解密。 接下来,生成 RSA 密钥对: ```java import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; public class RSAUtil { public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); return generator.generateKeyPair(); } } ``` 然后,使用公钥加密和私钥解密: ```java import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Security; import javax.crypto.Cipher; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Base64; public class RSAUtil { static { Security.addProvider(new BouncyCastleProvider()); } public static String encrypt(String data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(data.getBytes()); return new String(Base64.encode(encryptedBytes), "UTF-8"); } public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] encryptedBytes = Base64.decode(encryptedData.getBytes("UTF-8")); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } } ``` 最后,使用 `split` 方法对加密后的字符串进行分段: ```java import org.apache.commons.lang3.StringUtils; public class RSAUtil { public static String encrypt(String data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal(data.getBytes()); String encryptedStr = new String(Base64.encode(encryptedBytes), "UTF-8"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encryptedStr.length(); i += 100) { sb.append(StringUtils.substring(encryptedStr, i, i + 100)).append("\n"); } return sb.toString(); } } ``` 这样,就可以使用 RSA 加密和解密,以及对加密后的字符串进行分段了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值