Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用预定义的128位密钥进行Java AES加密和解密_Java_Aes - Fatal编程技术网

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

使用预定义的128位密钥进行Java AES加密和解密,java,aes,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

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");

对于初学者来说,通常不能转换存储为文本的密码文本,而通过调用
getBytes()
将其直接转换为字节

AES密码文本包含值为0到255的字节;我知道没有将所有256个值映射到一个字符的字符集编码,即使有一个字符,也不太可能是您的平台默认编码,并且您没有在文本到字节的转换中指定它

密文最常见的字节到文本转换是Base-64编码。如果这就是您在这里使用的,那么您必须找到或编写一个base-64解码实用程序

在创建
密码
实例时,还应指定完整的转换;否则,将使用特定于提供程序的默认值,这可能与发件人的选择不匹配

由于您没有显示任何静脉注射,您可能使用ECB作为模式。对于大多数消息,这是不安全的。只有当您的消息是一个大的随机数(如会话标识符)时,它才是安全的

最有可能的填充是PKCS#5填充(在.NET中称为PKCS7Padding),但您可能没有填充,或者使用一些自制的填充算法

假设ECB和PKCS#5填充,密码创建应如下所示:

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

了解C#code是如何进行加密的,并将结果编码为QR码。没有这一点,只有猜测。我删除了公钥加密标签,因为我没有看到任何证据表明这个问题与公钥加密有关,尽管代码中有注释。