代码如下:
//$input stuff to decrypt
//$key the secret key to use
function do_mencrypt($input $key)
{
$input = str_replace(""n" "" $input)
$input = str_replace(""t" "" $input)
$input = str_replace(""r" "" $input)
$key = substr(md ($key) )
$td = mcrypt_module_open( tripledes ecb )
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td) MCRYPT_RAND)
mcrypt_generic_init($td $key $iv)
$encrypted_data = mcrypt_generic($td $input)
mcrypt_generic_deinit($td)
mcrypt_module_close($td)
return trim(chop(base _encode($encrypted_data)))
}
//$input stuff to decrypt
//$key the secret key to use
function do_mdecrypt($input $key)
{
$input = str_replace(""n" "" $input)
$input = str_replace(""t" "" $input)
$input = str_replace(""r" "" $input)
$input = trim(chop(base _decode($input)))
$td = mcrypt_module_open( tripledes ecb )
$key = substr(md ($key) )
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td) MCRYPT_RAND)
mcrypt_generic_init($td $key $iv)
$decrypted_data = mdecrypt_generic($td $input)
mcrypt_generic_deinit($td)
mcrypt_module_close($td)
return trim(chop($decrypted_data))
lishixinzhi/Article/program/PHP/201311/21012Solaris下的系统,有一个用C做的加密工具,调用Sunwcry的des(1)对文件进行加密,然后在java中对文件进行解密。java中用的是标准的DES/CBC/NoPadding算法,可是解密后发现开头有8byte的数据出错了,请高人指点一下。
cbc_encrypt.c : 加密用的C程序
cbc_decrypt.c:解密用的C程序
TestDescbc.java:解密用的java程序
Test01.dat原始文件
Test03.dat cbc_encrypt加密后的文件
Test05.dat cbc_decrypt解密后的文件
Test06.dat TestDescbc解密后的文件
/** @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2)
for (int i = 0i <iLeni++) {
int intTmp = arrB[i]
// 把负数转换为正数
while (intTmp <0) {
intTmp = intTmp + 256
}
// 小于0F的数需要在前面补0
if (intTmp <16) {
sb.append("0")
}
sb.append(Integer.toString(intTmp, 16))
}
return sb.toString()
}
/*
* @param strIn 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes()
int iLen = arrB.length
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2]
for (int i = 0i <iLeni = i + 2) {
String strTmp = new String(arrB, i, 2)
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16)
}
return arrOut
}
/**
* 加密字节数组
*
* @param arrB
*需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
@SuppressWarnings("restriction")
private static byte[] encrypt(byte[] arrB,String keyParameter) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE())
Key key = getKey(keyParameter.getBytes())
Cipher encryptCipher = Cipher.getInstance("DES")
encryptCipher.init(Cipher.ENCRYPT_MODE, key)
return encryptCipher.doFinal(arrB)
}
/**
* 加密字符串
*
* @param strIn
*需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public static String encrypt(String strIn,String keyParameter) throws Exception {
return HexStrByteArrUtils.byteArr2HexStr(encrypt(strIn.getBytes(PiccConfig.PICC_INPUT_CHARSET),keyParameter))
}
/**
* 解密字节数组
*
* @param arrB
*需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
@SuppressWarnings("restriction")
private static byte[] decrypt(byte[] arrB,String keyParameter) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE())
Key key = getKey(keyParameter.getBytes())
Cipher decryptCipher = Cipher.getInstance("DES")
decryptCipher.init(Cipher.DECRYPT_MODE, key)
return decryptCipher.doFinal(arrB)
}
/**
* 解密字符串
*
* @param strIn
*需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public static String decrypt(String strIn,String keyParameter) throws Exception {
return new String(decrypt(HexStrByteArrUtils.hexStr2ByteArr(strIn),keyParameter),PiccConfig.PICC_INPUT_CHARSET)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)