关于RSA的解密操作。急都急死了真是有心无力啊。

daocha 2011-04-09 01:54:39
我参照这个帖子
http://sunxboy.iteye.com/blog/209156
做了个RSA的加密解密,
流程是这样的
浏览器通过服务端提供的公钥进行加密,然后发到服务器上,通过私钥解密,
但是使用这个解密方法的时候有时可以解密成功,但有时却在解密的时候报 Bad arguments异常,我看见那个帖子的回复说问题出在这句话上
byte[] en_result = new BigInteger(result, 16).toByteArray();
我查了查,还真是, 秘文传过来一共有256个字符,通过转成字节数组,同样的明文,有时传过来转成的字节数组长度为128,有的时候却又是129,
说是这个转字节数组的方式问题嘛,我就换了一个方式

public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}

这下不报Bad arguments异常了,改报 input too large for RSA cipher. 异常了,
用 hexStringToByte 这个方法转字节数组这下数组的长度很稳定都是128了 不会出现129的情况,可是依然解密不成功,
请教下又没人懂RSA的?

...全文
1867 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
daocha 2011-04-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 rxdw8121 的回复:]

以下给出一个加密解密方法供参考,测试通过的
/**
* 公钥加密
*/
public byte[] PublicKeyEncrypt(byte[] indata,int indatalen)
throws DataLengthException
{
int k=getInputBlockSize(tr……
[/Quote]

但是你的byte[] 是如何转换的呢 还有 datalen是什么长度
daocha 2011-04-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baobao04551 的回复:]

你的hexStringToByte 方法和 BigInteger的toByteArray方法返回的字节数组是不一样的。后者还有符号位的处理。而你的方法则没有。
[/Quote]
那你知道解决方法吗
baobao04551 2011-04-09
  • 打赏
  • 举报
回复
你的hexStringToByte 方法和 BigInteger的toByteArray方法返回的字节数组是不一样的。后者还有符号位的处理。而你的方法则没有。
  • 打赏
  • 举报
回复
http://wlh.iteye.com/blog/134796
rxdw8121 2011-04-09
  • 打赏
  • 举报
回复
以下给出一个加密解密方法供参考,测试通过的
/**
* 公钥加密
*/
public byte[] PublicKeyEncrypt(byte[] indata,int indatalen)
throws DataLengthException
{
int k=getInputBlockSize(true);
byte EncryptBlock[]=null;
// PS依据输入数据的长度>8
if (indatalen > getInputBlockSize(true)-10)
{
throw new DataLengthException("输入RSA加密的数据太长.\n");
}
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] PS=new byte[(getOutputBlockSize(true)-indatalen-2)];
for(int i=0;i<PS.length;i++)
{
PS[i]=(byte)(secureRd.nextInt(255)+1);
}
try
{
baos.write((byte)0x02);
baos.write(PS);
baos.write((byte)0x00);
baos.write(indata);
}catch(IOException ioe){
ioe.printStackTrace();
}
EncryptBlock=baos.toByteArray();
BigInteger input = new BigInteger(1, EncryptBlock);
byte[] output=null;
output = input.modPow(pukParam.getExponent(), pukParam.getModulus()).toByteArray();
if (output[0] == 0 && output.length > getOutputBlockSize(true))//比输出长度长,截断
{
byte[] tmp = new byte[output.length - 1];
System.arraycopy(output, 1, tmp, 0, tmp.length);
return tmp;
}
if (output.length < getOutputBlockSize(true)) // 比输出长度短,增长
{
byte[] tmp = new byte[getOutputBlockSize(true)];
System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length);
return tmp;
}
return output;
}
/**
* 私钥解密
*/
public byte[] PrivateKeyDecrypt(byte[] indata,int indatalen)
throws DataLengthException
{
int padLength=0;
if (indatalen > getInputBlockSize(false))
{
throw new DataLengthException("输入RSA解密的数据太长.");
}
byte[] output=null;
BigInteger input=new BigInteger(1,indata);
output = input.modPow(pvkParam.getPrivateExponent(), pukParam.getModulus()).toByteArray();
if(output.length<10)
throw new DataLengthException("消息格式不对.");
if(output[0]!=(byte)0x02)
{
throw new DataLengthException("不是公钥操作的结果");
}
int start=0;
int outLen=output.length;
while(output[start]!=(byte)0x00)
{
start++;
if(start>=outLen)
throw new DataLengthException("无效的填充数据!");
}
start++; // 跳过0
byte tm[]=new byte[outLen-start];
try {
System.arraycopy(output,start,tm,0,tm.length);
}
catch (Exception ex) {
return null;
}
return tm;
}

81,092

社区成员

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

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