#include <openssl/md5.h>
unsigned char *MD5(const unsigned char *d, unsigned long n,unsigned char *md)
int MD5_Init(MD5_CTX *c)
int MD5_Update(MD5_CTX *c, const void *data,unsigned long len)
int MD5_Final(unsigned char *md, MD5_CTX *c)
只有写的不好的程序,还没听说过报异常的算法,楼主真乃神人也!你搞清楚UNIX/Linux同Windows的在处理文件上的区别了吗?之前我也一度写过这类东西,Windows的换行同Linux的是不同的,所以如果用终端输入去测试的话,很可能会有考虑不周的地方,试着用同一个字符串变量去测试,就能知道是否是算法的问题了。
// C 语言 DES用的是 ECB模式, 没有填充// 因此Java端要对应, 你的明文是 liubiao 吗?
// 另外 DES已经不安全了, 如果可以改为 3DES或者 AES吧。
public class LearnDes {
public static void main(String[] args) {
try {
System.out.println(encrypt("liubiao", "12345678"))
System.out.println(decrypt("26d086be3a3a62fc", "12345678"))
} 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("UTF-8"))
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES")
SecretKey secretKey = keyFactory.generateSecret(desKeySpec)
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"))
//cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv)
cipher.init(Cipher.ENCRYPT_MODE, secretKey )
return toHexString(cipher.doFinal(message.getBytes("UTF-8")))
}
public static String decrypt(String message, String key) throws Exception {
byte[] bytesrc = convertHexString(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 = 0i <digest.lengthi++) {
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 = 0i <b.lengthi++) {
String plainText = Integer.toHexString(0xff &b[i])
if (plainText.length() <2)
plainText = "0" + plainText
hexString.append(plainText)
}
return hexString.toString()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)