+//g++ cipher_h265.cpp -o cipher_h265 -O3
+
+//example vid.h265 seed
+//cipher_h265 vid1.h265 38762
+
+
+#include <iostream>
+#include <fstream>
+#include <iterator>
+#include <vector>
+
+
+using namespace std;
+
+typedef unsigned char uchar;
+
+uchar modulo(int x,int N){ return (x % N + N) %N; }
+
+
+void cipher_frame(uchar *buf, int start_sl, int end_sl, int enc) {
+ for(int i=0;i<(end_sl-start_sl)/2;i++) {
+ int p=lrand48()%(end_sl-start_sl)+start_sl;
+ if(p<start_sl || p>=end_sl)
+ cout<<"pb"<<endl;
+ while(buf[p-1]==255 || buf[p]==255 ){
+ p=lrand48()%(end_sl-start_sl)+start_sl;
+ if(p<start_sl || p>=end_sl)
+ cout<<"pb2"<<endl;
+ }
+ uchar v=lrand48()%255;
+ if(enc) {
+ buf[p]=modulo((int)buf[p]+(int)v,255);
+ }
+ else {
+ buf[p]=modulo((int)buf[p]-(int)v,255);
+ }
+ }
+
+}
+
+
+
+void fxor(uchar *buf,int sz, int seed,int enc) {
+ int start_sl,end_sl;
+
+
+ int offset=0;
+
+ srand48(seed);
+
+ for(int i=0;i<sz-6;i++) {
+ if(((uint)buf[i])==0x00 && ((uint)buf[i+1])==0x00 && ((uint)buf[i+2])==0x01) {
+
+ if (( ((uint)buf[i+3])==0x26 || ((uint)buf[i+3])==0x2a ) && ((uint)buf[i+4])==0x01 ) {
+ cout<<"IDR or I slice "<<i<<"\t";
+ start_sl=i+7+offset;
+ int stop=0;
+ for(int k=i+1;k<sz-4 && !stop;k++) {
+ if(((uint)buf[k])==0x00 && ((uint)buf[k+1])==0x00 && ((uint)buf[k+2])==0x00 && ((uint)buf[k+3])==0x01 ) {
+ cout<<"end "<<k<<endl;
+ stop=1;
+ end_sl=k;//-offset;
+ }
+ }
+
+
+ cipher_frame(buf, start_sl, end_sl, enc);
+
+
+
+ }
+
+
+
+
+ }
+
+
+ if(((uint)buf[i])==0x00 && ((uint)buf[i+1])==0x00 && ((uint)buf[i+2])==0x00 && ((uint)buf[i+3])==0x01) {
+
+ if ( ( ((uint)buf[i+4])==0x02 || ((uint)buf[i+4])==0x10 || ((uint)buf[i+4])==0x12 ) && ((uint)buf[i+5])==0x01 ) {
+ cout<<"B or P slice "<<i<<"\t";
+ start_sl=i+7+offset;
+ int stop=0;
+ for(int k=i+1;k<sz-4 && !stop;k++) {
+ if(((uint)buf[k])==0x00 && ((uint)buf[k+1])==0x00 && ((uint)buf[k+2])==0x00 && ((uint)buf[k+3])==0x01 ) {
+ cout<<"end "<<k<<endl;
+ stop=1;
+ end_sl=k;//-offset;
+ }
+ }
+
+
+ cipher_frame(buf, start_sl, end_sl, enc);
+
+
+
+ }
+
+ }
+
+
+
+ }
+
+// cipher_frame(buf, 1000, sz, enc);
+
+
+}
+
+
+int main(int argc, char **argv){
+ cout<<argv[1]<< endl;
+
+ FILE *fp;
+ fp = fopen(argv[1], "r");
+ fseek(fp, 0L, SEEK_END);
+ int sz = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ uchar *buf=(uchar*)malloc(sz);
+ uchar *buf2=(uchar*)malloc(sz);
+ fread(buf, sz, 1, fp);
+ fclose(fp);
+
+
+ int seed=atoi(argv[2]);
+ cout<<sz<<endl;
+
+
+ fxor(buf,sz,seed,1);
+
+
+ fp = fopen("lena2.h265", "w");
+ fwrite (buf , 1, sz, fp);
+ fclose(fp);
+
+ for(int i=0;i<sz;i++) {
+ buf2[i]=buf[i];
+ }
+
+
+
+
+ fxor(buf2,sz,seed,0);
+
+ fp = fopen("lena3.h265", "w");
+ fwrite (buf2 , 1, sz, fp);
+ fclose(fp);
+
+
+
+
+
+
+
+}