versadac  1
versadac - Scalable Recorder Firmware
FullEncryption.h
1 /* $Id: FullEncryption.h 5196 2006-10-23 10:52:48Z martinto $ */
2 
3 // encryption algorithm used to encrypt passwords in 5000 security file
4 
5 #if !defined __FULLENCRYPTION_H
6 #define __FULLENCRYPTION_H
7 
8 #include "Cstring.h"
9 
10 typedef unsigned char byte;
11 typedef unsigned char BYTE;
12 
13 #ifdef WIN32
14 
15 #ifdef ERROR
16 #undef ERROR
17 #endif
18 
19 // From VxWorks
20 typedef int STATUS;
21 
22 /* return status values */
23 #define OK 0
24 #define ERROR (-1)
25 
26 
27 #endif
28 
30 {
31  public:
32 /*---------------------------------------------------------------------------
33 FUNCTION : encryptToString
34 DESCRIPTION : static method to encrypt a string
35  * Encrypts the given byte array to a string with the provided key.
36  * Uses the service to encrypt to a byte array and then converts this to
37  * a base64 string representation.
38 ARGUMENTS : z_data string to encrypt
39  z_key key to encrypt with
40  z_pOut where to put the answer
41 RETURN :
42 NOTES :
43 ---------------------------------------------------------------------------*/
44  static void encryptToString( const char *z_data, int z_key, CString* z_pOut );
45 
46 /*---------------------------------------------------------------------------
47 FUNCTION : decryptFromString
48 DESCRIPTION : static method to decrypt a string
49  * Attempts to decrypt the given string into a byte array using the provided
50  * key. This service converts the string from a base 64 character
51  * representation and then uses the decrypt service.
52 ARGUMENTS : z_data string to decrypt
53  z_key key to decrypt with
54  z_pOut where to put the answer
55 RETURN :
56 NOTES :
57 ---------------------------------------------------------------------------*/
58  static STATUS decryptFromString( const char *z_data, int z_key, CString* z_pOut );
59 
60 
61 
62  private:
63 
64 /*---------------------------------------------------------------------------
65 FUNCTION : adler32
66 DESCRIPTION : static method generate adler32 crc
67  derived from zlib free source code
68 ARGUMENTS :
69 RETURN :
70 NOTES :
71 ---------------------------------------------------------------------------*/
72  static unsigned long adler32(unsigned long adler, const byte* buf, int len);
73 
74 
75 /*---------------------------------------------------------------------------
76 FUNCTION : decrypt
77 DESCRIPTION : static method to encrypt a string
78  * Attempts to decrypt the given byte array into a byte array using the
79  * provided key. This service checks the CRC, if that is OK then it
80  * uses the decrypt3Bytes service repeatedly to decrypt the data, the key is
81  * then checked before returning the decrypted byte array.
82 ARGUMENTS : z_data string to decrypt
83  z_key key to decrypt with
84  z_nLength number of bytes in z_data
85 RETURN : pointer to new byte array containing decrypted data (binary)
86  the caller must delete this when done
87 NOTES : used as part of the total encryption
88 ---------------------------------------------------------------------------*/
89  static BYTE* decrypt( const byte* z_data, int z_key, int z_nlength );
90 
91 /*---------------------------------------------------------------------------
92 FUNCTION : decrypt3Bytes
93 DESCRIPTION : * Decrypts the given three bytes into the ls3 bytes of an int.
94  * The service combines the passed in bytes into an int and then calls
95  * decrypt3Bytes.
96 ARGUMENTS : z_b1 first byte
97  z_b2 second byte
98  z_b3 third byte
99 RETURN : data decrypted in an int
100 NOTES : used as part of the total encryption
101 ---------------------------------------------------------------------------*/
102  static int decrypt3Bytes( BYTE b1, BYTE b2, BYTE b3 );
103 
104 /*---------------------------------------------------------------------------
105 FUNCTION : decrypt3Bytes
106 DESCRIPTION : * Decrypts the given int into the ls3 bytes of an int.
107 ARGUMENTS : z_n data to decrypt
108 RETURN : data decrypted in an int
109 NOTES : used as part of the total encryption
110 ---------------------------------------------------------------------------*/
111  static int decrypt3Bytes( int z_n );
112 
113 /*---------------------------------------------------------------------------
114 FUNCTION : encrypt
115 DESCRIPTION : static method to encrypt a string
116  * Encrypts the given byte array to a byte array with the provided key.
117  * Combines the key with the data using EXOR, appends the key and then
118  * encrypts the data with repeated calls to encrypt3bytes. An adler32
119  * CRC is trhen appended.
120 ARGUMENTS : z_data string to encrypt
121  z_key key to encrypt with
122 RETURN : pointer to new byte array containing encrypted data (binary)
123  the caller must delete this when done
124 NOTES : used as part of the total encryption
125 ---------------------------------------------------------------------------*/
126  static BYTE* encrypt( const char *z_data, int z_key );
127 
128 /*---------------------------------------------------------------------------
129 FUNCTION : encrypt3Bytes
130 DESCRIPTION : static method
131  * Encrypts the given three bytes and returns an encrypted value
132  * Combines the bytes into the ls3 bytes of an int and calls the encrypt
133  * service.
134  * least significant 24 bits of an int.
135  * @param z_b1 first byte
136  * @param z_b2 second byte
137  * @param z_b3 third byte
138  * @return bytes encrypted in ls 24 bits of an int
139 NOTES :
140 ---------------------------------------------------------------------------*/
141  static int encrypt3Bytes( byte z_b1, byte z_b2, byte z_b3 );
142 
143 /*---------------------------------------------------------------------------
144 FUNCTION : encrypt3Bytes
145 DESCRIPTION : static method
146  * Encrypts the least significant three bytes of the passed in int and
147  * returns an encrypted value in the least significant 24 bits of an int.
148  * @param z_n int to encrypt
149  * @return z_n encrypted in ls 24 bits of an int
150 NOTES :
151 ---------------------------------------------------------------------------*/
152  static int encrypt3Bytes( int z_n );
153 
154 };
155 
156 
157 #endif // __FULLENCRYPTION_H
Definition: CString.h:6
Definition: FullEncryption.h:29