0

I'm completely new with encryption stuff and i have some questions about encryption in java I use to do this for RSA encryption in java

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPTION_MODE,publicKey);
byte result = cipher.doFinal(data);

and same way to use AES and I use this code to generate a AES key

SecureRandom random = new SecureRandom();
byte [] key = new byte [16];
random.nextByte(key);
SecretKeySpec secretKey = new SecretKeySpec(key,"AES");

but as i saw in other programs code this is not how they use encryption i always see they use something as IV param in AES and they never use "AES" or "RSA" to get a cipher instance. Is the way i using to encrypt data safe? I'm sure that i missing something

UPDATE:

I also have a question about changing data size in AES encryption in the way that i use to encrypt data with AES it changes the data size from 1024 to 1040

    byte key [] = new byte[16];
    SecureRandom random = new SecureRandom();
    random.nextBytes(key);
    SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE,keySpec);
    FileInputStream in = new FileInputStream("test.txt");
    byte [] buffer = new byte[1024];
    byte [] encrypted;
    while (in.read()>0){
        encrypted = c.doFinal(buffer);
        System.out.println(encrypted.length);
    }

the out put is: 1040 1040 . . 1040

Simply encrypted data size is always 16 bytes more than original data Do i have to deal with this or it's because i using Cipher.getInstance("AES");

pest mail
  • 7
  • 4
  • 1
    It's my opinion that questions this basic, or *tutorial*, are too broad. But since they sometimes attract good answers I'll hold off on a close vote. There are many excellent resources online that you can use to get more up to speed. – President James K. Polk Dec 20 '17 at 15:50
  • Spring Security – secondbreakfast Dec 20 '17 at 15:53
  • 2
    This *is* too broad. Cryptography is a complicated issue, and you have to understand it before you attempt to program it, at least if you're hoping to stay secure. For example, you need to understand what [IV](https://en.wikipedia.org/wiki/Initialization_vector) is and why it's used. Less coding for a while and more reading. – Kayaman Dec 20 '17 at 15:54
  • **In general**: Data is encrtypted with AES and keys are encrypted with RSA. AES has one key for both encrtyption and decryption, is fast and has no data size limitation. RSA has two key (one for encryption and another for decryption), is very slow and has a data size limitation to less than the key size. – zaph Dec 20 '17 at 21:08

2 Answers2

2

This is not the recommended way and you will need to change it. You may want to have a better look on StackOverflow. Your question is (in)directly answered in this post How to encrypt String in Java.

Make sure you look further down to all the answers. For example this one will probably help you to understand more.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
smos
  • 148
  • 1
  • 6
  • I've updated my question and a answer about RSA encryption will be great since there is nothing about RSA in https://stackoverflow.com/questions/1205135/how-to-encrypt-string-in-java] – pest mail Dec 20 '17 at 16:48
0

The most simple is to use the below class

package com.toptal.gif_downloader.tools;
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 CipherDecrypt {

  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();
    }
  }

  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;
  }
}
Malek Tubaisaht
  • 1,170
  • 14
  • 16
  • **Security warning**: the above code is using SHA-1 for "key derivation" and ECB mode for encryption, both are unsecure in most use cases. – Michael Fehr Oct 16 '22 at 00:15