请教Java中Cipher.getInstance(String)报错问题

ytxah 2019-08-29 02:55:31
我在写解密算法时用到语句Cipher.getInstance("DES/CBC/PKCS5Padding"),但编译器提示错误如下
package Decode;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class mail_Phone_PwdDe {
public static void main(String args[]) throws
NoSuchAlgorithmException, NoSuchPaddingException {
请问应该怎么解决?
...全文
1024 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Enchanter, 2020-06-29
  • 打赏
  • 举报
回复
因为你没有贴源码出来,先试试楼上的方法,应该可以解决你的问题,他这个加密方法是正确的
guishuanglin 2019-09-18
  • 打赏
  • 举报
回复

/**
	 * 加密数据
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return
	 */
	public static byte[] encryptForAES(byte[] content, String password) {
		if(content == null || content.length == 0 || password == null) return null;
		try {//不需要这么复杂的key
			//KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//kgen.init(128, new SecureRandom(password.getBytes("utf-8")));
			//SecretKey secretKey = kgen.generateKey();
			//byte[] enCodeFormat = secretKey.getEncoded();
			byte[] enCodeFormat = getBytesForUTF8(password);
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器,ECB模式,PKCS5Padding填充方式 AES/ECB/PKCS5Padding
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