title: Java AES encryption/decryption always return the same content
body: <p>I wrote a little test case for an AES encryption and decryption. The plan is to read some text from a file, encrypt it with a key and decrypt it again. Now the problem is, that the text is always the same, a wrong password does not result in unreadable text.</p>
  
  <p>Where is the problem in the code or did I make a fundamental mistake ? </p>
  
  <p>Main.java</p>
  
  <pre><code>import javax.crypto.spec.SecretKeySpec;
  
  public class Main {
      public static void main(String[] args) throws Exception {
          new Main();
      }
  
      public Main() throws Exception {
          Reader reader = new Reader();
          String text = reader.readFile("/home/benjamin/Test.txt");
          System.out.println("Original text before encryption: " + text);
  
          // User A verschlüsselt und speichert ab
          Crypto crypto = new Crypto();
          SecretKeySpec secretkey = crypto.generateSecretKey("123456aA");
          byte[] encryptedtext = crypto.encrypt(text, secretkey);
  
          // User B lädt Datei und kennt das Passwort
          Crypto crypto2 = new Crypto();
          SecretKeySpec secretkey2 = crypto2.generateSecretKey("1kkk23456aAjbhhjbhjb");
          byte[] decryptedtext = crypto2.decrypt(encryptedtext, secretkey2);
          System.out.println("Original text after encryption: " + new String(decryptedtext, "UTF-8"));
      }
  }
  </code></pre>
  
  <p>Crypto.java</p>
  
  <pre><code>import java.security.MessageDigest;
  import java.util.Arrays;
  
  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
  
  
  public class Crypto {
      public SecretKeySpec generateSecretKey(String password) throws Exception {
          MessageDigest shahash = MessageDigest.getInstance("SHA-1");
          byte[] key = shahash.digest();
          key = Arrays.copyOf(key,  16);
          return new SecretKeySpec(key, "AES");
      }
  
      public byte[] encrypt(String text, SecretKeySpec secretkey) throws Exception {
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, secretkey);
          return cipher.doFinal(text.getBytes());
      }
  
      public byte[] decrypt(byte[] encryptedtext, SecretKeySpec secretkey) throws Exception {
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, secretkey);
          return cipher.doFinal(encryptedtext);
      }
  }
  </code></pre>