UVALive 4174 Steganography 字符串处理 密码学

隐写术解密
本文介绍了一种基于隐写术的解密方法,通过分析文本中连续空格的数量来提取隐藏的信息。具体步骤包括判断空格数量的奇偶性、转换成二进制并解析成ASCII字符。

Problem H: Steganography

Source file:steg.{ccppjava}
Input file:steg.in

In cryptography, the goal is to encrypt a message so that, even if the the message is intercepted, only the intended recipient can decrypt it. In steganography, which literally means "hidden writing", the goal is to hide the fact that a message has even been sent. It has been in use since 440 BC. Historical methods of steganography include invisible inks and tatooing messages on messengers where they can't easily be seen. A modern method is to encode a message using the least-significant bits of the RGB color values of pixels in a digital image.

For this problem you will uncover messages hidden in plain text. The spaces within the text encode bits; an odd number of consecutive spaces encodes a 0 and an even number of consecutive spaces encodes a 1. The four texts in the example input below (terminated by asterisks) encode the following bit strings: 11111, 000010001101101, 01, and 000100010100010011111. Each group of five consecutive bits represents a binary number in the range 0–31, which is converted to a character according to the table below. If the last group contains fewer than five bits, it is padded on the right with 0's.

" " (space)0
"A" – "Z"1–26
"'" (apostrophe)27
"," (comma)28
"-" (hyphen)29
"." (period)30
"?" (question mark)31

The first message is 111112 = 3110 = "?". The second message is (00001, 00011, 01101)2 = (1, 3, 13)10 = "ACM". The third message is 010002 = 810 = "H", where the underlined 0's are padding bits. The fourth message is (00010, 00101, 00010, 01111, 10000)2 = (2, 5, 2, 15, 16)10 = "BEBOP".

Input: The input consists of one or more texts. Each text contains one or more lines, each of length at most 80 characters, followed by a line containing only "*" (an asterisk) that signals the end of the text. A line containing only "#" signals the end of the input. In addition to spaces, text lines may contain any ASCII letters, digits, or punctuation, except for "*" and "#", which are used only as sentinels.

Output: For each input text, output the hidden message on a line by itself. Hidden messages will be 1–64 characters long.

Note: Input text lines and output message lines conform to all of the whitespace rules listed in item 7 of Notes to Teams except that there may be consecutive spaces within a line. There will be no spaces at the beginning or end of a line.

Example input:Example output:
Programmer,
I  would  like  to  see
a  question
mark.
*
Behold, there is more to  me than you might
think  when  you read  me  the first  time.
*
Symbol   for    hydrogen?
*
A B C D  E F G H  I J  K L M N  O P Q  R  S  T  U  V
*
#
?
ACM
H
BEBOP

Last modified on October 21, 2008 at 9:17 PM.


思路比较简单的密码转换;

1     计算两单词之间 空格是奇是偶;

2     偶数变成1奇数记录为0 

3    五个二进制数变成十进制 不足的话二进制末尾补齐0

4   将十进制转化为对应的单词。

post code:


#include<stdio.h>
#include<string.h>
char a[1000];
int b[1000],len,code[1000],ji=1;
char put[1000];
void fun( int j)
{
   memset(code,0,sizeof(code));
   int i,base=16;
   ji=1;
   for( i=1; i<j; i++ )
   {
       if(i%5==0){code[ji]+=b[i]*base;ji++; base=16;}
       else{
           code[ji]+=b[i]*base;
           base/=2;
           } 
   }     
}

int main()
{   int flag=1;
    while(flag)
    {   int k=1,j=1,i;
         while(gets(a))
         {
             if( a[0]=='*'  )break;
             if( a[0]=='#'  ){flag=0;break;}
             len=strlen(a);
             int sum=0;
             for(i=0;i<len;i++)
             {
                 if(a[i]==' ')sum++;
                 if(a[i]!=' '&&sum!=0){     //统计两单词间空格的个数 偶数为1 奇数为0
                                       if(sum%2==0){b[j]=1;j++ ;}
                                       else {b[j]=0;j++;}
                                       sum=0;
                                      }              
             }                   
         }
         
         
       if(a[0]=='*')
         {
             fun(j);     //每五位二进制数转化为10进制数 不足五位 后面补齐

             if((j-1)%5==0)ji=(j-1)/5;
             else ji=(j-1)/5+1;
              int ge=0;
             for( i=1; i<=ji; i++)      //十进制对应的字符转化
             {
               if(code[i]>=1&&code[i]<=26)put[ge]=code[i]-1+'A';
               if(code[i]==0)put[ge]=' ';
               if(code[i]==27)put[ge]='\'';
               if(code[i]==28)put[ge]=',';
               if(code[i]==29)put[ge]='-';
               if(code[i]==30)put[ge]='.'; 
               if(code[i]==31)put[ge]='?'; 
               ge++;               
             }
            // put[ge]='\0';
            ge-=1;
             i=0;
             while(put[i]==' ')i++;   //这里个wa了一次 要注意hint的内容 末尾和开始都木有空格。
             while(put[ge]==' ')ge--;
             for(;i<=ge;i++)
             putchar(put[i]);
             printf("\n");
                         
         }

        
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值