C++ encryption


SUBMITTED BY: Guest

DATE: Jan. 16, 2014, 11:34 a.m.

FORMAT: C++

SIZE: 3.5 kB

HITS: 681

  1. A great code about encryption
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #define cypherbits 256 /* encryption strength in bits */
  7. #define cypher cypherbits/(sizeof(int)*8)
  8. int seed[cypher];
  9. void modseed() {
  10. int x;
  11. for(x=1;x<cypher;x++)seed[x]=seed[x]*0x81772012+seed[x]+0x49122035+seed[x+1];
  12. for(x=1;x<cypher;x++)seed[0]=seed[0]^seed[x];
  13. }
  14. int seedsum() {
  15. int n,x;
  16. n=0x80379251;
  17. for(x=0;x<cypher;x++)n=n^seed[x];
  18. return((n>>24)^((n>>16)&255)^((n>>8)&255)^(n&255));
  19. }
  20. char strequ(char *s1,char *s2) {
  21. int p;
  22. p=0;
  23. while((s1[p]==s2[p])&&(s1[p]!=0)&&(s2[p]!=0))p++;
  24. if(s1[p]==s2[p])return(1); else return(0);
  25. }
  26. int main(int argc,char *argv[]) {
  27. char
  28. banner[]="\x43\x6f\x64\x65\x64\x20\x62\x79\x20\x50\x72\x61\x76\x65\x65\x6e\x61"
  29. "\x20\x6f\x66\x20\x49\x6e\x64\x69\x61"
  30. "\x20\x28\x70\x76\x6e\x63\x61\x64\x40\x6b\x72\x65\x63\x2e\x65\x72\x6e\x65\x74\x2e\x69\x6e\x29";
  31. char buf[2048];
  32. int p,r,l,i,t,s,x;
  33. char b,c,pct,lpct;
  34. FILE *infile=NULL,*outfile=NULL;
  35. fprintf(stderr, "%s\n", banner);
  36. if(argc!=4){
  37. fprintf(stderr,"use: %s <infile> <outfile> <key>\n",argv[0]);
  38. return -1;
  39. }
  40. if(strequ(argv[1],"stdin"))infile=stdin; else
  41. if((infile=fopen(argv[1],"r"))==NULL){
  42. fprintf(stderr,"failed to open %s\n",argv[1]);
  43. return -1;
  44. }
  45. if(strequ(argv[2],"stdout"))outfile=stdout; else
  46. if((outfile=fopen(argv[2],"w"))==NULL){
  47. fprintf(stderr,"failed to create %s\n",argv[2]);
  48. return -1;
  49. }
  50. if(infile!=stdin) {
  51. fseek(infile,0,SEEK_END);
  52. l=ftell(infile);
  53. rewind(infile);
  54. } else l=0;
  55. s=l;
  56. t=0;
  57. pct=0;
  58. if(l<1)fprintf(stderr,"Encrypting data.. (%d bit cypher)\n",cypher*sizeof(int)*8);
  59. else fprintf(stderr,"Encrypting %d bytes.. (%d bit cypher)\n",l,cypher*sizeof(int)*8);
  60. /* bzero(seed,sizeof(seed)); */
  61. modseed();
  62. p=0;
  63. while(argv[3][p]!=0){
  64. modseed();
  65. seed[0]=seed[0]+argv[3][p];
  66. modseed();
  67. p++;
  68. }
  69. i=0;
  70. if(l>0){
  71. fputc('[',stderr);
  72. x=(l/sizeof(buf));
  73. if(l-x*sizeof(buf)!=0)x+=1;
  74. if(x>38)x=38;
  75. for(p=0;p<x;p++) fputc(32,stderr);
  76. fputc(']',stderr);
  77. fputc(13,stderr);
  78. fputc('[',stderr);
  79. fflush(stderr);
  80. }
  81. c=1;
  82. while(c){
  83. r=fread(&buf,1,sizeof(buf),infile);
  84. if(r>0) {
  85. t+=r;
  86. if(l>0){
  87. lpct=pct;
  88. pct=t/(l/x);
  89. if(pct>lpct) {
  90. fputc(88+32*i,stderr);
  91. fflush(stderr);
  92. i=1-i;
  93. }
  94. } else {
  95. fputc(88+32*i,stderr);
  96. fflush(stderr);
  97. i=1-i;
  98. }
  99. p=0;
  100. while(p<r) {
  101. modseed();
  102. buf[p]=buf[p]^seedsum();
  103. p++;
  104. }
  105. if(fwrite(&buf,1,r,outfile)!=r) {
  106. fprintf(stderr,"\nerror writing data\n");
  107. return -1;
  108. }
  109. } else c=0;
  110. }
  111. if(l>0)fputc(']',stderr);
  112. fprintf(stderr,"\nDone. Wrote %d bytes.\n",t);
  113. }

comments powered by Disqus