我用javax.crypto.CipherInputStream;CipherOutputStream

解决方案 »

  1.   

    你仔细看看,里面有你要的东西,再加上我正在弄的程序(还没有完成)。在读取解密内容时出现错误了。http://www.zdnet.com.cn/developer/code/story/0,2000081534,39038484,00.htmhttp://www.zdnet.com.cn/developer/code/story/0,2000081534,39038484-2,00.htmpackage temptest_project;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: shu</p>
     * @author [email protected]
     * @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 byte[] encode(byte[] info){
                  if(info==null)
                      return null;
                  try{
                        // 初始化加密器
                        this.m_pbeCipher.init(Cipher.ENCRYPT_MODE,this.m_pbeKey,this.m_pbeParamSpec);
                        // 明文
                       byte[] cleartext = info;
                        // 加密
                        byte[] ciphertext = this.m_pbeCipher.doFinal(cleartext);
                        System.out.println("*********************");
                        System.out.println("加密后");                   
                        for (int i = 0 ;i<ciphertext.length ;i++){
                              System.out.println(ciphertext[i]);
                        }
                        System.out.println("*********************");
                        //返回密文
                        return ciphertext;
                  }
                  catch (Exception e){
                        System.out.println(e);
                        return null;
                  }
            }
            public byte[] decode(byte[] info){
                  if(info==null)
                        return null;
                  try{
                        // 初始化加密器
                        this.m_pbeCipher.init(Cipher.DECRYPT_MODE,this.m_pbeKey, this.m_pbeParamSpec);        
                        // 密文
                        byte[] ciphertext = info;    
                        System.out.println("*********************");
                        System.out.println("解密前");
                        for (int i = 0 ;i<ciphertext.length ;i++){
                              System.out.println(ciphertext[i]);
                        }
                        System.out.println("*********************");    
                        // 解密
                       byte[] cleartext = this.m_pbeCipher.doFinal(ciphertext);
                    //返回明文               
                        return cleartext;
                  }
                  catch (Exception e){
                        System.out.println(e);
                        return null;
                  }
            }        
            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) {
                    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=256;
                        byte[] byteTom=new byte[nEnd];
                        int nCount=0;                    
                        InputStream is=this.getInputStream(inputStream); 
                        System.out.println("following is from is");                                       while((nCount=is.read(byteTom,0,nEnd))!=-1){
                              String s=new String (byteTom,0,nEnd);
                              System.out.println(s);                         
                        }
                        is.close ();
                  }
                  catch (Exception ex) {
                        System.err.println("ERROR: "+ex.toString());
                  }              
            }
            
            public static void main(String[] args) throws Exception{
                /*
                PBE PBE1 = new PBE("100");
                byte[] en_string,de_string;
                en_string = PBE1.encode("127.0.0.1".getBytes());
                de_string = PBE1.decode(en_string);
                System.out.println(new String (en_string));
                System.out.println(new String (de_string));
                */
                PBE PBE1 = new PBE("newman0708");
                String content="Description: author [email protected]";//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);
            }
      }
    /*
    *********************
    加密后
    27
    13
    -88
    -126
    98
    -16
    -15
    61
    64
    -58
    121
    17
    -82
    -87
    -96
    17
    *********************
    *********************
    解密前
    27
    13
    -88
    -126
    98
    -16
    -15
    61
    64
    -58
    121
    17
    -82
    -87
    -96
    17
    *********************
    [B@388993 ----------这是byte的地址
    [B@d04653
    */