springboot项目中对图片进行rsa加密解密

首先带入阿里云的依赖,里面有rsa加密解密的函数

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.38.183.ALL</version>
</dependency>

进行图片的加密解密

// 假设图片存储在本地文件系统的路径
String imagePath = "D:\\work\\tupian\\xiaogou.jpg";
// 读取图片字节数组
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
// 将字节数组转换为Base64字符串
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
RSA2Encryptor rsa2Encryptor = new RSA2Encryptor();

//此为加密
cipher = rsa2Encryptor.encrypt(base64Image, "UTF-8", publickey);

//此为解密
String decrypt = rsa2Encryptor.decrypt(cipher, "UTF-8", privatekey);
//解析base64字节
byte[] decode = Base64.getDecoder().decode(decrypt);
// 确定保存还原图片的路径和文件名
String restoredImagePath = "D:\\work\\tupian\\xiaogou_e.jpg";
// 将字节数组写入文件
Path write = Files.write(Paths.get(restoredImagePath), decode);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值