HarmonyOS鸿蒙Next中使用RSA公钥字符串对明文数据进行分段加密,SHA1签名工具
HarmonyOS鸿蒙Next中使用RSA公钥字符串对明文数据进行分段加密,SHA1签名工具
导包
SHA1包
import CryptoJS from '@ohos/crypto-js';
RSA包官方API12直接使用
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';
工具类直接复制修改下密钥字段Constants.RAS_KEY_FAT就可以直接使用
export default new class EncryptSignUtils {
/**
* sign签名
* @param text
* @returns
*/
public sha1Encrypt(text: string): string {
return CryptoJS.SHA1(text).toString();
}
/**
* RSA加密
* @param message
* @returns
*/
public rsaEncryptLongMessage(message: string) {
return new Promise<string>((reslove) => {
let base64Helper = new util.Base64Helper();
// 公钥转换为Uint8Array,然后包装为DataBlob类型
let pubKeyBlob: cryptoFramework.DataBlob = { data: base64Helper.decodeSync(Constants.RAS_KEY_FAT) };
let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from(message, 'utf-8').buffer) };
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA2048|PRIMES_2");
rsaGenerator.convertKey(pubKeyBlob, null, (err, keyPair) => {
if (err) {
}
this.rsaEncryptBySegment(keyPair.pubKey, plainText).then(res => {
reslove(res)
})
});
})
}
// 分段加密
private async rsaEncryptBySegment(pubKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
let cipher = cryptoFramework.createCipher("RSA2048|PKCS1");
let base64 = new util.Base64Helper();
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
let plainTextSplitLen = Constants.MAX_ENCRYPT_BLOCK;
let cipherText = new Uint8Array();
for (let i = 0; i < plainText.data.length; i += plainTextSplitLen) {
let updateMessage = plainText.data.subarray(i, i + plainTextSplitLen);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
// 将原文按64字符进行拆分,循环调用doFinal进行加密,使用1024bit密钥时,每次加密生成128字节长度的密文
let updateOutput = await cipher.doFinal(updateMessageBlob);
let mergeText = new Uint8Array(cipherText.length + updateOutput.data.length);
mergeText.set(cipherText);
mergeText.set(updateOutput.data, cipherText.length);
cipherText = mergeText;
}
let cipherBlob: cryptoFramework.DataBlob = { data: cipherText };
let encodeString = base64.encodeToStringSync(cipherBlob.data)
return encodeString
}
}
调用方式
let signData=EncryptSignUtils.sha1Encrypt('sign')
let rsaData= await EncryptSignUtils.rsaEncryptLongMessage('RSA') || ''
随机方式参考链接改下生成那块的代码即可
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/crypto-rsa-asym-encrypt-decrypt-by-segment-V5
更多关于HarmonyOS鸿蒙Next中使用RSA公钥字符串对明文数据进行分段加密,SHA1签名工具的实战教程也可以访问 https://www.itying.com/category-93-b0.html
分段加密长度 我这边是 MAX_ENCRYPT_BLOCK = 245
更多关于HarmonyOS鸿蒙Next中使用RSA公钥字符串对明文数据进行分段加密,SHA1签名工具的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,使用RSA公钥字符串对明文数据进行分段加密并生成SHA1签名的步骤如下:
-
导入RSA公钥:首先,将RSA公钥字符串转换为
PublicKey
对象。可以使用KeyFactory
和X509EncodedKeySpec
类来实现。 -
分段加密:由于RSA加密有长度限制,需要对明文数据进行分段处理。通常,RSA加密的最大数据长度为密钥长度减去填充长度。对于2048位密钥,最大加密数据长度为245字节。
-
加密数据:使用
Cipher
类进行加密。设置加密模式为RSA/ECB/PKCS1Padding
,并使用PublicKey
对象初始化Cipher
。 -
生成SHA1签名:使用
MessageDigest
类生成SHA1签名。将加密后的数据进行哈希计算,生成签名。 -
输出结果:将加密后的数据和SHA1签名输出。
示例代码如下:
import ohos.security.provider.KeyFactory;
import ohos.security.provider.X509EncodedKeySpec;
import ohos.security.cipher.Cipher;
import ohos.security.digest.MessageDigest;
import ohos.utils.Base64;
public class RSAEncryption {
public static byte[] encryptWithRSA(String publicKeyStr, byte[] data) throws Exception {
// 导入RSA公钥
byte[] publicKeyBytes = Base64.decode(publicKeyStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
// 分段加密
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int blockSize = 245; // 2048位密钥
int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
while (inputLen - offSet > 0) {
if (inputLen - offSet > blockSize) {
cache = cipher.doFinal(data, offSet, blockSize);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
offSet += blockSize;
}
byte[] encryptedData = out.toByteArray();
out.close();
// 生成SHA1签名
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
byte[] signature = sha1.digest(encryptedData);
return encryptedData;
}
}
此代码展示了如何在HarmonyOS鸿蒙Next中使用RSA公钥字符串对明文数据进行分段加密,并生成SHA1签名。
在HarmonyOS鸿蒙Next中,可以使用java.security
包实现RSA公钥字符串对明文数据进行分段加密,并结合MessageDigest
类进行SHA1签名。首先,将公钥字符串转换为PublicKey
对象,使用Cipher
类进行分段加密。对于SHA1签名,使用MessageDigest
类生成消息摘要。示例代码:
import java.security.*;
import javax.crypto.Cipher;
public class RSAUtil {
public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
public static byte[] sha1(byte[] data) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
return md.digest(data);
}
}
注意:分段加密需根据密钥长度进行分块处理,确保数据长度不超过密钥长度。