关于java加密与node之间的转换
发布于 8 年前 作者 mac-deng 3690 次浏览 来自 问答

java代码,求大神指教,如何翻译成node package single; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class DesUtil { private final static String secretKey = “test”; private final static String iv = “01234567”; private final static String encoding = “utf-8”;

public static String encode(String plainText) {
	Key deskey = null;
	byte[] encryptData = null;
	try {
		DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
		deskey = keyfactory.generateSecret(spec);

		Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
		IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
		cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
		encryptData = cipher.doFinal(plainText.getBytes(encoding));
		
		BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encode(encryptData);
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return "";
}

}

1 回复

请问你解决了吗,我也遇到加密问题了

回到顶部