2017-08-01 25 views
2
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代码

+3

您需要找到支持CCM模式的安全提供程序。默认的Sun JCE不会那样做。 –

回答

2

我知道这是迟到,对不起 - 我一直在研究如何做AES/CCM我自己。

无论如何,bouncycastle API支持CCM。如果您还没有它,添加它非常简单,因为它只是一个.jar文件。您可以前往java下载页面here以获取.jar。

 Provider[] providers = Security.getProviders(); 
     for (int i = 0; i < providers.length; i++){ 
      Log.e("Provider", "Name: " + providers[i].getName() + " Version: " + providers[i].getVersion()); 
     } 

我得到下面的输出:

但是,您可以通过运行下面的代码中看到您的当前安全提供

enter image description here

快速搜索后,它的已经相当明显,但“BC版本:1.52”是充气城堡。

我还发现了一个示例.pdf,其中指出“PKCS7Padding通常也被称为PKCS5Padding”。我没有使用填充,所以我不得不让你做这方面的研究。你可以找到那篇文章here。该报价在第17页,但您可以使用CTRL-F更快地找到它,然后粘贴到PKCS5Padding中。虽然只有CBC和EBC的例子。

作为便笺,您可以在this页面上看到支持PKCS。

我希望这有助于!

+0

是的,JCE使用名称PKCS5Padding来表示真正的PKCS7(大多数情况下),但是CCM根本不使用任何填充。使用BC确实是一个答案,可能是最好的答案,尽管可以用CTR + CBCMAC构建CCM并做一些工作。 –

1

由于全球安全问题,默认的JRE不包括对更高级加密的支持。您必须下载“无限安全”补丁 - 当前链接是http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

下载该补丁并按照其中包含的README.TXT文件中的说明进行操作。

只有位于“制裁”国家的客户才可以下载该补丁。

当然没有机会使用VPN来避开该限制,或者该补丁可通过文件共享软件获得。

+0

甲骨文(以前称为Sun Java的无限制政策)只影响超过128位的密钥大小/优势,与此问题无关,尽管它对于其他问题是一个很好的答案。 –

相关问题