资源简介

BouncyCastleProvider jar包,如果有引入该类包报错的,可以下载,亲测可用,测试代码附上,有问题请reply to me

资源截图

代码片段和文件信息

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.objectInputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;

import javax.crypto.Cipher;


public class Test {
public static void main(String[] args) throws Exception {
 /* 1
 SecureRandom sr = new SecureRandom();

     KeyPairGenerator kg = KeyPairGenerator.getInstance(“RSA“new org.bouncycastle.jce.provider.BouncyCastleProvider());

     //注意密钥大小最好为1024否则解密会有乱码情况.

     kg.initialize(1024 sr);

     FileOutputStream fos = new FileOutputStream(“C:/RSAKey.xml“);

     objectOutputStream oos = new objectOutputStream(fos);

     //生成密钥

     oos.writeobject(kg.generateKeyPair().getPrivate());

     oos.close();
     */

 /* 2
     Key kp;

     String fileName = “RSAKey.xml“;

     InputStream is = Test.class.getClassLoader().getResourceAsStream(fileName);

     objectInputStream oos = new objectInputStream(is);

     kp = (Key) oos.readobject();

     oos.close();

     encryptFile(“C:/bsmain_runtime.log““C:/encry.log“kp);
      
      */
    }

public static void encryptFile(String srcFileName String destFileNameKey publicKkey) throws Exception {

      OutputStream outputWriter = null;

      InputStream inputReader = null;

      try {

            Cipher cipher = Cipher.getInstance(“RSA“ new org.bouncycastle.jce.provider.BouncyCastleProvider());


            
            byte[] buf = new byte[100];

            int bufl;

            cipher.init(Cipher.ENCRYPT_MODE publicKkey);
            
            System.out.println(cipher.getBlockSize()+“================“);

            outputWriter = new FileOutputStream(destFileName);

            inputReader = new FileInputStream(srcFileName);

            while ((bufl = inputReader.read(buf)) != -1) {

                byte[] encText = null;

                byte[] newArr = null;

                if (buf.length == bufl) {

                    newArr = buf;

                } else {

                    newArr = new byte[bufl];

                    for (int i = 0; i < bufl; i++) {

                          newArr[i] = (byte) buf[i];

                    }

                }

                encText = cipher.doFinal(newArr);

                outputWriter.write(encText);

            }

            outputWriter.flush();

      } catch (Exception e) {

            throw e;

      } finally {

            try {

                if (outputWriter != null) {

                    outputWriter.close();

                }

                if (inputReader != null) {

                    inputReader.close();

                }

            } catch (Exception e) {

            }

      }

  }


}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3011  2009-06-29 22:13  Test.java

     文件    1632822  2009-06-29 21:50  bcprov-jdk15-143.jar

----------- ---------  ---------- -----  ----

              1635833                    2


评论

共有 条评论