Java C# DES加密结果不一致
这是我的代码:privatestaticbyte[]DESIV1=newbyte[]{45,88,46,97,78,17,17,16};publicstaticStrin...
这是我的代码:
private static byte[] DESIV1 = new byte[] { 45, 88, 46, 97, 78, 17, 17, 16 }; public static String enCrypto(String key, String txt) throws InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException ; byte[] arr=txt.getBytes("gb2312"); IvParameterSpec zeroIv = new IvParameterSpec(DESIV1); AlgorithmParameterSpec paramSpec = zeroIv; StringBuffer sb = new StringBuffer(); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("gb2312")); SecretKeyFactory skeyFactory = null; Cipher cipher = null; try { skeyFactory = SecretKeyFactory.getInstance("DES"); cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } Key deskey = skeyFactory.generateSecret(desKeySpec); cipher.init(Cipher.ENCRYPT_MODE, deskey,paramSpec); byte[] cipherText = cipher.doFinal(txt.getBytes("gb2312")); for (int n = 0; n < cipherText.length; n++) { String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF)); if (stmp.length() == 1) { sb.append("0" + stmp); } else { sb.append(stmp); } } return sb.toString().toUpperCase(); }
下面是经理写的C#算法,为什么我们加密出来的内容都不一样呢?
为什么加密出来的东西都不一样呢?C#代码我又不能改。 展开
private static byte[] DESIV1 = new byte[] { 45, 88, 46, 97, 78, 17, 17, 16 }; public static String enCrypto(String key, String txt) throws InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException ; byte[] arr=txt.getBytes("gb2312"); IvParameterSpec zeroIv = new IvParameterSpec(DESIV1); AlgorithmParameterSpec paramSpec = zeroIv; StringBuffer sb = new StringBuffer(); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("gb2312")); SecretKeyFactory skeyFactory = null; Cipher cipher = null; try { skeyFactory = SecretKeyFactory.getInstance("DES"); cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } Key deskey = skeyFactory.generateSecret(desKeySpec); cipher.init(Cipher.ENCRYPT_MODE, deskey,paramSpec); byte[] cipherText = cipher.doFinal(txt.getBytes("gb2312")); for (int n = 0; n < cipherText.length; n++) { String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF)); if (stmp.length() == 1) { sb.append("0" + stmp); } else { sb.append(stmp); } } return sb.toString().toUpperCase(); }
下面是经理写的C#算法,为什么我们加密出来的内容都不一样呢?
为什么加密出来的东西都不一样呢?C#代码我又不能改。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询