简体   繁体   English

使用AES / ECB / NoPadding和PKCS5Padding进行加密

[英]encryption using AES/ECB/NoPadding vs PKCS5Padding

I don't have problem with not working code but it's confusing and I don't know why even though I use 16byte long string (so padding is not needed) I've got weird short output cG+etVq+7l+RfJS27jCtwg== (without padding but before encription it was 16byte long) 我没有无法正常工作的代码的问题,但是这很令人困惑,我也不知道为什么即使我使用16字节长的字符串(所以不需要填充)我也得到了奇怪的短输出cG + etVq + 7l + RfJS27jCtwg == (无填充,但在加密之前为16字节长)

vs VS

cG+etVq+7l+RfJS27jCtwskFauqkVxpbMJGODZoZe5c= (with PKCS5Padding, yet String is the same ) So why? cG + etVq + 7l + RfJS27jCtwskFauqkVxpbMJGODZoZe5c =(使用PKCS5Padding,但String相同)为什么呢?

public class AES {

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{

        key = myKey.getBytes("UTF-8");
        secretKey = new SecretKeySpec(key, "AES");


}

public static String encrypt(String strToEncrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }
    catch (Exception e)
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

public static String decrypt(String strToDecrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
    catch (Exception e)
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

public static SecretKeySpec getSecretKey() {
    return secretKey;
}

public static void setSecretKey(SecretKeySpec secretKey) {
    AES.secretKey = secretKey;
}

public static byte[] getKey() {
    return key;
}

public static void setKey(byte[] key) {
    AES.key = key;
}

} }

With PKCS5Padding , then padding is always added. 使用PKCS5Padding ,则总是添加填充。 There is no string where padding is not needed. 没有不需要填充的字符串。 A 16 byte string will produce a 32 byte output (the next multiple of 16). 16字节的字符串将产生32字节的输出(16的下一个倍数)。

See Padding on Wikipedia for example: 例如,请参阅Wikipedia上的填充

If the original data is an integer multiple of N bytes, then an extra block of bytes with value N is added. 如果原始数据是N个字节的整数倍,那么将添加一个额外的字节块,其值为N。 This is necessary so the deciphering algorithm can determine with certainty whether the last byte of the last block is a pad byte indicating the number of padding bytes added or part of the plaintext message. 这是必需的,以便解密算法可以确定地确定最后一个块的最后一个字节是填充字节,该填充字节指示添加的填充字节数或明文消息的一部分。

Consider a plaintext message that is an integer multiple of N bytes with the last byte of plaintext being 01. With no additional information, the deciphering algorithm will not be able to determine whether the last byte is a plaintext byte or a pad byte. 考虑一个纯文本消息,该消息是N字节的整数倍,而纯文本的最后一个字节为01。如果没有其他信息,解密算法将无法确定最后一个字节是纯文本字节还是填充字节。 However, by adding N bytes each of value N after the 01 plaintext byte, the deciphering algorithm can always treat the last byte as a pad byte and strip the appropriate number of pad bytes off the end of the ciphertext; 但是,通过在01个明文字节之后添加N个字节,每个字节的值N,解密算法可以始终将最后一个字节视为填充字节,并从密文末尾剥离适当数量的填充字节; said number of bytes to be stripped based on the value of the last byte. 表示要基于最后一个字节的值剥离的字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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