欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > HarmonyOS NEXT - 使用crypto-js实现Base64、MD5、AES加解密(CBC/PKCS7)

HarmonyOS NEXT - 使用crypto-js实现Base64、MD5、AES加解密(CBC/PKCS7)

2024/10/24 21:36:47 来源:https://blog.csdn.net/iotjin/article/details/141224280  浏览:    关键词:HarmonyOS NEXT - 使用crypto-js实现Base64、MD5、AES加解密(CBC/PKCS7)

demo 地址: https://github.com/iotjin/JhHarmonyDemo
代码不定时更新,请前往github查看最新代码

在线加密网站

AES加密
MD5
Base64

OpenHarmony三方库中心仓 已经有大牛发布适配好的crypto-js库,直接使用这个就行

@ohos/crypto-js

需要先在项目中导入三方库

ohpm  install @ohos/crypto-js 

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

更多使用方法请参照 @ohos/crypto-js 的demo 及 参考原库使用文档

我在项目中只用了Base64、MD5、AES加解密(CBC/PKCS7) 写了个工具类(JhEncryptUtils.ets)进行调用

调用:

aboutToAppear() {this.test('123')}test(str: string) {console.log('-------------------数据加解密-------------------')const text = strconsole.log(`明文:${text}`)const base64encodeStr = JhEncryptUtils.encodeBase64(text)console.log(`base64编码: ${base64encodeStr}`)const base64decodeStr = JhEncryptUtils.decodeBase64(base64encodeStr)console.log(`base64解码: ${base64decodeStr}`)const enStr = JhEncryptUtils.aesEncrypt(text)console.log(`AES 加密:${enStr}`)const deStr = JhEncryptUtils.aesDecrypt(enStr)console.log(`AES 解密:${deStr}`)const md5 = JhEncryptUtils.encodeMd5(text)console.log(`md5 :${md5}`)}

结果:

-------------------数据加解密-------------------明文:123base64编码: MTIzbase64解码: 123AES 加密:tLVv8L9xQCNQ8dA04wlhPw==AES 解密:123md5 :202cb962ac59075b964b07152d234b70

全部代码:

///  JhEncryptUtils.ets
///
///  Created by iotjin on 2024/08/09. 
///  description: Base64、MD5、AES加解密(CBC/PKCS7)import { CryptoJS } from '@ohos/crypto-js'// 16或32位密钥
const SECRET_KEY: string = CryptoJS.enc.Utf8.parse('1231231231231231')
// 16位密钥偏移量
const SECRET_IV: string = CryptoJS.enc.Utf8.parse('4564564564564564')export class JhEncryptUtils {/// Base64编码public static encodeBase64(data: string) {const encoded: string = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data))return encoded}/// Base64解码public static decodeBase64(data: string) {const decoded: string = CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8)return decoded}/// md5 加密 32位小写public static encodeMd5(plainText: string) {let hash: string = CryptoJS.MD5(plainText)return hash.toString()}/// 字符串AES加密public static aesEncrypt(dataStr: string) {const dataHex: string = CryptoJS.enc.Utf8.parse(dataStr)const encrypted: string = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {iv: SECRET_IV,mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7})return encrypted.toString()}/// 字符串AES解密public static aesDecrypt(dataStr: string) {const encryptedStr: string = CryptoJS.enc.Base64.parse(dataStr)const str: string = CryptoJS.enc.Base64.stringify(encryptedStr)const decrypt: string = CryptoJS.AES.decrypt(str, SECRET_KEY, {iv: SECRET_IV,mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7})const decryptedStr: string = CryptoJS.enc.Utf8.stringify(decrypt)return decryptedStr.toString()}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com