本人近期在做國創專案,其中需要使用重復碼,并且使用經過重復碼編碼后的資料進行加密,
問題是重復碼編碼后再讀取就亂碼了,而且我不知道怎么樣可以讀取出二進制資料
重復碼代碼如下:
- #include<iostream>
- #include<stdlib.h>
- #include<string>
- #include<fstream>
- using namespace std;
- static unsigned int inbfr,outbfr;
- static FILE *outfile,*infile;
- static int incnt,outcnt,mask;
- voidinit()
- {
- outbfr=0;
- outcnt=8;
- inbfr=0;
- incnt=8;
- mask=0x80; //10000000
- }
- intgetbit()
- {
- int bitval;
- bitval=inbfr&mask; //bitval0000000
- incnt--; //7
- mask >>= 1; //01000000
- bitval >>= incnt;
- if (incnt==0)
- {
- inbfr=fgetc(infile);
- incnt=8;
- mask=0x80;
- }
- return bitval; //0000000bitval
- }
- voidputbit( int bitval)
- {
- outbfr = (outbfr<<1)&255; //00000000
- outbfr |= bitval; //0000000bitval
- outcnt --;
- if (outcnt==0)
- {
- fputc(outbfr,outfile);
- outcnt = 8;
- }
- }
- voidalignbits()
- {
- if (outcnt!=8)
- {
- for (int i=0;i<outcnt;i++)
- putbit(0);
- }
- }
- voiddecode()
- {
- int n = 3;
- int bitsum;
- if ((infile = fopen("ciphertext.txt", "rb")) == NULL)
- {
- printf("cannot open infile!!!\n");
- exit(0);
- }
- if ((outfile = fopen("decryption.txt", "wb")) == NULL)
- {
- printf("cannot open outfile!!!\n");
- exit(0);
- }
- init();
- inbfr = fgetc(infile);
- while (!feof(infile))
- {
- bitsum = 0;
- for (int i = 0; i < n; i++) bitsum += getbit();
- if (bitsum >= 2) putbit(1);
- else putbit(0);
- }
- alignbits();
- fclose(infile);
- fclose(outfile);
- }
- voidcode()
- {
- int n = 3;
- int bitval;
- if((infile=fopen("plaintext.txt","rb"))==NULL)
- {
- int a;
- cout << "請輸入數值: ";
- cin >> a;
- ofstream file_writer("plaintext.txt", ios_base::out);
- file_writer << a;
- file_writer.close();
- }
- if ((infile = fopen("plaintext.txt", "w+")) != NULL)
- {
- int a;
- cout << "請輸入數值: ";
- cin >> a;
- ofstream file_writer("plaintext.txt", ios_base::out);
- file_writer << a;
- file_writer.close();
- }
- if((outfile=fopen("ciphertext.txt","wb"))==NULL)
- {
- ofstream infile("ciphertext.txt");
- }
- init();
- inbfr=fgetc(infile);
- while(!feof(infile))
- {
- bitval=getbit();
- for (int i=0;i<n;i++)
- putbit(bitval);
- }
- alignbits();
- ifstream in("ciphertext.txt");
- string str;
- while (getline(in, str));
- cout << "重復碼編碼后的內容: " << str << endl; //無效讀取
- in.close();
- fclose(infile);
- fclose(outfile);
- }
- intmain(){
- code();
- decode();
- }
亂碼截圖如下:

我試過網上的讀取方法,我用的都不適用,
我想讀取重復碼編碼后的二進制資料,最好是用十進制表示的二進制數 ,因為后續還要加密(例如 2 表示為 十進制的10),求各位大佬幫助!
標籤:其他
上一篇:SpringMvc(五) - 支付寶沙箱和關鍵字過濾,md5加密,SSM專案重要知識點
下一篇:java基礎-檔案與IO
