欢迎光临
我们一直在努力

des对称加密解密,des加密是对称加密吗

import com.alibaba.fastjson.JSONObject;import org.apache.commons.codec.binary.Base64;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import java.security.SecureRandom;import java.util.HashMap;import java.util.Map;/** * description: */public class DESCUtils { private static final String key = “DES”; /** * @param data * @param password * @param encryptOrDecryptMode Cipher.ENCRYPT_MODE Cipher.DECRYPT_MODE * @return * @throws Exception */ public static byte[] encryptOrDecrypt(byte[] data, String password, int encryptOrDecryptMode) throws Exception { SecureRandom secureRandom = new SecureRandom(); DESKeySpec desKeySpec = new DESKeySpec(password.getBytes()); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(key); //密钥工厂 SecretKey 美国高防vps secretKey = secretKeyFactory.generateSecret(desKeySpec); //生成密钥 Cipher cipher = Cipher.getInstance(key); cipher.init(encryptOrDecryptMode, secretKey, secureRandom); return cipher.doFinal(data); } public static String encode(String data, String password) throws Exception { return Base64.encodeBase64String(encryptOrDecrypt(data.getBytes(“UTF-8”), password, Cipher.ENCRYPT_MODE)); } public static String decode(String data, String password) throws Exception { return new String(encryptOrDecrypt(Base64.decodeBase64(data), password, Cipher.DECRYPT_MODE), “UTF-8”); } public static void main(String标致的小蝴蝶 = “hjkg&t*jf/”; Map<String, Object> map = new HashMap<>(); map.put(“user”, “jack”); String s = JSONObject.toJSONString(map); System.out.println(s); try { String encode = encode(s, password); System.out.println(“encode: ” + encode); String decode = decode(encode, password); System.out.println(“decode: ” + decode); } catch (Exception e) { e.printStackTrace(); } }}

<p重要的眼睛结果:

{“user”:”jack”}
encode: CBEUKiVQKk0fiV7EWEsfVQ==
decode: {“user”:”jack”}

56047488

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。