0% found this document useful (0 votes)
30 views1 page

R1

This Java class defines methods for encrypting and decrypting strings using AES encryption. It imports necessary libraries, defines a SecretKeySpec and byte array for the key, and includes setKey, encrypt, and decrypt methods. The setKey method generates an AES encryption key from the input string. The encrypt method encrypts a string, and the decrypt method decrypts an encrypted string.

Uploaded by

Japesh reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views1 page

R1

This Java class defines methods for encrypting and decrypting strings using AES encryption. It imports necessary libraries, defines a SecretKeySpec and byte array for the key, and includes setKey, encrypt, and decrypt methods. The setKey method generates an AES encryption key from the input string. The encrypt method encrypts a string, and the decrypt method decrypts an encrypted string.

Uploaded by

Japesh reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.io.

UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.spec.SecretKeySpec;

public class AES {

private static SecretKeySpec secretKey;


private static byte[] key;

public static void setKey(final String myKey) {


MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();https://www.chegg.com/homework-help/questions-and-
answers/problem-13-53-gear-train-composed-four-helical-gears-three-shaft-axes-
single-plane-shown-f-q79446470
}
}

public static String encrypt(final String strToEncrypt, final String secret) {


try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder()
.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}

public static String decrypt(final String strToDecrypt, final String secret) {


try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder()
.decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
}

You might also like