public class AESCCMEncryption {
public static int AES_KEY_SIZE = 128 ;
public static int TAG_BIT_LENGTH = 128 ;
public static String ALGO_TRANSFORMATION_STRING = "AES/CCM/PKCS5Padding" ;
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
SecretKey aesKey = null ;
String message="messageToEncrypt";
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES") ;
keygen.init(AES_KEY_SIZE) ;
aesKey = keygen.generateKey() ;
} catch(NoSuchAlgorithmException noSuchAlgoExc) { System.out.println("Key being request is for AES algorithm, but this cryptographic algorithm is not available in the environment " + noSuchAlgoExc) ; System.exit(1) ; }
byte[] encryptedText = aesEncrypt(message, aesKey) ;
byte[] decryptedText = aesDecrypt(encryptedText, aesKey) ;
System.out.println("Decrypted text " + new String(decryptedText)) ;
}
public static byte[] aesEncrypt(String message, SecretKey aesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher c = null ;
c = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
c.init(Cipher.ENCRYPT_MODE, aesKey) ;
byte[] cipherTextInByteArr = null ;
cipherTextInByteArr = c.doFinal(message.getBytes()) ;
return cipherTextInByteArr ;
}
public static byte[] aesDecrypt(byte[] encryptedMessage, SecretKey aesKey) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
Cipher c = null ;
c = Cipher.getInstance(ALGO_TRANSFORMATION_STRING); // Transformation specifies algortihm, mode of operation and padding
c.init(Cipher.DECRYPT_MODE, aesKey) ;
byte[] plainTextInByteArr = null ;
plainTextInByteArr = c.doFinal(encryptedMessage) ;
return plainTextInByteArr ;
}
}
我收到不支持的例外,我現在用的Java 1.8版本 如果要做加密我錯了可以幫助我如何實現「AES/CCM/PKCS5Padding」 是需要添加IV矢量規範加密我試圖「AES/CCM/PKCS5Padding」在Java加密,但我得到了一些例外任何一個可以幫助我如何使用CCM代碼
您需要找到支持CCM模式的安全提供程序。默認的Sun JCE不會那樣做。 –