Exemple #1
0
        /**
         * 加密文件
         *
         * @param key
         *          SecretKey
         * @param src
         *          原文件
         * @param dest
         *          加密文件
         *
         * @return
         *              加密是否成功
         *
         */
        #region nouse

        /*
         * public boolean encryptFile(SecretKey key, File src, File dest)
         * {
         *
         * if (src == null || !src.exists() || src.isDirectory() || !src.canRead() || dest == null)
         * {
         *
         *  return false;
         *
         * }
         *
         * long len = src.length(); // 文件大小
         *
         * if (len == 0L)
         * {
         *
         *  return false;
         *
         * }
         *
         * byte[] block_bytes = new byte[BLOCK_MAX_FILE];
         *
         * byte[] read_bytes = null;
         *
         * InputStream bis = null;
         *
         * OutputStream bos = null;
         *
         * boolean close = true;
         *
         * try
         * {
         *
         *  bis = new BufferedInputStream(new FileInputStream(src));
         *
         *  bos = new BufferedOutputStream(new FileOutputStream(dest));
         *
         *  for (;;)
         *  {
         *
         *      int read_size = bis.read(block_bytes); // 读数据
         *
         *      if (read_size == -1)
         *      {
         *
         *          break;
         *
         *      }
         *
         *      read_bytes = new byte[read_size];
         *
         *      System.arraycopy(block_bytes, 0, read_bytes, 0, read_size); // 复制实际读到的数据
         *
         *      byte[] cipher_bytes = encrypt(key, read_bytes); // 加密数据
         *
         *      if (cipher_bytes == null)
         *      {
         *
         *          return false;
         *
         *      }
         *
         *      byte[] size_cipher = encrypt(key, getBlockSizeBytes(cipher_bytes.length)); // 加密数据长度加密,24个字节
         *
         *      if (size_cipher == null)
         *      {
         *
         *          return false;
         *
         *      }
         *
         *      bos.write(size_cipher);
         *
         *      bos.write(cipher_bytes);
         *
         *  }
         *
         * }
         * catch (IOException ex)
         * {
         *
         *  log("加密文件出错:");
         *
         *  ex.printStackTrace();
         *
         *  return false;
         *
         * }
         * finally
         * {
         *
         *  if (bos != null)
         *  { // 关闭输出流
         *
         *      try
         *      {
         *
         *          bos.close();
         *
         *      }
         *      catch (IOException ex)
         *      {
         *
         *          log("关闭输出流出错:");
         *
         *          ex.printStackTrace();
         *
         *          close = false;
         *
         *      }
         *
         *  }
         *
         *  if (bis != null)
         *  { // 关闭输入流
         *
         *      try
         *      {
         *
         *          bis.close();
         *
         *      }
         *      catch (IOException ex)
         *      {
         *
         *          log("关闭输入流出错:");
         *
         *          ex.printStackTrace();
         *
         *          close = false;
         *
         *      }
         *
         *  }
         *
         *  block_bytes = null;
         *
         *  read_bytes = null;
         *
         * }
         *
         * if (!close || !dest.exists() || dest.length() < len)
         * {
         *
         *  return false;
         *
         * }
         *
         * return true;
         *
         * }
         */
        #endregion

        /**
         * 加密网络数据
         *
         * @param key
         *          SecretKey
         *
         * @param bytes
         *          原始数据
         *
         * @return
         *              [加密数据+噪音数据]长度值的加密数据 + [加密数据 + 噪音数据]
         *
         */
        public byte[] encryptNet(SecretKey key, byte[] bytes)
        {
            if (key == null || bytes == null || bytes.Length == 0)
            {
                return(null);
            }

            byte[] IV = getSecureRandom(IV_SIZE);

            //IvParameterSpec IVSpec = new IvParameterSpec(IV);


            try
            {
                //_cipher.init(Cipher.DECRYPT_MODE, key, IVSpec);
                _cipher.Init(true, new ParametersWithIV(new KeyParameter(key.Key), IV));
            }
            catch (Exception ex)
            {
                log("初始化Cipher出错:");

                //ex.printStackTrace();

                return(null);
            }

            // 加密数据
            byte[] cipher_bytes = null;

            try
            {
                cipher_bytes = _cipher.DoFinal(bytes);
            }
            catch (Exception ex)
            {
                log("加密数据出错:");

                //ex.printStackTrace();

                return(null);
            }

            // long start = System.currentTimeMillis();

            // 噪音数据
            byte[] noise_bytes = (cipher_bytes.Length < NOISE_MAX / 2) ? getSecureRandom(new Random().Next(NOISE_MAX)) : new byte[0];

            // long end = System.currentTimeMillis();

            // log("噪音字节长度:" + noise_bytes.length);

            // log("制造噪音时间:" + (end - start));

            // [IV+加密数据+噪音数据]的长度值加密,30个字节
            byte[] size_bytes = encrypt(key, getBlockSizeBytes((IV_SIZE + cipher_bytes.Length), noise_bytes.Length));

            if (size_bytes == null || size_bytes.Length != ENCRYPT_SIZE)
            {
                return(null);
            }

            byte[] all_cipher = new byte[size_bytes.Length + IV_SIZE + cipher_bytes.Length + noise_bytes.Length];

            System.Array.Copy(size_bytes, 0, all_cipher, 0, size_bytes.Length);

            System.Array.Copy(IV, 0, all_cipher, size_bytes.Length, IV.Length);

            System.Array.Copy(cipher_bytes, 0, all_cipher, size_bytes.Length + IV.Length, cipher_bytes.Length);

            if (noise_bytes.Length > 0)
            { // 是否加噪音数据
                System.Array.Copy(noise_bytes, 0, all_cipher, size_bytes.Length + IV.Length + cipher_bytes.Length, noise_bytes.Length);
            }

            size_bytes = null;

            IV = null;

            cipher_bytes = null;

            noise_bytes = null;

            return(all_cipher);
        }