<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Deep Nikode</title>
    <description>The latest articles on DEV Community by Deep Nikode (@deepnikode).</description>
    <link>https://dev.to/deepnikode</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1716687%2Fb559d3f0-6f3a-453e-a35d-9e05cabea5eb.jpeg</url>
      <title>DEV Community: Deep Nikode</title>
      <link>https://dev.to/deepnikode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepnikode"/>
    <language>en</language>
    <item>
      <title>Enhancing Data Security with AES Encryption in Java 🚀🔒</title>
      <dc:creator>Deep Nikode</dc:creator>
      <pubDate>Tue, 02 Jul 2024 12:06:45 +0000</pubDate>
      <link>https://dev.to/deepnikode/enhancing-data-security-with-aes-encryption-in-java-95</link>
      <guid>https://dev.to/deepnikode/enhancing-data-security-with-aes-encryption-in-java-95</guid>
      <description>&lt;p&gt;Encryption is a fundamental aspect of Information Security practice to protect sensitive data. There could be several use cases to encrypt data to hide sensitive information like password, card details, contact details, and several other details.🔑&lt;/p&gt;

&lt;p&gt;The AES Algorithm requires a plain-text and a secret key for encryption and the same secret key is required again to decrypt it.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔍 How It Works
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;THEORY&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Encryption: Converts plain text into a secure, encrypted format using the AES algorithm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Decryption: Reverts the encrypted data back to its original form, ensuring data integrity and confidentiality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Key Generation: Uses a hardcoded key for encryption and decryption. This key is critical for both processes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxoi54v91am6khf81ost.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkxoi54v91am6khf81ost.png" alt="AES" width="796" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CODE&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm excited to share a recent code snippet I developed for my Project, demonstrating how to implement AES encryption and decryption in Java. 🌐👨‍💻&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constants:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt; ALGORITHM: Set to "AES", specifying the encryption algorithm.&lt;/li&gt;
&lt;li&gt; KEY: A byte array derived from the string "MySuperSecretKey". This is used as the encryption key.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static final String ALGORITHM = "AES";
private static final byte[] KEY = "deepNikodeCoderr".getBytes(StandardCharsets.UTF_8);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encryption Method (encrypt)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static String encrypt(String data) throws Exception 
{

        Key key = generateKey();


        //Creates a Cipher object for AES.
        Cipher cipher = Cipher.getInstance(ALGORITHM); 


        //Initializes the cipher for encryption.
        cipher.init(Cipher.ENCRYPT_MODE, key);


        //Performs the actual encryption.
        byte[] encryptedValue = cipher.doFinal(data.getBytes());

      //converts encrypted bytes to a Base64 string.
        return java.util.Base64.getEncoder().encodeToString(encryptedValue);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decryption Method (decrypt)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static String decrypt(String encryptedData) throws Exception 
{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(ALGORITHM);

      //Initializes for decryption.
        cipher.init(Cipher.DECRYPT_MODE, key);

      //converts the Base64 string back to bytes.
        byte[] decodedValue = java.util.Base64.getDecoder().decode(encryptedData);

      //Performs the actual decryption.
        byte[] decryptedValue = cipher.doFinal(decodedValue);

        return new String(decryptedValue);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key Generation Method (generateKey)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static Key generateKey() 
{
        return new SecretKeySpec(KEY, ALGORITHM);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this snippet helps you in your projects and encourages more developers to prioritize data security. Feel free to reach out if you have any questions or suggestions!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstjdvtgjqvghu73pohcb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fstjdvtgjqvghu73pohcb.png" alt="My Code" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay secure, and happy coding! 💻✨&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>security</category>
      <category>api</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
