免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3741 | 回复: 18
打印 上一主题 下一主题

[文本处理] 文件加密算法求助 [复制链接]

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-01-05 15:52 |只看该作者 |倒序浏览
30可用积分
本帖最后由 bikkuri 于 2016-01-05 16:00 编辑

大家好!
我有一个问题向大家请教!

假如有两个二进制文件a和b,其中a为明文文件(文件长度为x字节),b为密匙文件(文件长度为y字节)。

希望用以下算法用密匙文件对明文文件进行加密:
  1. 明文文件a        运算        密钥文件b
  2. 第一字节                XOR        最后一字节
  3. 第二字节                XOR        倒数第二字节
  4. 第n字节                XOR        第y-((n-1) mod y)字节 (其中0<n<y+1)
  5. ......
  6. 第y字节                XOR        第一字节
  7. 第y+1字节        XOR        最后一字节
  8. ...
  9. 第x字节                XOR        第y-((x-1) mod y)字节
复制代码
希望大家帮忙用shell脚本实现这个加密算法。
  1. cat a|file_xor b|file_xor b > c
  2. diff a c
复制代码
明文文件a经过两次用同一个密钥文件加密后得到的结果应该和原来的明文文件相同。

谢谢大家!

最佳答案

查看完整内容

回复 10# bikkuri run only, like as below:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4190772&page=1#pid24467396

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
2 [报告]
发表于 2016-01-05 15:52 |只看该作者
回复 10# bikkuri

run only, like as below:

http://bbs.chinaunix.net/forum.p ... ;page=1#pid24467396
   

论坛徽章:
12
IT运维版块每日发帖之星
日期:2015-11-17 06:20:00程序设计版块每日发帖之星
日期:2016-01-19 06:20:0015-16赛季CBA联赛之江苏
日期:2016-01-17 15:31:3915-16赛季CBA联赛之上海
日期:2016-01-16 15:44:3015-16赛季CBA联赛之浙江
日期:2016-01-15 20:38:1815-16赛季CBA联赛之北京
日期:2016-01-09 14:30:15CU十四周年纪念徽章
日期:2016-01-07 12:31:5115-16赛季CBA联赛之四川
日期:2016-01-01 11:49:1515-16赛季CBA联赛之深圳
日期:2015-12-24 14:23:4115-16赛季CBA联赛之山西
日期:2015-12-15 16:22:31技术图书徽章
日期:2015-12-10 17:41:0015-16赛季CBA联赛之北控
日期:2016-02-03 10:03:24
3 [报告]
发表于 2016-01-05 16:49 |只看该作者
  1. awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b a
复制代码

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
4 [报告]
发表于 2016-01-05 17:11 |只看该作者
谢谢您的回复!
不过显然加密处理后的结果是错误的。
  1. [root@hp8:~/newtest]# date > a
  2. [root@hp8:~/newtest]# date +%s > b
  3. [root@hp8:~/newtest]# ls -l
  4. total 8
  5. -rw-r--r-- 1 root root 29 Jan  5 17:09 a
  6. -rw-r--r-- 1 root root 11 Jan  5 17:09 b
  7. [root@hp8:~/newtest]# awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b a
  8. 14519849851521914131101451118515
  9. [root@hp8:~/newtest]# awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b a > c
  10. [root@hp8:~/newtest]# ls -l
  11. total 12
  12. -rw-r--r-- 1 root root 29 Jan  5 17:09 a
  13. -rw-r--r-- 1 root root 11 Jan  5 17:09 b
  14. -rw-r--r-- 1 root root 33 Jan  5 17:10 c
  15. [root@hp8:~/newtest]# awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b c > d
  16. [root@hp8:~/newtest]# ls -l
  17. total 16
  18. -rw-r--r-- 1 root root 29 Jan  5 17:09 a
  19. -rw-r--r-- 1 root root 11 Jan  5 17:09 b
  20. -rw-r--r-- 1 root root 33 Jan  5 17:10 c
  21. -rw-r--r-- 1 root root 35 Jan  5 17:10 d
  22. [root@hp8:~/newtest]#
复制代码
回复 2# sync_1521


   

论坛徽章:
12
IT运维版块每日发帖之星
日期:2015-11-17 06:20:00程序设计版块每日发帖之星
日期:2016-01-19 06:20:0015-16赛季CBA联赛之江苏
日期:2016-01-17 15:31:3915-16赛季CBA联赛之上海
日期:2016-01-16 15:44:3015-16赛季CBA联赛之浙江
日期:2016-01-15 20:38:1815-16赛季CBA联赛之北京
日期:2016-01-09 14:30:15CU十四周年纪念徽章
日期:2016-01-07 12:31:5115-16赛季CBA联赛之四川
日期:2016-01-01 11:49:1515-16赛季CBA联赛之深圳
日期:2015-12-24 14:23:4115-16赛季CBA联赛之山西
日期:2015-12-15 16:22:31技术图书徽章
日期:2015-12-10 17:41:0015-16赛季CBA联赛之北控
日期:2016-02-03 10:03:24
5 [报告]
发表于 2016-01-05 17:41 |只看该作者
回复 3# bikkuri
  1. test@server ~> cat a
  2. 0101010101111111111
  3. 0101010
  4. 111
  5. test@server ~> cat b
  6. 010101
  7. 10
  8. test@server ~> awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b a
  9. 0000001100101001101
  10. 1110011
  11. 101
  12. test@server ~> awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b a >c
  13. test@server ~> awk 'BEGIN{FS=OFS=""}NR==FNR{for(i=1;i<=NF;i++){a[++j]=$i};next}{for(i=1;i<=NF;i++){++k;$i=xor($i,a[k%j])}}1' b c >d
  14. test@server ~> cat d
  15. 0101010101111111111
  16. 0101010
  17. 111
  18. test@server ~>
