程 序 源 代 码
第一部分:相对算法代码
(一)提零压缩生成流密文程序
#include"iostream.h"
#include"math.h"
#include"stdlib.h"
#include"time.h"
#include"stdio.h"
#include"fstream.h"
#include"io.h"
char getword(FILE *fp)
{ int *s;
int i;
s=&i;
s[0]=getc(fp);
s[1]=getc(fp);
return (i);}//读入一个字
int position(int a,int b)//求数组元素之间相对关系的函数:0表示与本身同性;而1反之
{
if((a&&b)||(!a&&!b))
return 0;
else
return 1;
}
int main()
{ static unsigned char alawbyte[50000];//存放A率压缩后的样值
static int data[50000];
long offset=0;
int i;
int j=0;
int k=0;
long samples=0;
static int bitstream[50000];
static int bit[50000];//总的关系比特流
static int a[50000];//存放非零元素的数组
static int abit[50000];//存放非零元素数组的关系比特流
static int b[50000];//存放零元素的数组
static int bbit[50000];//存放零元素数组的关系比特流
int l=0;int m,n,t=0;
FILE *fp;
if((fp=fopen("m8000-1out_high.wav","rb"))==NULL)
{ printf("cannot open file");
exit(0);}
i=0;
while(!feof(fp))
{ bitstream[i]=getc(fp);
i++;}
for(k=0;k<16;k++)
printf("%d",bitstream[k]);
k=0;m=0;n=0;
for(j=0;j<i;j++)
{
bit[k]=position(bitstream[j],bitstream[j+1]);
k++;
if(bitstream[j])
{
a[m]=bitstream[j];
abit[m]=position(bitstream[j],bitstream[j+1]);
m++;
}
else
{
b[n]=bitstream[j];
bbit[n]=position(bitstream[j],bitstream[j+1]);
n++;
}
}
for(l=0;l<10;l++)
printf("%d",abit[l]);
printf("\n");
for(l=0;l<10;l++)
printf("%d",bbit[l]);
ofstream out1("G723.1编码提零压缩后的比特流密文a.wav",ios::binary);//存放最终转换好的比特流文件
if(!out1)
{ printf("Cannot open output file.\n!");
return 1;}
offset=0;
out1.seekp(offset,ios::beg);
for(t=0;t<=m-1;t++)
{ out1.write((char *)&a[t],2); }
out1.close();
ofstream out3("G723.1编码提零压缩后的零元素的数组b.wav",ios::binary);//存放最终转换好的比特流文件
if(!out3)
{ printf("Cannot open output file.\n!");
return 1;}
offset=0;
out3.seekp(offset,ios::beg);
for(t=0;t<=n-1;t++)
{ out3.write((char *)&b[t],1); }
out3.close();
ofstream out4("G723.1编码提零压缩后的相对关系bbit.wav",ios::binary);//存放最终转换好的比特流文件
if(!out4)
{ printf("Cannot open output file.\n!");
return 1;}
offset=0;
out4.seekp(offset,ios::beg);
for(t=0;t<=n-1;t++)
{ out4.write((char *)&bbit[t],1); }
out4.close();
ofstream out2("G723.1编码提零压缩后的相对关系abit.wav",ios::binary);//存放最终转换好的比特流文件
if(!out2)
{ printf("Cannot open output file.\n!");
return 1;}
offset=0;
out2.seekp(offset,ios::beg);
for(t=0;t<=m-1;t++)
{ out2.write((char *)&abit[t],1
); }
out2.close();
ofstream out5("G723.1编码提零压缩后的相对关系bit.wav",ios::binary);//存放最终转换好的比特流文件
if(!out5)
{ printf("Cannot open output file.\n!");
return 1;}
offset=0;
out5.seekp(offset,ios::beg);
for(t=0;t<=k-1;t++)
{ out5.write((char *)&bit[t],1); }
out5.close();
return 0;
}
(二)BIT流展开和LSB嵌入程序代码
#include"iostream.h"
#include"math.h"
#include"stdlib.h"
#include"time.h"
#include"stdio.h"
#include"fstream.h"
#include"io.h"
char getword(FILE *fp)
{ int *s;
int i;
s=&i;
s[0]=getc(fp);
s[1]=getc(fp);
return (i);}//读入一个字
int main()
{ static unsigned char alawbyte[5000000];//存放A率压缩后的样值
int channel=0,samplespersec=0,bitpersample=0;
static int data[5000000];
long offset=0;
static int head[44];
int i;
int j=0;
int k=0;
long samples=0;
static int bitstream[5000000];
static int bitstream1[5000000];
int l=0;
int bit=0;
static int group[8];
ifstream in("f8000-1alaw.wav",ios::binary);
if(!in)
{ cout<<"Cannot open input file.\n";
return 1;}
offset=0;
in.seekg(offset,ios::beg);
for(i=0;i<=28;i++)
offset=54;
in.seekg(offset,ios::beg);
offset=58;
免费网站源码资源分享in.seekg(offset,ios::beg);
for(i=0;i<=samples-1;i++)
{in.read((char *)&data[i],1);}//首先将数据全部读入data数组中
in.close();
FILE *fp;//此段程序读入G723.1编码压缩的密文比特流文件
if((fp=fopen("G723.1编码去零压缩后的m8000-1out_high.wav","rb"))==NULL)
{cout<<"cannot open file"<<endl;
exit(0);}
i=0;
while(!feof(fp))
{bitstream[i]=getword(fp);
i++;}
cout<<i-1<<endl;
fclose(fp);
l=0;
k=0;
while(k<i-1)
{ for(j=0;j<8;j++)
{bit=bitstream[k]&0x0080;
if(bit==0)
{bitstream1[l]=0;
l++;}
else
{bitstream1[l]=1;
l++;}
bitstream[k]<<=1;}
k++; }//此段程序将bit流散开
for(i=0;i<l;i++)
{cout<<bitstream1[i]<<' ';}
for(i=0;i<=(l-1);i++)
{if(bitstream1[i]==1)//bitstream1[i]=1;
{if(data[i]%2==0)
data[i]=data[i]+1;
else
data[i]=data[i];}
else if(bitstream1[i]==0)//bitstream1[i]=0;
{if(data[i]%2==0)
data[i]=data[i];
else
data[i]=data[i]-1;}}//进行LSB位的替换
ofstream out1("LSB替换后的加密语音alaw.wav",ios::binary);
if(!out1)
{cout<<"Cannot open output file.\n!";
return 1;}
offset=0;
out1.seekp(offset,ios::beg);
for(i=0;i<=28;i++)
{out1.write((char *)&head[i],2);}
offset=58;
out1.seekp(offset,ios::beg);
for(i=0;i<=samples-1;i++)
{out1.write((char *)&data[i],1);}
out1.close();
return 0;
}
(三)提取程序代码
#include"iostream.h"
#include"math.h"
#include"stdlib.h"
#include"time.h"
#include"stdio.h"
#include"fstream.h"
#include"io.h"
int multiply(int k)
{
int temp=1;
for(int i=0;i<k;i++)
temp
*=2;
return temp;
}
int main()
{ static unsigned char alawbyte[5000000];//存放A率压缩后的样值
int channel=0,samplespersec=0,bitpersample=0;
static int data[5000000];
long offset=0;
static int head[44];
static int a[1000000];
int i;
int j=0;
int k=0;
int l=0;
long samples=0;
static int bitstream[5000000];
static int bitstream1[5000000];
static int bit[5000000];
static int bit1[5000000][8];
ifstream in("LSB替换后的加密语音alaw.wav",ios::binary);
if(!in)
{ cout<<"Cannot open input file.\n";
return 1;}
offset=0;
in.seekg(offset,ios::beg);
for(i=0;i<=28;i++)
offset=54;
in.seekg(offset,ios::beg);
cout<<"该文件样本数为:"<<samples<<endl;
offset=58;
in.seekg(offset,ios::beg);
for(i=0;i<=samples-1;i++)
{in.read((char *)&data[i],1);}//首先将数据全部读入data数组中
in.close();
k=1;l=0;
i=0;
for(i=0;i<=samples-1;i++)
{
if(data[i]%2==0)
{bit[l]=0;l++;}
else
{bit[l]=1;l++;}
//cout<<bit[i]<<' ';
/
/ cout<<l<<endl;
}
for(i=0;i<=l;i++)
{
bit1[(i/8)][i%8]=bit[i];
}
for(i=0;i<=(l/8);i++)
{
a[i]=0;
for(j=0;j<8;j++)
a[i]+=bit1[i][j]*(multiply(7-j));
}
cout<<endl;
// for(i=0;i<(l/8);i++)
//cout<<a[i]<<' '<<endl;
ofstream out1("恢复的压缩密文m8000-1out_high.wav",ios::binary);
if(!out1)
{cout<<"Cannot open output file.\n!";
return 1;}
offset=0;
out1.seekp(offset,ios::beg);
k=0;
for(i=0;i<=(l/8);i++)
{
out1.write((char *)&a[i],2);
k++;}
out1.close();
return 0;}
(四)恢复源密文的程序代码
#include"iostream.h"
#include"math.h"
#include"stdlib.h"
#include"time.h"
#include"stdio.h"
#include"fstream.h"
#include"io.h"
char getword(FILE *fp)
{ int *s;
int i;
s=&i;
s[0]=getc(fp);
s[1]=getc(fp);
return (i);}//读入一个字
int main()
{ static unsigned char alawbyte[5000000];//存放A率压缩后的样值
static int data[5000000];
long offset=0;
int i;
int j=0;
int k=0;
long samples=0;
static int abit[5000000];
static int bbit[5000000];
static int c[5000000];
static int d[5000000];
static int e[5000000];
int m=0;int n=0;int l=0;int t=0;int p=0;int q=0;int w=0;
FILE *fp;
if((fp=fopen("G723.1编码提零压缩后的相对关系abit.wav","rb"))==NULL)
{ cout<<"cannot open file"<<endl;
exit(0);}
i=0;
while(!feof(fp))
{ abit[i]=getc(fp);
i++;}
fclose(fp);
/
/FILE *fp;
if((fp=fopen("G723.1编码提零压缩后的相对关系bbit.wav","rb"))==NULL)
{ cout<<"cannot open file"<<endl;
exit(0);}
l=0;
while(!feof(fp))
{ bbit[l]=getc(fp);
l++;}
fclose(fp);
//FILE *fp;
if((fp=fopen("G723.1编码提零压缩后的零元素的数组b.wav","rb"))==NULL)
{ cout<<"cannot open file"<<endl;
exit(0);}
j=0;
while(!feof(fp))
{ c[j]=getc(fp);
j++;}
fcl
ose(fp);
//FILE *fp;
if((fp=fopen("恢复的压缩密文m8000-1out_high.wav","rb"))==NULL)
{ cout<<"cannot open file"<<endl;
exit(0);}
k=0;
while(!feof(fp))
{ d[k]=getword(fp);
k++;}
fclose(fp);
e[0]=d[0];
t=0;m=1;n=1;p=1;q=0;
for(m=1;m<i+l-2;m++)
{
if(t==0)
{
if(abit[p-1]==0)
{
e[n]=d[p];
n++;p++;
}
else
{
e[n]=c[q];
n++;q++;
t=1;
}
}
else
{
if(bbit[q-1]==0)
{
e[n]=c[q];
n++;q++;
}
else
{
e[n]=d[p];
n++;p++;
t=0;
}
}
}
for(w=0;w<20;w++)
cout<<e[w]<<endl;
ofstream out1("G723.1解密的m8000-1out_high.wav",ios::binary);//存放最终转换好的比特流文件
if(!out1)
{ cout<<"Cannot open output file.\n!";
return 1;}
offset=0;
out1.seekp(offset,ios::beg);
for(w=0;w<n;w++)
{ out1.write((char *)&e[w],1); }
out1.close();
return 0;
}
??
??
??
?
?
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论