【发布时间】:2019-03-06 17:59:40
【问题描述】:
我正在尝试使用 JAVA 进行 AES 加密,我进行了多次尝试,尝试了很多代码并进行了许多更改,最终达到了我的加密文本与使用 C# 代码生成的加密文本匹配但部分匹配的地方。最后一个 32 位块是不同的。我无权访问 C# 代码,因为它是第 3 方服务。谁能指导我错过了什么?
提到的条件是使用:
在 CBC 模式下使用 256 位 AES 加密并使用 PKCS5 填充使用您的主键和初始化向量来加密整个查询字符串。 (不要在查询字符串中包含消息摘要。)主键是 64 位十六进制字符串,初始化向量是 32 位十六进制字符串。
我使用的样本值是:
Aes_IV = 50B666AADBAEDC14C3401E82CD6696D4
Aes_Key = D4612601EDAF9B0852FC0641DC2F273E0F2B9D6E85EBF3833764BF80E09DD89F(我的 KeyMaterial)
Plain_Text = ss=brock&pw=123456&ts=20190304234431(输入)
Encrypted_Text = 7643C7B400B9A6A2AD0FCFC40AC1B11E51A038A32C84E5560D92C0C49B3B7E0 A072AF44AADB62FA66F047EACA5C6A018(输出)
我的输出 = 7643C7B400B9A6A2AD0FCFC40AC1B11E51A038A32C84E5560D92C0C49B3B7E0 A38E71E5C846BAA6C31F996AB05AFD089
public static String encrypt( String keyMaterial, String unencryptedString, String ivString ) {
String encryptedString = "";
Cipher cipher;
try {
byte[] secretKey = hexStrToByteArray( keyMaterial );
SecretKey key = new SecretKeySpec( secretKey, "AES" );
cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
IvParameterSpec iv;
iv = new IvParameterSpec( hexStrToByteArray( ivString ) );
cipher.init( Cipher.ENCRYPT_MODE, key, iv );
byte[] plainText = unencryptedString.getBytes( "UTF-8") ;
byte[] encryptedText = cipher.doFinal( plainText );
encryptedString = URLEncoder.encode(byteArrayToHexString( encryptedText ),"UTF-8");
}
catch( InvalidKeyException | InvalidAlgorithmParameterException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException e ) {
System.out.println( "Exception=" +e.toString() );
}
return encryptedString;
}
我已将其用于转换。
public static byte[] hexStrToByteArray ( String input) {
if (input == null) return null;
if (input.length() == 0) return new byte[0];
if ((input.length() % 2) != 0)
input = input + "0";
byte[] result = new byte[input.length() / 2];
for (int i = 0; i < result.length; i++) {
String byteStr = input.substring(2*i, 2*i+2);
result[i] = (byte) Integer.parseInt("0" + byteStr, 16);
}
return result;
}
public static String byteArrayToHexString(byte[] ba) {
String build = "";
for (int i = 0; i < ba.length; i++) {
build += bytesToHexString(ba[i]);
}
return build;
}
public static String bytesToHexString ( byte bt) {
String hexStr ="0123456789ABCDEF";
char ch[] = new char[2];
int value = (int) bt;
ch[0] = hexStr.charAt((value >> 4) & 0x000F);
ch[1] = hexStr.charAt(value & 0x000F);
String str = new String(ch);
return str;
}
任何建议,我应该怎么做才能匹配输出?
【问题讨论】:
-
当我通过 C# 的 AES 实现运行您的输入时,我得到与您相同的输出(以 ...089 结尾)。由于只有最后一个块(即 16 字节)给您带来问题,我怀疑 C# 服务使用不同的填充。
-
如果消息(36 字节)用 0 到 48 (= 16 * 3) 字节的值填充,则参考值
7643C7B400B9A6A2AD0FCFC40AC1B11E51A038A32C84E5560D92C0C49B3B7E0A072AF44AADB62FA66F047EACA5C6A018是可重现的。但是,这不是@RogerN 已经怀疑的PKCS5Padding。因此,必须验证 C# 端使用的填充。纯零字节填充并不可靠(例如,请参阅 crypto-it.net/eng/theory/padding.html)。
标签: java c# encryption aes tripledes