java公钥+cliper加密_qileilove

package com.lazycat.secure.aes;

import java.nio.charset.Charset;

import java.security.NoSuchAlgorithmException;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import javax.crypto.Cipher;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.spec.SecretKeySpec;

public class AESCoder {

public static int count = 1000000;

public static CountDownLatch latch =new CountDownLatch(count);

public static void main(String[] args)throws Exception {

ExecutorService pool = Executors.newFixedThreadPool(200);

final byte[] payload = "{\"msg\":{\"content\":{\"text\":\"JJH\",\"tplId\":0},\"from\":{\"name\":\"10000213\",\"id\":1,\"type\":0},\"to\":{\"name\":\"10095812\",\"id\":10000213,\"type\":0},\"time\":0,\"txid\":0,\"subtype\":1},\"type\":\"chat\"}".getBytes(Charset.forName("utf-8"));

final String secureKey ="BBmFdTFVgAjgHNwRkWWRcOFFiBzAANFU9DmMAP1JpBmc.";

long start = System.currentTimeMillis();

for(int i = 0 ; i

pool.execute(new Runnable() {

public void run() {

AESCoder coder = new AESCoder();

byte[] enret =null;

try {

enret = coder.encrypt(payload,secureKey);

byte[] deret = coder.decrypt(enret, secureKey);

System.out.println(new String(deret,"utf-8"));

} catch (Exception e) {

e.printStackTrace();

}

latch.countDown();

}

});

}

latch.await();

System.out.println(System.currentTimeMillis() - start);

pool.shutdown();

}

private byte[] encrypt(byte[] payload,String securekey)throws Exception{

byte[] enCodeFormat = securekey.substring(0, 16).getBytes();

SecretKeySpec key = newSecretKeySpec(enCodeFormat,"AES");

Cipher cipher = CliperInstance.getInstance();//创建密码器

//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key);//初始化

byte[] result = cipher.doFinal(payload);

return result;

}

public byte[] decrypt(byte[] buffer,StringsecureKey)throws Exception{

byte[] enCodeFormat = secureKey.substring(0,16).getBytes();

SecretKeySpec key = newSecretKeySpec(enCodeFormat,"AES");

//Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//创建密码器

Cipher cipher = CliperInstance.getInstance();

cipher.init(Cipher.DECRYPT_MODE, key);//初始化

byte[] result = cipher.doFinal(buffer);

return result;

}

}

class CliperInstance {

private static ThreadLocal cipherTL =new ThreadLocal(){

@Override

protected Cipher initialValue() {

try {

return Cipher.getInstance("AES/ECB/PKCS5Padding");

}catch(Exception e){

return null;

}

}

};

public static   CiphergetInstance() throws NoSuchAlgorithmException, NoSuchPaddingException{

returncipherTL.get();

}

}

  • 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、付费专栏及课程。

余额充值