Network Security First Two Exp
Network Security First Two Exp
```java
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.util.Base64;
// Message to be encrypted
byte[] text = "Secret Information".getBytes();
System.out.println("Message [Byte Format]: " +
Base64.getEncoder().encodeToString(text));
System.out.println("Message : " + new String(text));
```
Message Encryption Using DES Algorithm
-------
Message [Byte Format]: U2VjcmV0IEluZm9ybWF0aW9u
Message : Secret Information
Encrypted Message: tP/b5D1eTpS7J9Jpe6i+/Q==
Decrypted Message: Secret Information
```
**Changes Made:**
1. Added `Base64.getEncoder().encodeToString()` to convert byte arrays to readable
strings.
2. Fixed formatting for clarity in output messages.
```java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
**Sample Output:**
```
URL Encryption Using AES Algorithm
------------
Original URL: www.annauniv.edu
Encrypted URL: XXXXXXXXXXXXXXXX (Note: This will be different each time you run
the code)
Decrypted URL: www.annauniv.edu
```
Replace `XXXXXXXXXXXXXX` with the actual encrypted string that will be different on
each run.
To convert the Java DES encryption/decryption code into C using the OpenSSL
library, follow these steps. OpenSSL provides the necessary cryptographic functions
to perform DES operations.
```c
#include <stdio.h>
#include <string.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/encoder.h>
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
buff = (char*)malloc(bufferPtr->length);
memcpy(buff, bufferPtr->data, bufferPtr->length - 1);
buff[bufferPtr->length - 1] = '\0';
BIO_free_all(bio);
return buff;
}
int main() {
DES_cblock key;
DES_key_schedule schedule;
DES_cblock ivec;
DES_cblock plaintext = "Secret Information";
DES_cblock ciphertext;
DES_cblock decryptedtext;
// Encode to base64
char *encrypted_base64 = base64_encode(ciphertext, sizeof(ciphertext));
printf("Encrypted Message: %s\n", encrypted_base64);
free(encrypted_base64);
return 0;
}
```
### Explanation
4. **Output**: The program prints the base64-encoded encrypted message and the
decrypted message.
### Compilation
```bash
gcc des_example.c -o des_example -lssl -lcrypto
```
```bash
./des_example
```
```plaintext
Encrypted Message: <base64_encoded_string>
Decrypted Message: Secret Information
```
Yes, you can implement AES encryption and decryption in C as well. However, working
with AES in C requires a bit more setup than in Java because C doesn't have built-
in cryptographic libraries. You'll need to use an external library for AES
functionality. One popular choice is the [OpenSSL](https://www.openssl.org/)
library, which provides comprehensive cryptographic functionality, including AES
encryption.
Here's an example of how you can implement AES encryption and decryption in C using
OpenSSL:
### Example Code
1. **Install OpenSSL:**
On Debian-based systems like Ubuntu, you can install the development libraries
with:
```bash
sudo apt-get install libssl-dev
```
```c
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
void handleErrors(void) {
ERR_print_errors_fp(stderr);
abort();
}
int main() {
// Key and plaintext should be 16 bytes for AES-128
unsigned char key[16] = "annaUniversity";
unsigned char plaintext[16] = "www.annauniv.edu";
// Encrypt
aes_encrypt(key, plaintext, ciphertext);
// Decrypt
aes_decrypt(key, ciphertext, decryptedtext);
// Print results
printf("Original text: %s\n", plaintext);
printf("Encrypted text: ");
for (int i = 0; i < 16; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
printf("Decrypted text: %s\n", decryptedtext);
return 0;
}
```
```bash
gcc aes_example.c -o aes_example -lssl -lcrypto
```
```bash
./aes_example
```
### Explanation
### Note
- This example uses AES in ECB mode with a 128-bit key. ECB mode is not recommended
for most use cases due to its lack of security features. For real applications,
consider using a more secure mode like CBC or GCM, and ensure proper padding is
handled.
- Ensure that both the plaintext and key are 16 bytes long for AES-128. For
different key sizes (e.g., 192 or 256 bits), adjust accordingly.
- Error handling and security considerations (like padding and key management)
should be addressed in a production environment.