Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

关于java加密与node之间的转换

$
0
0

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 "";
}

}


Viewing all articles
Browse latest Browse all 14821

Trending Articles