程序在myEclipse中可以运行 在命令行里运行出错

gq2010 gq2010 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package com.hnzskj.encrypt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;

public class Encrypt {

/**
* 对称加密-产生密钥
*/
public static void generatorKey() {
SecretKey key = null;
try {
// 指定算法,这里为DES;如果想用Blowfish算法,则用getInstance("Blowfish")
// BouncyCastle基本上支持所有通用标准算法
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 指定密钥长度,长度越高,加密强度越大
keygen.init(56);
// 产生密钥
key = keygen.generateKey();
// 构造输出文件,这里的目录是动态的,根据用户名称来构造目录
ObjectOutputStream keyFile = new ObjectOutputStream(
new FileOutputStream("c:\\encrypt\\yhb.des"));
keyFile.writeObject(key);
keyFile.close();
} catch (NoSuchAlgorithmException e5) {
// generateKey()抛出的异常
e5.printStackTrace();
System.out.print("no such algorithm");
System.exit(0);
} catch (IOException e4) {
e4.printStackTrace();
System.out.print("error when generate the deskey");
System.exit(0);
}
}

/**
* 方法描述:对称加密-读取密钥
*/
private static SecretKey getSecretKey() {
// 从密钥文件中读密钥
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
new FileInputStream("c:\\encrypt\\yhb.des"));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
ey1.printStackTrace();
System.out.println("Error when read keyFile");
System.exit(0);
} catch (Exception ey2) {
System.out.println("error when read the keyFile");
ey2.printStackTrace();
}
return key;
}

/**
* 方法描述:加密文本信息
*/
public static String encrypt(String encryptStr) {
SecretKey key = getSecretKey();
Cipher cipher = null;
try {
// 设置算法,应该与加密时的设置一样
cipher = Cipher.getInstance("DES");
// 设置解密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (Exception ey3) {
ey3.printStackTrace();
System.out.println("Error when create the cipher");
}
byte[] data = null;
try {
data = cipher.doFinal(encryptStr.getBytes());
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
encryptStr = Base64.encodeBase64String(data);
return encryptStr;
}

public static void main(String[] args) {
System.out.println(encrypt("加密字符串"));
}
}

这个类在myeclipse中可以正常运行,但是命令行里执行时就出现以下问题:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES
at javax.crypto.Cipher.getInstance(DashoA13*..)
at com.hnzskj.encrypt.Encrypt.encrypt(Encrypt.java:90)
at com.hnzskj.encrypt.Encrypt.main(Encrypt.java:110)
Error when create the cipher
Exception in thread "main" java.lang.NullPointerException
at com.hnzskj.encrypt.Encrypt.encrypt(Encrypt.java:99)
at com.hnzskj.encrypt.Encrypt.main(Encrypt.java:110)


...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
gq2010
Java SE 62,620 成员 307,251 内容