⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsadecryption.java

📁 对称和非对称加密
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        //
        if (plainText[0] != 0x00 || plainText[1] != 0x02) {
          throw new InvalidCryptographException("Invalid Cryptograph Exception");
        }
        int start = 0;
        for (int ij = 2; ij < plainText.length; ij++) {
          if (plainText[ij] == 0x00) {
            start = ij + 1;
            break;
          }
        }
        length = 128 - start;
        byte[] plainNew = new byte[length];
        System.arraycopy(plainText, start, plainNew, 0, length);
        //
        fOutput.write(plainNew);
        //

      } // for
      //
    } // while

    fInput.close();
    fOutput.close();
    //
    ret = true;
    //
    return ret;

  }

  /**
   * 密钥为二进制文件的解密
   * @param nfilename RSA算法大整数模的文件
   * @param dfilename RSA算法解密指数文件
   * @param inFile   RSA输入文件
   * @param outFile    RSA输出文件
   * @return  如果成功,返回true,否则 false
   * @throws InvalidCryptographException 如果密文文件格式错误,抛异常
   * @throws FileNotFoundException 文件没有找到
   * @throws IOException  IO 异常
   */
  public boolean FileDecryptionF(String nfilename, String dfilename,
                                 String inFile,
                                 String outFile) throws
      InvalidCryptographException,
      FileNotFoundException, IOException {
    boolean ret = false;
    //

    // 解密指数  模
    DmgcMpInteger dd = null;
    DmgcMpInteger nn = null;
    FileInputStream fInputN = null;
    FileInputStream fInputD = null;

    // 密钥文件

    try {
      fInputD = new FileInputStream(dfilename);
      fInputN = new FileInputStream(nfilename);

      int lengthN = fInputN.available();
      int lengthD = fInputD.available();
      byte[] BufferN = new byte[lengthN];
      byte[] BufferD = new byte[lengthD];
      lengthN = fInputN.read(BufferN);
      lengthD = fInputD.read(BufferD);
      byte[] N = new byte[lengthN];
      System.arraycopy(BufferN, 0, N, 0, lengthN);
      byte[] D = new byte[lengthD];
      System.arraycopy(BufferD, 0, D, 0, lengthD);
      dd = new DmgcMpInteger(D);
      nn = new DmgcMpInteger(N);
      fInputN.close();
      fInputD.close();
    }
    catch (FileNotFoundException ex) {
      throw new FileNotFoundException("file not found");
    }
    catch (IOException ex) {
      throw new IOException("IO  Exception");
    } //
    //
    //
    FileInputStream fInput = null;
    FileOutputStream fOutput = null;
    //

    try {
      //  输入文件
      fInput = new FileInputStream(inFile);
      //  输出文件
      fOutput = new FileOutputStream(outFile);
    }
    catch (FileNotFoundException ex) {
      throw new FileNotFoundException("file not found");
    }
    //
    int inLength = fInput.available();
    if (inLength % 128 != 0) {
      fInput.close();
      fOutput.close();
      throw new InvalidCryptographException("Invalid cryptograph exception");
    }
    //
    byte[] buffer = new byte[8192];
    int bufferLength = 0;
    //
    while ( (bufferLength = fInput.read(buffer)) != -1) {
      //
      for (int i = 0; i < bufferLength; i = i + 128) {
        byte[] temp = new byte[128];
        //
        System.arraycopy(buffer, i, temp, 0, 128);
        //
        DmgcMpInteger ccc = new DmgcMpInteger(temp);
        DmgcMpInteger pp = ccc.modPow(dd, nn);
        //
        //
        byte[] plainTemp = pp.toByteArray();
        byte[] plainText = new byte[128];
        //
        int length = plainTemp.length;
        int padCount = 128 - length;
        if (padCount > 0) {
          byte[] paddZero = new byte[padCount];
          System.arraycopy(paddZero, 0, plainText, 0, padCount);
          System.arraycopy(plainTemp, 0, plainText, padCount, length);
        }
        else {
          System.arraycopy(plainTemp, 0, plainText, padCount, length);
        }
        //
        if (plainText[0] != 0x00 || plainText[1] != 0x02) {
          throw new InvalidCryptographException("Invalid Cryptograph Exception");
        }
        int start = 0;
        for (int ij = 2; ij < plainText.length; ij++) {
          if (plainText[ij] == 0x00) {
            start = ij + 1;
            break;
          }
        }
        length = 128 - start;
        byte[] plainNew = new byte[length];
        System.arraycopy(plainText, start, plainNew, 0, length);
        //
        fOutput.write(plainNew);
        //

      } // for
      //
    } // while

    fInput.close();
    fOutput.close();
    //
    ret = true;
    //

    //
    return ret;
  }

  //---------------------------------------------------------------------------
  /**
   * 解密文件
   * @param keyfilename   n和 d
   * @param cipherText 密文
   * @return  明文字节,小于等于117位
   * @throws InvalidCryptographException 无效密文
   * @throws FileNotFoundException 文件没有找到
   * @throws InvalidKeyFileException  无效密钥文件
   * @throws IOException IO 异常
   */
  public byte[] NewMessageDecryptionF(String keyfilename,
                                      byte[] cipherText) throws
      InvalidCryptographException, FileNotFoundException,
      InvalidKeyFileException, IOException {
    //--------------------------------------------------------------------
    //    byte[] ret = new byte[128];
    //
    FileInputStream fInputKey = new FileInputStream(keyfilename);
    int length = fInputKey.available();
    if (length <= 128) {
      throw new InvalidKeyFileException("Invalid Key file Exception");
    }
    //
    byte[] Buffer = new byte[128];
    //
    int lengthN = fInputKey.read(Buffer);

    byte[] N = new byte[lengthN];
    System.arraycopy(Buffer, 0, N, 0, lengthN);

    lengthN = fInputKey.read(Buffer);
    byte[] E = new byte[lengthN];
    System.arraycopy(Buffer, 0, E, 0, lengthN);

    fInputKey.close();

    DmgcMpInteger NN = new DmgcMpInteger(N);
    DmgcMpInteger EE = new DmgcMpInteger(E);
//
//    System.out.println(NN.toString());
//    System.out.println(EE.toString());
    // 以上为读取私钥
    // ------------------------------------------------------------------------
    byte[] ret = MessageDecryption(NN.toString(), EE.toString(), cipherText);

    return ret;

  }

  /**
   * 解密文件
   * @param keyfilename 密钥文件 n d
   * @param inFile 密文文件
   * @param outFile 明文文件
   * @return 真,操作成功,假,操作失败
   * @throws InvalidCryptographException  无效密文异常
   * @throws InvalidKeyFileException 无效密钥异常
   * @throws FileNotFoundException 文件异常
   * @throws IOException IO 异常
   */
  public boolean NewFileDecryptionF(String keyfilename,
                                 String inFile,
                                 String outFile) throws
      InvalidCryptographException, InvalidKeyFileException,
      FileNotFoundException, IOException {
    //--------------------------------------------------------------------
    //    byte[] ret = new byte[128];
    //
    FileInputStream fInputKey = new FileInputStream(keyfilename);
    int length = fInputKey.available();
    if (length <= 128) {
      throw new InvalidKeyFileException("Invalid Key file Exception");
    }
    //
    byte[] Buffer = new byte[128];
    //
    int lengthN = fInputKey.read(Buffer);

    byte[] N = new byte[lengthN];
    System.arraycopy(Buffer, 0, N, 0, lengthN);

    lengthN = fInputKey.read(Buffer);
    byte[] E = new byte[lengthN];
    System.arraycopy(Buffer, 0, E, 0, lengthN);

    fInputKey.close();

    DmgcMpInteger NN = new DmgcMpInteger(N);
    DmgcMpInteger EE = new DmgcMpInteger(E);
    // 以上为读取私钥
    // ------------------------------------------------------------------------
    boolean ret = FileDecryption(NN.toString(),EE.toString(),inFile,outFile);

    return ret;

  }

  //---------------------------------------------------------------------------

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -