对流进行加密!

newman0708 2003-04-22 10:49:43
我在读取解密内容时出现错误了,

应该读出为:
byte(41): Description: author nch@peoplemail.com.cn
但是实际读出为:
byte(32):Description: author nch@peoplema

请高手帮忙,知道我这是为什么!
谢谢!!!!!

import java.security.*;
import java.security.cert.X509Certificate;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.lang.*;
import com.newman.io.FileReadWrite;
/**
* PBE算法
*
* <p>Title: newman的类库</p>
* <p>Description: 没有最好只有更好</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: newman0708@eastday.com</p>
* @author newman0708
* @version 1.0
*/
public class PBE{
private String m_Password;
private final String SECRETKEYFACTORY= "PBEWithMD5AndDES";
private byte[] m_Salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

private PBEKeySpec m_pbeKeySpec=null;
private SecretKeyFactory m_keyFac=null;
private SecretKey m_pbeKey=null;
private PBEParameterSpec m_pbeParamSpec=null;
private Cipher m_pbeCipher=null;

private int m_Count = 2;//???

public PBE(String password){
this.setPassword(password);
init();
}

public void setPassword(String password){
this.m_Password =password;
}

private void init(){
try {
this.m_pbeKeySpec = new PBEKeySpec(this.m_Password.toCharArray());
this.m_keyFac = SecretKeyFactory.getInstance(this.SECRETKEYFACTORY);
this.m_pbeKey = this.m_keyFac.generateSecret(this.m_pbeKeySpec);
// 生成pbe算法所需的参数对象,两个参数详见 RSA的 PKCS #5 标准
this.m_pbeParamSpec = new PBEParameterSpec(this.m_Salt, this.m_Count);
// 生成一个加密器
this.m_pbeCipher= Cipher.getInstance(this.SECRETKEYFACTORY);
}
catch (Exception ex) {
}
}

public InputStream getInputStream(InputStream is) throws IOException {
try {
this.m_pbeCipher.init(Cipher.DECRYPT_MODE,this.m_pbeKey, this.m_pbeParamSpec);
CipherInputStream cis = new CipherInputStream(is, this.m_pbeCipher);
return cis;
}
catch (Exception ex) {
return null;
}
}

public OutputStream getOutputStream(OutputStream os) throws IOException {
try {
this.m_pbeCipher.init(Cipher.ENCRYPT_MODE,this.m_pbeKey,this.m_pbeParamSpec);
CipherOutputStream cos = new CipherOutputStream(os, this.m_pbeCipher);
return cos;
}
catch (Exception ex) {
System.err.println("error: getOutputStream");
return null;
}
}

public void writeFile(OutputStream os,String content){
try {
OutputStream os2=this.getOutputStream(os);
byte[] tom=content.getBytes();
System.out.println("byte("+tom.length +"): "+new String(tom));
os2.write(content.getBytes());
}
catch (Exception ex) {
}
}

public void PRINT(InputStream inputStream){
try {
int nEnd=128;
byte[] byteTom=new byte[nEnd];
int nCount=0;
//StringBuffer bufftext=new StringBuffer();
/*
while((nCount=inputStream.read(byteTom,0,nEnd))!=-1){
//String s=new String (byteTom,0,nEnd);
int i=0;
while(i<byteTom.length){
System.out.println(byteTom[i]);
i++;
}
//bufftext.append(s+String.valueOf(10));
}
*/
InputStream is=this.getInputStream(inputStream);
System.out.println("following is from is");

while((nCount=is.read(byteTom))!=-1){
// String s=new String (byteTom,0,nEnd);
String s=new String (byteTom);
System.out.println(s);
System.out.println("byte("+nCount+")");
//bufftext.append(s+String.valueOf(10));
}
//is.close ();
}
catch (Exception ex) {
System.err.println("ERROR: "+ex.toString());
}
}

public static void main(String[] args) throws Exception{
PBE PBE1 = new PBE("newman0708");
String content="Description: author nch@peoplemail.com.cn";//41
String content2="Description: 没有最好只有更好";//29
FileOutputStream bw =new FileOutputStream("f://foo.txt");
PBE1.writeFile(bw,content);
BufferedInputStream br =new BufferedInputStream(new FileInputStream("f://foo.txt"));
PBE1.PRINT(br);
}
}

