java字符串加密_在JAVA中快速简单的字符串加密/解密

该博客详细介绍了如何在Java中使用DES算法进行数据加密和解密。通过创建DESKeySpec,使用SecretKeyFactory生成SecretKey,然后利用Cipher进行加密和解密操作。同时,代码中还涉及到BASE64编码和解码的过程。
摘要由CSDN通过智能技术生成

小编典典

链接上方的代码

DESKeySpec keySpec = new DESKeySpec("Your secret Key phrase".getBytes("UTF8"));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

SecretKey key = keyFactory.generateSecret(keySpec);

sun.misc.BASE64Encoder base64encoder = new BASE64Encoder();

sun.misc.BASE64Decoder base64decoder = new BASE64Decoder();

.........

// ENCODE plainTextPassword String

byte[] cleartext = plainTextPassword.getBytes("UTF8");

Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe

cipher.init(Cipher.ENCRYPT_MODE, key);

String encryptedPwd = base64encoder.encode(cipher.doFinal(cleartext));

// now you can store it

......

// DECODE encryptedPwd String

byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedPwd);

Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));

2020-09-18

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值