解密用户信息偶尔会出现乱码,怎么解决?
发布于 5 年前 作者 yang80 12738 次浏览 来自 问答
String data = "6n8O4a/8pKUBkg6f1MhKAnL83hbpdCrLGa7yeVo0J2QtDxshWFVdtXz/kDMX8rUHWTHXSEV4N3pQeifqWYRk+/WLLTf1/y1O4gGnOoTvwnU8jNN4GEMokBR+qtf75btaDq89wQy5IFdXhvivyaIO9EwFFm+ncutg5Y/BimKxAo4+leJHjiUNyXmZCVzz2wluNXogBuYNEoIOY6p/y5dE0naKS/3/5KyA1TZt8p9B8XPRXEFVucWNc7c80ou2WXCXCn1P8s+O3w59/hw9EtxUwuDwp3CObKlrPD4GT44rl9l64hk4Q2AST/8IspvRCFgtWew2l6hZeGMYTmziBH2mozbUrSizACi45T2sZk3RyWxPteZIAH2HzubsIlxbZ/XiGKRKINus7V3u4yuJlON7IYKLISpWzOTUkWGnKd15o3oO8d0fI2gsoeuXK1pNWdqS2Vfs0R5teSlFbsqcztVfRkzEymwEb+8wEOpZ/pkxWlWpbfb5vdo2QFaPC5fNU7pONA65JyKihfFqThhIeqAt1A==";
String sessionKey = "1jD\\/wapVuP0zcy0uNJm5Dg==";
String iv = "nztsHOp34sQwgXtX07x5cw=="

解密方法

public static String decrypt(String sessionKey, String encryptedData, String ivStr) {
    try {
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(org.apache.commons.codec.binary.Base64.decodeBase64(ivStr)));
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(2, new SecretKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(sessionKey), "AES"), params);
        return new String(Pkcs7Encoder.decode(cipher.doFinal(org.apache.commons.codec.binary.Base64.decodeBase64(encryptedData))), StandardCharsets.UTF_8);
    } catch (Exception var5) {
        throw new BadException("AES解密失败");
    }
}
回到顶部