如何使用Qt加密/解密/播放视频?

时间:2015-03-28 11:57:50

标签: qt

我正在寻找一种方法来加密视频文件,然后使用它解密并直接播放。

2 个答案:

答案 0 :(得分:1)

setMedia将QIOdevice作为第二个参数:

  #include <QMediaPlayer>
  #include <QApplication>
  #include <QFile>
  int main(int argc, char ** argv)
  {
      QApplication app(argc,argv);
      QString fileName = app.arguments().at(1);

      QFile io(fileName);
      io.open(QFile::ReadOnly);

      QMediaPlayer player;

      player.setMedia(QUrl("test.mp3"), &io);
      player.play();

      return app.exec();
  }

但是,如果你真的意味着QDataStream:

QDataStream是缓冲io,QIODevice是直接io,它们不兼容,所以你必须像这样加倍缓冲:

#include <QMediaPlayer>
#include <QApplication>
#include <QFile>
#include <QBuffer>
#include <QDataStream>
int main(int argc, char ** argv)
{
    QApplication app(argc,argv);
    QString fileName = app.arguments().at(1);

    // our double buffer
    QByteArray bufferSpace;

    // our stream on which we can put "media" data
    QDataStream stream(&bufferSpace, QIODevice::WriteOnly);

    // this is just demo data
    QFile io(fileName);
    io.open(QFile::ReadOnly);
    stream << io.readAll();

    // open an IO Device on our buffer
    QBuffer buffer(&bufferSpace);
    buffer.open(QBuffer::ReadOnly);

    // give the IO to the media player
    QMediaPlayer player;
    player.setMedia(QUrl("test.mp3"), &buffer);
    player.play();

    return app.exec();
}

修改

这是您发布的“加密”代码的更快版本,而不使用整个文件的缓冲区:

#include <QMediaPlayer>
#include <QApplication>
#include <QFile>
#include <QBuffer>
#include <QDataStream>

static const unsigned char key = 0xAB;
class MyFunnyCrypto : public QFile /*or subclass any other IO*/
{
protected:
    virtual qint64 readData(char *data, qint64 maxSize)
    {
        qint64 r = QFile::readData(data, maxSize);
        if (r > 0) {
            for (qint64 i = 0; i < r; i++) {
                data[i] = data[i]^key;
            }
        }
        return r;
    }

};


int main(int argc, char ** argv)
{
    QApplication app(argc,argv);
    QString fileName = app.arguments().at(1);


    MyFunnyCrypto io;
    io.setFileName(fileName);
    io.open(QFile::ReadOnly);

    // give the IO to the media player
    QMediaPlayer player;
    player.setMedia(QUrl("test.mp3"), &io);
    player.play();

    return app.exec();
}

答案 1 :(得分:1)

感谢@aep。 它与QDataStream没有任何关系,而且非常简单。 加密:

unsigned char key = 0xAB;
QFile file("test.mp3");
file.open(QIODevice::ReadOnly);
QByteArray byteArray = file.readAll();
for(int i=1024;i<byteArray.length();i++){
    byteArray[i] = byteArray[i]^key;
}
file.close();

QFile encrypted("encrypted.mp3");
encrypted.open(QIODevice::WriteOnly);
encrypted.write(byteArray);
encrypted.close();

解密:

unsigned char key = 0xAB;
QFile file("encrypted.mp3");
file.open(QIODevice::ReadOnly);
auto byteArray = file.readAll();
for(int i=1024;i<byteArray.length();i++){
     byteArray[i] = byteArray[i]^key;
}

QBuffer buffer(&byteArray);
buffer.open(QBuffer::ReadOnly);

// give the IO to the media player
QMediaPlayer player;
player.setMedia(QUrl(), &buffer);
player.play();

在for循环中,我想将i声明为qint64而不是int。但编译器显示QByteArray::​operator[](int i)