Why is the ciphertext 32 bytes long when encrypting 16 bytes with AES?

前端 未结 3 1984
你的背包
你的背包 2020-12-29 10:39

I use encryption AES algorithm, when i encrypt 16 byte(one block) the result is 32 byte. Is this ok?

My source code that i used is:

package net.sf.an         


        
3条回答
  •  清酒与你
    2020-12-29 11:25

    package com.cipher;
    
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Encrypt {
    
        public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
            // TODO Auto-generated method stub
    String s="You are doing encryption at deep level";
    SecureRandom sr=SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(s.getBytes());
    byte[] k=new byte[128/8];
    sr.nextBytes(k);
    SecretKeySpec spec=new SecretKeySpec(k,"AES");
    byte[] iv={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    IvParameterSpec ivs=new IvParameterSpec(iv);
    Cipher cps=Cipher.getInstance("AES/CBC/PKCS5Padding");
    cps.init(Cipher.ENCRYPT_MODE,spec,ivs);
    byte[] iv2=cps.doFinal(s.getBytes());
    System.out.println("En"+iv2);
    Cipher cpr=Cipher.getInstance("AES/CBC/PKCS5Padding");
    cpr.init(Cipher.DECRYPT_MODE, spec,ivs);
    byte[] iv3=cpr.doFinal(iv2);
    String ds=new String(iv3);
    System.out.println(ds);
    
    
        }
    
    }
    

提交回复
热议问题