求将下面java des加密代码翻译成delphi或C
delphi吧
全部回复
仅看楼主
level 1
弋孤秋 楼主
package des;import java.io.IOException;
import java.security.SecureRandom;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class testDecrypt {
public String info( String data )
{
String key = "123qazwsx";
String dt="";
System.out.print("原文:" + data);
try {
dt = encrypt(data, key);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.print("加密后:" + dt);
try {
System.out.print("解密后:" + decrypt(dt, key));
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return dt;
}
public static String encrypt(String data, String key)
throws Exception
{
byte[] bt = encrypt(data.getBytes(), key.getBytes());
String strs = new BASE64Encoder().encode(bt);
return strs;
}
public static String decrypt(String data, String key)
throws IOException, Exception
{
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf, key.getBytes());
return new String(bt);
}
private static byte[] encrypt(byte[] data, byte[] key)
throws Exception
{
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(1, securekey, sr);
return cipher.doFinal(data);
}
private static byte[] decrypt(byte[] data, byte[] key)
throws Exception
{
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(2, securekey, sr);
return cipher.doFinal(data);
}
public static void main(String[] args) {
testDecrypt nc = new testDecrypt();
String src="123456789";
nc.info(src);
}
}
2017年06月13日 08点06分 1
level 12
翻了也没用,JAVA库又没有对应的DELPHI单元
要想DES加密的话直接去搜DES算法
2017年06月15日 03点06分 2
吧务
level 14
des,aes都有Delphi源代码。不过des的加密强度太弱了,速度还慢。
2017年06月18日 14点06分 3
找到了一些代码,但是delphi加密的结果总和java解密出来的不一样[心碎]
2017年06月22日 08点06分
@弋孤秋 测试数据是什么类型的?要用二进制数据做对比测试
2017年06月22日 10点06分
@BambooCaep 哎呀,确实是,从二进制源头找原因,已经整出来了[太开心]
2017年06月28日 13点06分
des有多弱?
2017年06月29日 20点06分
1