复制代码
要把文件搞成010101这种形式的吧?
   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
6 [报告]
发表于 2016-01-05 18:16 |只看该作者
本帖最后由 jason680 于 2016-01-05 18:18 编辑

回复 1# bikkuri

Note: debug by yourself with busybox and awk null (ASCII 0) issue

$ awk 'BEGIN{RS="@#*#@";for(n=1;n<=255;n++)a[sprintf("%c",n)]=n}FNR==NR{y=length-1;for(n=1;n<=y;n++)b[n]=a[substr($0,n,1)];next}{x=length;s="";for(n=1;n<=x;n++)s=s sprintf("%c",xor(a[substr($0,n,1)],b[y-n%y]));printf("%s",s)}' b a  > a.x

$ awk 'BEGIN{RS="@#*#@";for(n=1;n<=255;n++)a[sprintf("%c",n)]=n}FNR==NR{y=length-1;for(n=1;n<=y;n++)b[n]=a[substr($0,n,1)];next}{x=length;s="";for(n=1;n<=x;n++)s=s sprintf("%c",xor(a[substr($0,n,1)],b[y-n%y]));printf("%s",s)}' b a.x  > c

$ diff a c

$ ls -l a b c
-rw-rw-r-- 1 jason jason 29 Jan  5 17:45 a
-rw-rw-r-- 1 jason jason 11 Jan  5 17:44 b
-rw-rw-r-- 1 jason jason 29 Jan  5 18:07 c

$ cat a b c
Tue Jan  5 17:45:03 CST 2016
1451987093
Tue Jan  5 17:45:03 CST 2016

   

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
7 [报告]
发表于 2016-01-06 13:03 |只看该作者
谢谢您的回复!
确实在Linux上您的程序可以得到正确的结果,但是在busybox上执行出错。
我研究一下看看在busybox上要怎么改。
另外您的程序中的RS="@#*#@"是什么意思啊?
好像从来没有见过这种写法,网上搜索了也没找到。
和RS=“”有什么区别吗?

回复 5# jason680


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
8 [报告]
发表于 2016-01-06 13:52 |只看该作者
回复 6# bikkuri

https://www.gnu.org/software/gawk/manual/gawk.html#Multiple-Line

4.8 Multiple-Line Records
...

You can achieve the same effect as ‘RS = ""’ by assigning the string "\n\n+" to RS. This regexp matches the newline at the end of the record and one or more blank lines after the record. In addition, a regular expression always matches the longest possible sequence when there is a choice (see Leftmost Longest). So, the next record doesn’t start until the first nonblank line that follows—no matter how many blank lines appear in a row, they are considered one record separator.
...



https://www.gnu.org/software/gawk/manual/gawk.html#gawk-split-records

4.1.2 Record Splitting with gawk

                                           RS = "\0" Is Not Portable
There are times when you might want to treat an entire data file as a single record. The only way to make this happen is to give RS a value that you know doesn’t occur in the input file. This is hard to do in a general way, such that a program always works for arbitrary input files.

You might think that for text files, the NUL character, which consists of a character with all bits equal to zero, is a good value to use for RS in this case:

BEGIN { RS = "\0" }  # whole file becomes one record?

gawk in fact accepts this, and uses the NUL character for the record separator. This works for certain special files, such as /proc/environ on GNU/Linux systems, where the NUL character is in fact the record separator. However, this usage is not portable to most other awk implementations.

Almost all other awk implementations19 store strings internally as C-style strings. C strings use the NUL character as the string terminator. In effect, this means that ‘RS = "\0"’ is the same as ‘RS = ""’. (d.c.)

It happens that recent versions of mawk can use the NUL character as a record separator. However, this is a special case: mawk does not allow embedded NUL characters in strings. (This may change in a future version of mawk.)

See Readfile Function, for an interesting way to read whole files. If you are using gawk, see Extension Sample Readfile, for another option.
   

论坛徽章:
18
辰龙
日期:2014-05-21 21:01:4115-16赛季CBA联赛之深圳
日期:2016-12-23 13:51:3815-16赛季CBA联赛之北控
日期:2016-11-28 18:26:3815-16赛季CBA联赛之佛山
日期:2016-11-03 11:18:5815-16赛季CBA联赛之辽宁
日期:2016-07-10 16:09:4115-16赛季CBA联赛之江苏
日期:2016-02-20 23:09:202015亚冠之塔什干棉农
日期:2015-08-17 19:49:492015年亚洲杯之日本
日期:2015-04-30 01:24:342015年亚洲杯之约旦
日期:2015-04-01 00:37:182015年亚洲杯之沙特阿拉伯
日期:2015-03-02 15:55:40处女座
日期:2014-05-25 10:34:0020周年集字徽章-年
日期:2023-04-23 11:17:52
9 [报告]
发表于 2016-01-06 14:13 |只看该作者
本帖最后由 bikkuri 于 2016-01-06 14:16 编辑

啊?原来RS="@#*#@"是用一个不可能出现的RS来把整个文件当作一个Record。
但是如果要处理的文件就是程序本身的话,这个RS就会把文件分成两个Record吧。
我觉得用RS=“$(md5sum $0|awk '{print$1}')”的话,也就是用文件的md5校验和来做RS,就可以确保RS不会出现在文件里吧?

回复 7# jason680


   

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
10 [报告]
发表于 2016-01-06 16:56 |只看该作者
回复 8# bikkuri

>> ...但是如果要处理的文件就是程序本身的话...

说说你要做什么....
也可以只加密可打印字符(ASCII 32~126)
你说的我也曾做过...密码缺省则用档名
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP