首先带入阿里云的依赖,里面有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);