免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2493 | 回复: 4
打印 上一主题 下一主题

ArrayIndexOutOfBoundsException:1 请大家来看看 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-25 09:51 |只看该作者 |倒序浏览
是这样的:小弟最近在学java加密解密方面,小弟用jdk6.0编译、运行程序都没有问题,但用netbeans5.5编译没有问题,但运行它报ArrayIndexOutOfBoundsException:1错误,源代码如下:

import java.security.*;
import javax.crypto.*;
import java.io.*;
import javax.crypto.spec.*;
import java.util.*;
import sun.misc.*;


public class Main
{
private static int ITERATIONS = 1000;

private static void usage() throws Exception
{
System.err.println("Usage: Java PBE -e|-d password text");
System.exit(1);
}

public static void main(String[] args) throws Exception
{
if(args.length != 3) usage();

System.in.read();

//Convert password to a char array.
char[] password = args[1].toCharArray()     //运行程序就是这里报错。
String text = args[2];
String output = null;

if("-e".equals(args[0])) output = encrypt(password, text);
else if("-d".equals(args[0])) output = decrypt(password, text);
else usage();

System.out.println(output);
}

private static String encrypt(char[] password, String plaintext) throws Exception
{
byte[] salt = new byte[8];
Random random = new Random();
random.nextBytes(salt);

PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);

Cipher cipher = CIpher.getInstance("PBEWIthSHAAndTwofish-CBC");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

byte[] ciphertext = cipher.doFinal(plaintext.getBytes());

BASE64Encoder encoder =new BASE64Encoder();

String saltString = encoder.encode(salt);
String ciphertextString = encoder.encode(ciphertext);

return saltString+ciphertextString;
}

private static String decrypt(char[] password, String text) throws Exception
{
String salt = text.substring(0, 12);
String ciphertext = text.substring(12, text.length());

BASE64Decoder decoder = new BASE64Decoder();

byte[] saltArray = decoder.decodeBuffer(salt);
byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);

PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, ITERATIONS);

Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

byte[] plaintextArray = cipher.doFinal(ciphertextArray);

return new String(plaintextArray);
}
}

[ 本帖最后由 yhzy 于 2007-6-25 11:11 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-06-25 12:52 |只看该作者
你用netbeans运行的时候是怎么传递命令行参数的?

论坛徽章:
0
3 [报告]
发表于 2007-06-25 13:34 |只看该作者
System.in.read()传输参数的。

论坛徽章:
0
4 [报告]
发表于 2007-06-26 05:41 |只看该作者
set breakpoint在出错的那一行,就可以知道原因。
我觉得是args somehow被重置了。在出错的那一行是空的。所以就抛出异常。

论坛徽章:
0
5 [报告]
发表于 2007-06-28 10:56 |只看该作者
是小弟自已对命令行输入参数有误,现在已解决问题。多谢大家!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP