node
npm i crypto
import * as crypto from 'crypto'
/**
* rsa 256签名
* @param data
*/
export function createSign(key,data) {
const sign = crypto.createSign('RSA-SHA256');
sign.update(Buffer.from(data, 'utf-8'));
const signature = sign.sign(key, 'base64');
return signature
}
/**
* 3des加密
* @param symmetricKey
* @param data
*/
export function encryptDESede(symmetricKey: Buffer, data: any) {
// 3des-ecb -> des-ede3
const cipher = crypto.createCipheriv(
'des-ede3',
symmetricKey,
Buffer.alloc(0)
)
return cipher.update(data, 'utf8', 'base64') + cipher.final('base64')
}
/**
* 3des解密
* @param symmetricKey
* @param data
*/
export function decryptDESede(symmetricKey: Buffer, data: any) {
const decipher = crypto.createDecipheriv(
'des-ede3',
symmetricKey,
Buffer.alloc(0)
)
return decipher.update(data, 'base64', 'utf8') +decipher.final('utf8')
}
java
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.9</version>
</dependency>
/**
* DESede 加密
* @param text 加密内容
* @param symmetricKey 加密所用的key
* @return
*/
public static String encryption(String text,String symmetricKey){
byte[] bytes = HexUtil.decodeHex(symmetricKey);
return SecureUtil.desede(bytes).encryptBase64(text);
}
// 解密
public static String getDecryptedValue(String value, String key) throws Exception {
if (null == value || "".equals(value)) {
return "";
}
byte[] valueByte = org.apache.commons.codec.binary.Base64.decodeBase64(value);
byte[] sl = decrypt3DES(valueByte, HexUtil.decodeHex(key));
String result = new String(sl);
return result;
}
public static byte[] decrypt3DES(byte[] input, byte[] key) throws Exception {
Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "DESede"));
return c.doFinal(input);
}
版权声明:本文为qq_38152423原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。