关于C#和JAVA的AES加密CBC模式下,加密出来的结果不相同,求帮助
我是做C#开发的,对JAVA不太懂,附上JAVA加密的源代码:publicstaticStringencrypt(StringsSrc,StringsKey)throws...
我是做C#开发的,对JAVA不太懂,附上JAVA加密的源代码:
public static String encrypt(String sSrc, String sKey) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException {
byte[] raw = sKey.getBytes(charsetName);
SecretKeySpec skeySpec = new SecretKeySpec(raw, aes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes(charsetName));
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(charsetName));
return new String(Base64.encode(encrypted), charsetName);
}
C#使用的CBC模式PKCS7方式加密,结果进行ToBase64String转码,但是加密结果不同,因为字数超限制,这里没有放出C#加密的源码 展开
public static String encrypt(String sSrc, String sKey) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException,
UnsupportedEncodingException {
byte[] raw = sKey.getBytes(charsetName);
SecretKeySpec skeySpec = new SecretKeySpec(raw, aes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes(charsetName));
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(charsetName));
return new String(Base64.encode(encrypted), charsetName);
}
C#使用的CBC模式PKCS7方式加密,结果进行ToBase64String转码,但是加密结果不同,因为字数超限制,这里没有放出C#加密的源码 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询