/*
*********************
加密后
113
-89
-55
-60
-122
-94
76
-122
36
114
-121
3
6
38
-22
-61
10
-110
23
115
-45
-81
-62
-75
111
-73
93
60
-23
80
100
-128
*********************
*********************
解密前
113
-89
-55
-60
-122
-94
76
-122
36
114
-121
3
6
38
-22
-61
10
-110
23
115
-45
-81
-62
-75
111
-73
93
60
-23
80
100
-128
*********************
en_string:
de_string: Description: 没有最好只有更好
*/
...全文
84 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
eyeieye 2003-06-04
  • 打赏
  • 举报
回复
pbe没有空字符填充的吗?问一下
treeClimber 2003-06-04
  • 打赏
  • 举报
回复
你可以不用它的IO流嘛!
就用标准流,read原文后调用encode写入,要解密先read密文在decode,OK,我就是这样的。
xiaonan0120 2003-06-04
  • 打赏
  • 举报
回复
我以我血荐轩辕的方法可取!我们以前做加解密的时候都要进行编解码的!在加密之前先对其进行编码,然后解密的时候进行解码!要不然好像数据会丢失
newman0708 2003-06-04
  • 打赏
  • 举报
回复
先谢谢各位的回答!

我现在也是用与
treeClimber(我以我血荐轩辕) 一样的方法。

但是我更想知道用流加密是怎么实现的。

treeClimber 2003-06-03
  • 打赏
  • 举报
回复
是的,发现字符串丢失了。
nick19800707 2003-05-30
  • 打赏
  • 举报
回复
up!
zazzle2003 2003-05-30
  • 打赏
  • 举报
回复
帮不了,哎
WilliamXiaoLiang 2003-05-25
  • 打赏
  • 举报
回复
up!
newman0708 2003-05-20
  • 打赏
  • 举报
回复
脱离jbuilder后,会出现不能运行的错误已经解决。

现在的问题:

我在读取解密内容时出现错误了,

应该读出为:
byte(41): Description: author nch@peoplemail.com.cn
但是实际读出为:
byte(32):Description: author nch@peoplema

代码如问题(上面)。
请高手帮忙!

谢谢!!!!!


newman0708 2003-05-20
  • 打赏
  • 举报
回复
我的版本是j2dk1.4.0

难道是版本的问题吗?
proteinboy 2003-05-19
  • 打赏
  • 举报
回复
是不是JDK的版本不一样?顶
newman0708 2003-04-30
  • 打赏
  • 举报
回复
那我应该在系统变量中还要加入些什么包呢?
bdsc 2003-04-30
  • 打赏
  • 举报
回复
流是无头无尾的,你不能保证确切的知道一次到底读写了多少内容,需要检查,也许要读写多次

jbuilder和你系统的java的运行环境不一样(如:classpath)
newman0708 2003-04-29
  • 打赏
  • 举报
回复
请高手帮忙啊,

还有一个问题,就是:脱离jbuilder后,会出现不能动错误。
Exception in thread "main" java.lang.NoSuchMethodError
at javax.crypto.SunJCE_d.a(DashoA6275)
at javax.crypto.SunJCE_d.a(DashoA6275)
at javax.crypto.SunJCE_d.verify(DashoA6275)
at javax.crypto.SunJCE_b.f(DashoA6275)
at javax.crypto.SunJCE_b.<clinit>(DashoA6275)
at javax.crypto.Cipher.getInstance(DashoA6275)
at encode_decode_study.DES.<init>(encode_decode_test.java:91)
at encode_decode_study.crypt_test.main(crypt_test.java:20)

这怎么解决啊!
JavaBoyCaoJi 2003-04-25
  • 打赏
  • 举报
回复
up
newman0708 2003-04-24
  • 打赏
  • 举报
回复
我解决不了,请高手帮忙!
tonight77 2003-04-22
  • 打赏
  • 举报
回复
建议跟踪调试一下

62,614

社区成员

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

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