急急!异常java.security.InvalidKeyException:No installed provider supports this key

love_xiaobai 2011-05-30 09:33:45
我的jsp页面:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="org.des.dmw.*" %>
<%@ page import="org.des.factory.*" %>
<%@ page import="java.security.*" %>
<%@ page import="javax.crypto.*" %>
<html>
<head>
<title>Jiami.jsp</title>
</head>

<body>
<center>
<%
request.setCharacterEncoding("gb2312");
%>
<%
String text = request.getParameter("plaintext");
String key = request.getParameter("key");
DesFactory.getDesEncryptInstance().getKey(key);
String strEnc = DesFactory.getDesEncryptInstance().getEncString(text);
%>

<form action="Jiemi.jsp" method="post">
<table border="1">

<tr>
<td>输出密文:</td>
<td><%=strEnc%></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="解密">
<input type="reset" value="重置">
</td>
</tr>
</form>


</center>
</body>
</html>

DES算法程序:
package org.des.dmw;

import java.security.*;
import javax.crypto.*;

public class DesEncrypt
{
Key key ;
public void getKey(String strKey)
{
try
{
KeyGenerator generator = KeyGenerator.getInstance("DES") ;
generator.init(new SecureRandom(strKey.getBytes()));
this.key = generator.generateKey();
generator = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String getEncString(String strMing)
{
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try
{
return byte2hex(getEncCode(strMing.getBytes()));
}catch(Exception e)
{
e.printStackTrace();
}
finally{
byteMing = null;
byteMi = null;
}
return strMi;

}
public String getDesString(String strMi)
{
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try{
return new String(getDesCode(hex2byte(strMi.getBytes())));
}catch(Exception e)
{
e.printStackTrace();
}finally
{
byteMing = null;
byteMi = null;
}
return strMing;
}
private byte[] getEncCode(byte[] byteS)
{
byte[] byteFina = null;
Cipher cipher;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
cipher = null;
}
return byteFina;
}
private byte[] getDesCode(byte[] byteD)
{
Cipher cipher;
byte[] byteFina = null;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
cipher = null;
}
return byteFina;
}
public static String byte2hex(byte[] b)
{
//转成16进制字符串
String hs = "";
String stmp = "";
for(int n=0;n<b.length;n++)
{
//整数转成 十六进制表示
stmp = (java.lang.Integer.toHexString(b[n]&0XFF));
if(stmp.length()==1)
hs = hs + "0" +stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();//转成大写
}
public static byte[] hex2byte(byte[] b)
{
if((b.length%2)!=0)
throw new IllegalArgumentException("长度表示偶数");
byte[] b2 = new byte[b.length/2];
for(int n=0;n<b.length;n+=2)
{
String item = new String(b,n,2);
//两位一组,表示一个字节,把这样表示的十六进制字符串,还原成一个进制字节
b2[n/2] = (byte)Integer.parseInt(item,16);
}
return b2;
}

}

另外写了一个工厂类:
package org.des.factory;

import org.des.dmw.*;

public class DesFactory
{
public static DesEncrypt getDesEncryptInstance()
{
return new DesEncrypt();
}
}

但在jsp前台显示时总是出现java.security.InvalidKeyException: No installed provider supports this key: (null)
急急急!求高手解答 谢谢 我们的大作业过几天就要交了
...全文
2122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang12 2011-07-07
  • 打赏
  • 举报
回复
表示你的机器上没有DES加密算法的实现
coolfoxzlz 2011-07-07
  • 打赏
  • 举报
回复
关注一下。

81,094

社区成员

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

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