Mastering Algorithms With C - Kyle Loudon [190]
* *
* ----------------------------- cbc_encipher ----------------------------- *
* *
*****************************************************************************/
void cbc_encipher(const unsigned char *plaintext, unsigned char *ciphertext,
const unsigned char *key, int size) {
unsigned char temp[8];
int i;
/*****************************************************************************
* *
* Encipher the initialization vector. *
* *
*****************************************************************************/
des_encipher(&plaintext[0], &ciphertext[0], key);
/*****************************************************************************
* *
* Encipher the buffer using DES in CBC mode. *
* *
*****************************************************************************/
i = 8;
while (i < size) {
bit_xor(&plaintext[i], &ciphertext[i - 8], temp, 64);
des_encipher(temp, &ciphertext[i], NULL);
i = i + 8;
}
return;
}
/*****************************************************************************
* *
* ----------------------------- cbc_decipher ----------------------------- *
* *
*****************************************************************************/
void cbc_decipher(const unsigned char *ciphertext, unsigned char *plaintext,
const unsigned char *key, int size) {
unsigned char temp[8];
int i;
/*****************************************************************************
* *
* Decipher the initialization vector. *
* *
*****************************************************************************/
des_decipher(&ciphertext[0], &plaintext[0], key);
/*****************************************************************************
* *
* Decipher the buffer using DES in CBC mode. *
* *
*****************************************************************************/
i = 8;
while (i < size) {
des_decipher(&ciphertext[i], temp, NULL);
bit_xor(&ciphertext[i - 8], temp, &plaintext[i], 64);
i = i + 8;
}
return;
}
Description of RSA
RSA (Rivest-Shamir-Adleman) is one of the most popular asymmetric, or public-key, ciphers. RSA is asymmetric because the key used to encipher data is not the same key used to decipher it. Like DES, RSA is a block cipher, but the block size varies depending on the size of the keys. If the amount of data to be encrypted is not an even multiple of this size, it is padded in some application-specific way.
One important implication of RSA being an asymmetric cipher is that when transmitting data across a network, the key used to encipher the data does not have to be transmitted with the data itself. Thus, there is less chance of having the key compromised. RSA is also useful when parties enciphering data are not allowed to decipher the data of others. Parties who wish to encipher data use one key, which is considered public, while parties allowed to decipher the data use a second key, which they keep private.
RSA is considered very secure, but it runs considerably slower than DES. As with DES, the security of RSA has never been proven, but it is related to the difficult problem of factoring large numbers (numbers containing at least 200 decimal digits). Since no efficient solutions are known for this problem, it is conjectured that there are no efficient ways to crack RSA.
RSA is based on principles that are less obtuse than the numerous permutations and substitutions performed in DES. Fundamentally, enciphering and deciphering data revolves around modular exponentiation, an operation in modular arithmetic. Modular arithmetic is integer arithmetic as usual except that when we work modulo n, every result x is replaced with a member of {0, 1, . . . , n - 1} so that x mod n is the remainder of x /n. For example, 40 mod 11 = 7 because 40/11 = 3 with a remainder of 7. Modular exponentiation is the process of computing ab mod n.
Computing Public and Private Keys
In RSA, the public key and private key work together as a pair. The public key is used to encipher a block of data, after which only the corresponding