-5

I want an implementation of AES that takes as parameters two String inputs (plaintext, key) and outputs the encrypted String ciphertext. I have search all over the Internet but I haven't found. I have found AES that has byte[] input and outputs String and the other way round. Is it already implemented or I have to play aroundwith Java and create some methods that will finally do the job? Thank you!

Meris
  • 3
  • This may help http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption – sbowde4 May 20 '16 at 20:46
  • You are asking the wrong question. The question you should be asking is "*Why* is it that encryption always seems to spit out byte[] ciphertext, not String ciphertext?" You should be able to find the answer to that question yourself, and when you understand the answer you will understand encryption better. – zwol May 20 '16 at 20:47
  • Note also that *asking this question at all* suggests you're trying to reinvent a wheel. Are you **sure** you need to work with AES directly, and not, say, TLS? – zwol May 20 '16 at 20:48
  • As stated above, there is a reason why AES usually don't get String as an input. If you really want it, then look at a version using byte[] and make the conversion. @zwol I don't exactly see your point. AES and TLS are two **very** different things, and I see no reason to mistakenly use AES instead of TLS. – T. Claverie May 20 '16 at 21:02
  • Possible duplicate of [Simple java AES encrypt/decrypt example](http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example) – Artjom B. May 21 '16 at 08:52
  • Questions asking us to recommend or find a book, tool, ***software library***, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Yassin Hajaj May 22 '16 at 16:41
  • @T.Claverie My point was exactly that they are two very different things. Using a cryptographic *primitive* (e.g. AES) directly, as opposed to using a cryptographic *protocol* (e.g. TLS) that has already addressed all of the next-level-up design issues, is almost always a mistake. – zwol May 23 '16 at 09:52

1 Answers1

2

AES encryption produces bytes, which is why you always get bytes out. What you could use is a separate process to convert the resulting byte array into a string. The usual way to do this is to use Base64. See the java.util.Base64 class.

Remember to convert your Base64 string back into a byte array before decrypting. If you don't then the decryption will fail.

Encryption: plaintext -> encrypt -> bytes -> Base64 string

Decryption: Base64 string -> bytes -> decrypt -> decrypted plaintext

rossum
  • 15,344
  • 1
  • 24
  • 38