论坛首页 Java企业应用论坛

Java用des加密后用 linux c语言解密 怎么互通

浏览 3279 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-03-26   最后修改:2014-03-26
问题是这样的,我用java des加密 然后,c来解密,但是我加密出来的数据,是字节数组,然后我转换成16进制,c程序也编程16进制输出,两个16进制密文的数不一样,密钥都一样。愁死了,
下面是java的代码,那位哥哥给弄个c代码最好
package one;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class LearnDes {
	private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    public static void main(String[] args) {
        try {
            System.out.println(encrypt("12345678", "0000000000"));

            System.out.println(decrypt("858b176da8b12503", "0000000000"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String encrypt(String message, String key) throws Exception {
      // Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");

        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes());
        //cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey );

        return bytesToHexStr(cipher.doFinal(message.getBytes()));
    }

    public static String decrypt(String message, String key) throws Exception {

        byte[] bytesrc = hexStrToBytes(message);

        //Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        Cipher cipher = Cipher.getInstance("DES/ECB/NOPADDING");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

        //cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey );

        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }
    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }
    /**
     * Transform the specified byte into a Hex String form.
     */
    public static final String bytesToHexStr(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);

        for (int i = 0; i < bcd.length; i++) {
            s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
            s.append(bcdLookup[bcd[i] & 0x0f]);
        }

        return s.toString();
    }

    /**
     * Transform the specified Hex String into a byte array.
     */
    public static final byte[] hexStrToBytes(String s) {
        byte[] bytes;

        bytes = new byte[s.length() / 2];

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
        }

        return bytes;
    }
}
   发表时间:2014-04-01  
http://snowolf.iteye.com/blog/380034
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics