文件名称:C#-DES_SAME_JAVA-DES.zip
文件大小:3.93MB
文件格式:ZIP
更新时间:2023-06-10 09:02:48
C#DES加密 JAVA DES加密与C#互通 C#加密 JAVA
本代码是C#方法,通过开源C#BouncyCastle加密组件进行DES加解密。和JAVA DES加解密互通。JAVA方法如下: public static String desEncrypt(String source, String desKey) throws Exception { try { // 从原始密匙数据创建DESKeySpec对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(new DESKeySpec(desKey.getBytes())); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey); // 现在,获取数据并加密 byte[] destBytes = cipher.doFinal(source.getBytes()); StringBuilder hexRetSB = new StringBuilder(); for (byte b : destBytes) { String hexString = Integer.toHexString(0x00ff & b); hexRetSB.append(hexString.length() == 1 ? 0 : "").append(hexString); } return hexRetSB.toString(); } catch (Exception e) { throw new Exception("DES加密发生错误", e); } } public static String desDecrypt(String source, String desKey) throws Exception { // 解密数据 byte[] sourceBytes = new byte[source.length() / 2]; for (int i = 0; i < sourceBytes.length; i++) { sourceBytes[i] = (byte) Integer.parseInt(source.substring(i * 2, i * 2 + 2), 16); } try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(new DESKeySpec(desKey.getBytes())); Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey); // 现在,获取数据并解密 byte[] destBytes = cipher.doFinal(sourceBytes); return new String(destBytes); } catch (Exception e) { throw new Exception("DES解密发生错误", e); } }
【文件预览】:
DES_SAME_JAVA
----DES_SAME_JAVA()
--------bin()
--------obj()
--------DesWithBouncy.cs(3KB)
--------Properties()
--------Program.cs(648B)
--------BouncyCastle.cs(4.33MB)
--------App.config(189B)
--------DES_SAME_JAVA.csproj(3KB)
----.vs()
--------DES_SAME_JAVA()
----DES_SAME_JAVA.sln(1006B)