數(shù)據(jù)結(jié)構(gòu):JAVA實(shí)現(xiàn)RSA算法

字號:

實(shí)現(xiàn)一對密鑰對整個項(xiàng)目所有加密解密文件都適用的方法,采用先生成一對密鑰.保存到xml文件中,以后獲得私匙和公鑰只需要從xml文件中取得就可以了.
    /**
    * 把成生的一對密鑰保存到RSAKey.xml文件中
    */
    public void saveRSAKey() {
    try {
    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());
    oos.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    注意:需要從http://www.bouncycastle.org下載bcprov-jdk14-137.jar包.
    獲取密鑰方法如下:
    /**
    * 獲得RSA加密的密鑰。
    * @return KeyPair返回對稱密鑰
    */
    public static KeyPair getKeyPair() {
    //產(chǎn)生新密鑰對
    KeyPair kp;
    try {
    String fileName = \"conf/RASKey.xml\";
    InputStream is = FileUtils.class.getClassLoader()
    .getResourceAsStream(fileName);
    ObjectInputStream oos = new ObjectInputStream(is);
    kp = (KeyPair) oos.readObject();
    oos.close();
    } catch (Exception e) {
    throw new EprasRuntimeException(\"讀取加密文件出錯.\", e);
    }
    return kp;
    }
    文件采用RSA算法加密文件
    /**
    * 文件file進(jìn)行加密并保存目標(biāo)文件destFile中
    * @param srcFileName
    * 要加密的文件 如c:/test/srcFile.txt
    * @param destFileName
    * 加密后存放的文件名 如c:/加密后文件.txt
    */
    public static void encryptFile(String srcFileName,
    String destFileName) throws Exception {
    OutputStream outputWriter = null;
    InputStream inputReader = null;
    try {
    Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
    new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] buf = new byte[100];
    int bufl;
    cipher.init(Cipher.ENCRYPT_MODE, getKeyPair().getPublic());
    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 = (byte) buf;
    }
    }
    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) {
    }
    }
    }
    文件采用RSA算法解密文件
    /**
    * 文件file進(jìn)行加密并保存目標(biāo)文件destFile中
    * @param srcFileName
    * 已加密的文件 如c:/加密后文件.txt
    * @param destFileName
    * 解密后存放的文件名 如c:/ test/解密后文件.txt
    */
    public static void decryptFile(String srcFileName,
    String destFileName) throws Exception {
    OutputStream outputWriter = null;
    InputStream inputReader = null;
    try {
    Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
    new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] buf = new byte[128];
    int bufl;
    cipher.init(Cipher.DECRYPT_MODE, getKeyPair().getPrivate());
    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 = (byte) buf;
    }
    }
    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) {
    }
    }
    }
    如果對于大文件加密采用RSA算法執(zhí)行速度要非常非常慢。