有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用预定义的128位密钥进行Java AES加密和解密

java中的以下代码试图解密QR码中编码的字符串,C#码中加密的字符串。它似乎无法解密字符串。有没有一个简单的方法可以做到这一点

   //string encrypted contains the string of the encoded characters. 

     String encrypted = intent.getStringExtra("SCAN_RESULT");

     //converting the string into a byte array          
     byte[] byteEncrypted = encrypted.getBytes();

     //instantiating the AES cipher object
     Cipher cipher = Cipher.getInstance("AES");

     //Predefined public-key 

     byte[] skey = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,  0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

     //creating a secretKeySpec       
     SecretKeySpec skeyspec = new SecretKeySpec(skey, "AES");

 //initializing the cipher to Decrypt               
     cipher.init(Cipher.DECRYPT_MODE, skeyspec);
  final byte[] decrypt  = cipher.doFinal(byteEncrypted);

 //decrypting the string                
 String contents = new String(decrypt, "UTF-8");

共 (0) 个答案