java aes encryption

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;
 
public class AES {
 
    private static SecretKeySpec secretKey;
    private static byte[] key;
 
    public static void setKey(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 e) {
            e.printStackTrace();
        } 
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
 
    public static String encrypt(String strToEncrypt, 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(String strToDecrypt, 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;
    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
AES encyption in java AES encryption .net to java aes encryption iv cbc java aes encryption and decryption in java aes 256 encryption java aes encryption java source code aes256 encryption in java aes encryption in java java aes encryption iv aes encryption of text in java java aes encryption class password encryption java AES aes cipher java aes encryption java 11 AES encryption and decryption functions java aes encryption java library java aes 256 cbc encryption aes cryptography in java java aes 256 encryption example java aes encryption decryption example AES256 encryption in java aes-256 encryption java java aes 256 aes encryption key java where to store aes encryption key java how to do aes encryption in java aes encryption algorithm code in java java aes encryption library encryption with aes java aes java encryption java aes string encryption explained aes string encryption java aes text encryption java AES tutorial in java java aes 256 decrypt encryption java salt based aes encryption java generate secret key aes 256 java java aes encryption with salt example decrypt aes 128 bit key java aes program in java aes encryption program in java aes password encryption java java encryption example aes GCM encryption in java and decryption in java example console application aes encryption in java and decryption in java example console application how to encrypt with aes java javax crypto decrypt eaxmple java program for aes encryption and decryption aes encryption java code java aes algorithms java encryption and decryption AES in java implementation of aes 256 java java decrypt secret key java decrypt aes java aes encryption example aes code in java cipher.getinstance aes 256 aes decryption java java aes aes java decrypt example java aes encryption aes encryption java example AES encryption java aes 256 decrypt java java encryption and decryption example aes java aes 128 ecb mode java code
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source