Java AES encryption/decryption always return the same content -


i wrote little test case aes encryption , decryption. plan read text file, encrypt key , decrypt again. problem is, text same, wrong password not result in unreadable text.

where problem in code or did make fundamental mistake ?

main.java

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

crypto.java

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

this problem:

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

you don't use password anywhere within generatesecretkey, it'll create same secret key every time...

if change to:

public secretkeyspec generatesecretkey(string password) throws exception {     messagedigest shahash = messagedigest.getinstance("sha-1");     byte[] key = shahash.digest(password.getbytes("utf-8"));     key = arrays.copyof(key,  16);     return new secretkeyspec(key, "aes"); } 

then fail expected when given wrong password. doesn't mean it's best way of creating secret key, or of rest of crypto code appropriate, don't have enough experience comment on that.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -