您的位置:首页 > 科技 > 能源 > 宁波效果图公司_制作图片的软件加字体_seo销售代表招聘_适合推广的app有哪些

宁波效果图公司_制作图片的软件加字体_seo销售代表招聘_适合推广的app有哪些

2024/11/16 22:45:51 来源:https://blog.csdn.net/weixin_43497935/article/details/143715959  浏览:    关键词:宁波效果图公司_制作图片的软件加字体_seo销售代表招聘_适合推广的app有哪些
宁波效果图公司_制作图片的软件加字体_seo销售代表招聘_适合推广的app有哪些

假如使用的JDK1.8不支持PKCS7填充方式,需要添加依赖

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.64</version>
</dependency>

AESUtils

package com.hm.utils;import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Base64;public class AESUtils {private static final String transformation = "AES/CBC/PKCS7Padding";private static final String algorithm = "AES";public static String encrypt(byte[] key, byte[] initVector, String value) {try {IvParameterSpec iv = new IvParameterSpec(initVector);SecretKeySpec skeySpec = new SecretKeySpec(key, algorithm);Cipher cipher = Cipher.getInstance(transformation);cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);byte[] encrypted = cipher.doFinal(value.getBytes());return Base64.getEncoder().encodeToString(encrypted);} catch (Exception ex) {ex.printStackTrace();}return null;}public static String decrypt(String key, String initVector, String encrypted) {try {IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), algorithm);Cipher cipher = Cipher.getInstance(transformation);cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));return new String(original);} catch (Exception ex) {ex.printStackTrace();}return null;}// 对数据进行PKCS7补位public static byte[] addPKCS7Padding(byte[] keyByte, int blockSize) {int groups = keyByte.length / blockSize + (keyByte.length % blockSize != 0 ? 1 : 0);byte[] temp = new byte[groups * blockSize];Arrays.fill(temp, (byte) 0);System.arraycopy(keyByte, 0, temp, 0, keyByte.length);keyByte = temp;return keyByte;}
}
package com.hm.utils.wms;import com.hm.utils.AESUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Component;import java.nio.charset.StandardCharsets;
import java.security.Security;/*** WMS系统接口对接工具类** @author Xu Wei* @version 1.0* @aate 2024/11/12 10:54*/
@Component
public class WmsUtil {static {Security.addProvider(new BouncyCastleProvider());}public static void main(String[] args) {int client = 1; // 1表示风卡,2表示名家(这里只是模拟,实际应用中可能从其他地方获取这个值)String key = "123";byte[] temp = AESUtils.addPKCS7Padding(key.getBytes(), 32);String iv = "1234567890abcdef";String data = "data数据";System.out.println(AESUtils.encrypt(temp, iv.getBytes(StandardCharsets.UTF_8), data));}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com