代码已改好,但是具体原因不详,求教
加密:
public class SEnc {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
String s="you are jokking baby";
FileInputStream f= new FileInputStream("Key1.dat");
ObjectInputStream b=new ObjectInputStream(f);
Key k=(Key)b.readObject();
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, k);
byte ptext[]=s.getBytes("UTF8");//字符串转数组
for(int i=0;i<ptext.length;i++)
{
System.out.print(ptext[i]+",");
}
System.out.print("");
byte ctext[]=cp.doFinal(ptext);
for(int i=0;i<ctext.length;i++)
{
System.out.print(ctext[i]+",");
}
FileOutputStream f2=new FileOutputStream("SEnc.dat");
f2.write(ctext);
f.close();
}
}
解密
public class SDec {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FileInputStream f= new FileInputStream("SEnc.dat");//miwen
int num =f.available();
byte[]ctext =new byte[num];
f.read(ctext);
FileInputStream f2=new FileInputStream("Key1.dat");//miyao
ObjectInputStream b=new ObjectInputStream(f2);
Key k=(Key)b.readObject();
/*int num2=f2.available();
byte[] KeyKb=new byte[num2];
f2.read(KeyKb);
SecretKeySpec w=new SecretKeySpec(KeyKb,"DESede");*/
Cipher cp=Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE,k);
byte []ptext=cp.doFinal(ctext);
String p=new String(ptext,"UTF8");
System.out.println(p);
f.close();
}
}
原来的为红色部分,但是会抛出
Exception in thread "main" java.security.InvalidKeyException: Invalid key length: 282 bytes
后来改为蓝色部分,,,dofinal()异常,最后加了个文件流关闭就对了
想问一下:对于获取密钥方式不同到底有啥影响,我觉得原来的获取密钥方式也对呀????
?????????????????????????????????????????、、、