1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
/* encrypt.pike 
 * 
 */ 
 
// The name of the receiver: 
#define NAME "nisse" 
 
int main(int argc, array(string) argv) 
{ 
  object rsa = keys.pub_to_rsa(Stdio.read_file(NAME + ".pub")); 
  //Reads a rsa-key from nisse.pub and converts it to a rsa-object. 
 
 
  function r = Crypto.randomness.reasonably_random()->read; 
  //Gets a function that kan be used for generation of random bits. 
 
 
  string iv = r(8); 
  //Creates a random 8-octet initvector (Just as big as a IDEA-block) 
 
  string session_key = r(16); 
  //Creates a 16 octet (128-bit, key size for IDEA) seasion key. 
 
  object cipher = Crypto.crypto(Crypto.idea_cbc() 
    ->set_encrypt_key(session_key) 
    ->set_iv(iv)); 
  //creates an object that crypts with IDEA in CBC-mode 
 
   
  string data = Stdio.stdin->read(0x7fffffff); 
  //Reads a file from Standard Input. 
 
  if (!data) 
  { 
    werror("Read from stdin failed.\n"); 
    return 1; 
  } 
 
  string gibberish = cipher->crypt(data) + cipher->pad(); 
  //Crypt the file with IDEA-CBC-crypto-object 
 
  string encrypted_key = rsa->encrypt(session_key); 
  //Crypt season key with RSA. 
   
  string data = sprintf("%s%s%s%s", iv, 
                        iv ^ sprintf("%4c%4c", 
                                     strlen(encrypted_key), 
                                     strlen(gibberish)), 
                        encrypted_key, 
                        gibberish); 
  //Writes the crypted file with the format: 
  // IV (8 octets) 
  // IV XOR (Length of crypted saeason key and crypted data. 8 octets) 
  // season key, crypted with RSA 
  // data, crypted with season key. 
         
 
 
  if (strlen(data) != Stdio.stdout->write(data)) 
  { 
    werror("Write to stdout failed.\n"); 
    return 1; 
  } 
 
  return 0; 
}