How to check if a string is encrypted or not?

I use this encryption method to encrypt and decrypt a specific string: -

package encryption;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES {

     private static final String ALGO = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
}

}

It works great.

The problem is that how do I know that the string to be decrypted is encrypted?

I mean, I can pass a long "unencrypted" string to the decryption method, and it will still work.

Any suggestions.

+3
source share
5 answers

; , , , - ( , -, file , "" ). - , " " (, ASCII Unicode $LANGUAGE), .

+3

, - , , , .

, , , , , . , , !

CBC, , (, AES), , . , , , , , - .

, , , , , , , .

, , (MAC) .

:

CIPHERTEXT = ENCRYPT(KEY_1, PADDING, MODE, PLAINTEXT)
MESSAGE = CIPHERTEXT || HMAC(KEY_2, CIPHERTEXT)

CIPHERTEXT ,

MESSAGE CIPHERTEXT, HMAC - (||).

KEY_1 , KEY_2, .

, HMAC CIPHERTEXT, , , , .

KEY_2 , , , 1/1 ^ 256 ( ) , HMAC.

HMAC, , , , KEY_1, .

+3

, 100% , - . base64, , , . base64 - , .

92 , base64. , , .

+1

, , . . , 100% - contrexample, , .

, , . , , ..

, . BASE64 6 ..

+1

, /, , .

, , CRC.

, , .

, .

, , CRC.

BTW, the authentication method is usually a hashing algorithm such as SHA, HMAC, etc. by its nature, CRC will usually be more vulnerable to attacks with the selected ciphertext, where the attacker generates random strings, hoping that some of them will be detected as genuine.

0
source

All Articles