/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Samples/mcrypt/decrypt.c

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* First example: Encrypts stdin to stdout using TWOFISH with 128 bit key and CFB */
 
2
 
 
3
#include <mcrypt.h>
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include "tmpkey.h"
 
7
 
 
8
unsigned char ReadByte(char *s) {
 
9
    char str[3];
 
10
    str[0]=s[0];str[1]=s[1];str[2]=0;
 
11
    return strtoul(str,NULL,16);
 
12
}
 
13
 
 
14
main() {
 
15
  int i,j;
 
16
 
 
17
  MCRYPT td;
 
18
  char block_buffer;
 
19
  char s[]="101e966a2155120f79795782592f7aa75cb4";
 
20
  char sout[255];
 
21
  unsigned ssize;
 
22
  unsigned IVsize;
 
23
  unsigned blocksize;
 
24
  char *IV;
 
25
 
 
26
  td = mcrypt_module_open("twofish", NULL, "cfb", NULL);
 
27
  if (td==MCRYPT_FAILED) {
 
28
     return 1;
 
29
  }
 
30
  
 
31
  ssize=strlen(s);
 
32
  if (ssize<2) return 1;
 
33
  IVsize=ReadByte(s);
 
34
  if (ssize<4+IVsize) return 1;
 
35
  IV = malloc(IVsize);
 
36
  for (i=0,j=2;i<IVsize;i++,j+=2)
 
37
    IV[i]=ReadByte(s+j);
 
38
 
 
39
  i=mcrypt_generic_init( td, tmpkey, tmpkeysize, IV);
 
40
  if (i<0) {
 
41
     mcrypt_perror(i);
 
42
     return 1;
 
43
  }
 
44
 
 
45
  ssize=strlen(s+j)/2;
 
46
  for (i=0;i<ssize;i++,j+=2)
 
47
    sout[i]=ReadByte(s+j);
 
48
  sout[i]=0;
 
49
  
 
50
  mdecrypt_generic (td, sout, ssize);
 
51
  printf("%s\n",sout);
 
52
 
 
53
  mcrypt_generic_end(td);
 
54
 
 
55
  return 0;
 
56
 
 
57
}