Java安全(JCA/JSSE):对称加密

得到一个类的实例,一般有这些方法:

New

本类. getInstance()   Cipher. getInstance(“AES”)

其他类的某个方法产生    Connectionct=DriverManager.getConnection()

工厂类的产生方法

ServerSocket ss= ServerSocketFactory.getDefault().createServerSocket()

 

 

关于密钥加密加密和流的一些小算法

//key写入文件给别//

package com.xiongshiyan.security;

 

import java.io.ByteArrayOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStream;

import java.security.Key;

import java.security.NoSuchAlgorithmException;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKeyFactory;

 

publicclass SecretKeyTest {

 

    /**

     * @param args

     * @throwsNoSuchPaddingException

     * @throwsNoSuchAlgorithmException

     */

    publicstaticvoid main(String[] args) throws Exception {

       // TODO Auto-generatedmethod stub

       SecretKeyTest.secretAES();

       //SecretKeyTest.deSecretAES();

    }

   

    privatestaticvoid secretAES() throws Exception{

       //创建加密对象

       Cipher cipher = Cipher.getInstance("AES");

       //Keykey=SecretKeyFactory.getInstance("AES").generateSecret(null);

       Key key=KeyGenerator.getInstance("AES").generateKey();

       cipher.init(Cipher.ENCRYPT_MODE, key);

       byte[] secResult=cipher.doFinal("abcdefg数据".getBytes());

       System.out.println(new String(secResult));

      

       //把加密后的数据和key一起给别人  别人拿到了之后进行恢复

       ObjectOutputStream oosKey=new ObjectOutputStream(new FileOutputStream("key.key"));

       oosKey.writeObject(key);//写入加密key

       oosKey.close();

      

       FileOutputStream oosDat=new FileOutputStream("dat.dat");

       oosDat.write(secResult);//写入加密dat

       oosDat.close();

    }

   

    privatestaticvoid deSecretAES() throws Exception{

       //创建加密对象

       Cipher cipher = Cipher.getInstance("AES");

/**

        * 实现Serializable接口,保存数据的时候你就能按照sun定义的标准格式进行,ObjectInputStreamObjectOutputStream提供的readObjectwriteObject进行读写,你也可以自己扩展格式,实现Externalizable接口

        */

       ObjectInputStream oisKey=new ObjectInputStream(new FileInputStream("key.key"));

        Keykey=(Key)oisKey.readObject();

       oisKey.close();

       cipher.init(Cipher.DECRYPT_MODE, key);

      

       FileInputStream fisDat=new FileInputStream("dat.dat");

    /*  ByteArrayOutputStreambaos=new ByteArrayOutputStream();

       SecretKeyTest.copyStream(fisDat, baos);

       byte[] deSecResult=cipher.doFinal(baos.toByteArray());*/

      

       byte[] src=newbyte[fisDat.available()];//对于文件流来说就是其长度,而对于像网络流就不是了

       int len=fisDat.read(src);

       int total=0;

       while(total<src.length){

           total+=len;

           len=fisDat.read(src, total, src.length-total);

       }

       byte[] deSecResult=cipher.doFinal(src);

      

       fisDat.close();

       System.out.println(new String(deSecResult));

    }

   

    privatestaticvoid copyStream(InputStream is,OutputStream os) throws Exception{

       byte[] buff=newbyte[1024];

       int total=0;

       int len=is.read(buff);

       while(len!=-1){

           os.write(buff, 0, len);

           len=is.read(buff);

       }

    }

 

}

 

 

 

//脑袋里面记住一个密码就好了

privatestaticvoid secretPBE() throws Exception{//脑袋里面记住你的密码

       //创建加密对象

       Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");

       Key key=SecretKeyFactory.getInstance("PBEWithMD5AndDES")

           .generateSecret(new PBEKeySpec("xiongshiyan".toCharArray()));//xiongshiyan就是你的密码

       //Keykey=KeyGenerator.getInstance("AES").generateKey();

      

       PBEParameterSpec pbeParameterSpec=new PBEParameterSpec(newbyte[]{1,2,3,4,5,6,7,8},1000);

       cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec);

      

       byte[] secResult=cipher.doFinal("数据abcdefg".getBytes());

       System.out.println(new String(secResult));

      

       FileOutputStream oosDat=new FileOutputStream("dat.dat");

       oosDat.write(secResult);//写入加密dat

       oosDat.close();

    }

    privatestaticvoid deSecretPBE() throws Exception{

       Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");

       Key key=SecretKeyFactory.getInstance("PBEWithMD5AndDES")

           .generateSecret(new PBEKeySpec("xiongshiyan".toCharArray()));

       //Keykey=KeyGenerator.getInstance("AES").generateKey();

      

       PBEParameterSpec pbeParameterSpec=new PBEParameterSpec(newbyte[]{1,2,3,4,5,6,7,8},1000);

       cipher.init(Cipher.DECRYPT_MODE,key,pbeParameterSpec);

      

       FileInputStream fisDat=new FileInputStream("dat.dat");

    /*  ByteArrayOutputStreambaos=new ByteArrayOutputStream();

       SecretKeyTest.copyStream(fisDat, baos);

       byte[] deSecResult=cipher.doFinal(baos.toByteArray());*/

      

       byte[] src=newbyte[fisDat.available()];//对于文件流来说就是其长度,而对于像网络流就不是了

       int len=fisDat.read(src);

       int total=0;

       while(total<src.length){

           total+=len;

           len=fisDat.read(src, total, src.length-total);

       }

       byte[] deSecResult=cipher.doFinal(src);

      

       fisDat.close();

       System.out.println(new String(deSecResult));

    }


你可能感兴趣的:(Java安全(JCA/JSSE):对称加密)