简体   繁体   中英

Encryption using AES 256 and SHA-2

I have a situation where I need to first encrypt a message using a public key and vector, that is already provided. Also as per requirement I need to use SHA-2 as well. For now, I am assuming that I need to hash the encrypted message and then send to the server. I have two questions related to this 1. Is it wise to hash the encrypted message? Also, will sending the encrypted message and hashed value to the server be a good idea? 2. I have done a lot search on internet, but whenever I try to get some example of using AES 256 and SHA-2 together, I actually land up where the difference between the two is explained. Can any help me with some sample code?

Thanks in Advance!!!

Let's break down the stuff first.


Public Key Cryptography

Allows a given pair ( Kpriv , Kpub ) to be used on a cipher to encrypt and decrypt data. Any data encrypted with Kpriv can only be decrypted with Kpub and any data encrypted with Kpub can only be decrypted with Kpriv .

A nice and well known example of a public key cipher is RSA .

Asymmetric cryptography requires extremely large keys in order to be secure, such that it's extremely slow to execute! You should never encrypt large amount of data with Asymetric keys cryptography. You can use it in the beginning of a connecition to exchange a symetric key Ks , though.


Symetric Key Cryptography

Allows a Ks to be used on a cipher to encrypt and decrypt data.

An example of a symetric cipher is AES . AES is in fact so versatile you can change lots of parameters, such as, (as you mention) the Blocksize which can be of 128, 192 or 256 bits. AES256 is the AES cipher configured with a blocksize of 256 bits.

The block size is what's used against the provided Ks to perform the actual encryption. Note that your data can be larger than the block size (the algorithm will still work, It'l simply reuse the same Ks ). Simply reusing the key every block is known as ECB mode and can reveal patterns if your data is repetitive. An alternative is to use modes like CBC or CTR which rely on also using previous block data and XORing with the next block data, to eliminate such patterns. What mode should you use depends on your data.

Note that, according to your cipher mode, you eventually will need padding . I'm assuming you are already quite familiar with this terms when you asked that question.


Guarantees By Cryptography

Cryptography does guarantee that the encrypted data is confidential but that's just it. It does not give any other guarantees such as whether the data is authentic or whether it has been tampered with or not.

While tampering data will most likely result in unintelligible text even after decryption, in cryptography, there's no such thing as invalid plaintext. As such, you need some mechanism to know if your data is valid or not.

A secure hash algorithm such as SHA can help you know whether your decrypted data is valid or not.

However, for these purposes, you usually shouldn't directly use a Digest algorithm. Try to instead use a MAC . That MAC can use SHA256 algorithm but MAC's and Hashes are not exactly the same.


How To Do It In Practice

If all you want is confidentiality and tampering detection, you would use the cipher and digest (or hash) algorithm as such:

E ks ( SHA(data) || data )

Where E is a symmetric cipher, ks is the shared symmetric key, SHA(data) is the digest of data using a secure hash algorithm, || means concatenation and data is a byte array.

A more safer approach would be: E ks ( MAC mk(data) || data )

Where mk is the MAC's secret key.

Now just search how to "java symetric cipher" and "java hash byte array" and use the two as I'm describing above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM