[英]Encryption Method using md5 hash and triple Des encryption in angular 7
I want to implement the same encryption with angular7,我想用angular7实现同样的加密,
The below function is in C#, I tried crypto-js but its not giving me the same results.下面的 function 在 C# 中,我尝试了 crypto-js 但没有给我相同的结果。
using System.Security.Cryptography;使用 System.Security.Cryptography;
public static string Encrypt(string PlainText, string SecretKey)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(PlainText);
var hashMD5 = new MD5CryptoServiceProvider();
keyArray = hashMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecretKey));
hashMD5.Clear();
var triDESProv = new TripleDESCryptoServiceProvider();
triDESProv.Key = keyArray;
triDESProv.Mode = CipherMode.ECB;
triDESProv.Padding = PaddingMode.PKCS7;
var cTransform = triDESProv.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
triDESProv.Clear();
string encText = Convert.ToBase64String(resultArray, 0, resultArray.Length);
return encText;
}
catch
{
return "ERROR";
}
}
Instead of the algorithm you implemented, why not just install libsodium-net in C#, and sodium-plus in JavaScript, and use crypto_secretbox()
?而不是您实现的算法,为什么不在 C# 中安装libsodium-net ,在 JavaScript 中安装钠加,并使用
crypto_secretbox()
?
Libsodium's secretbox is more secure than the code you posted above for the following reasons: Libsodium 的 secretbox 比您上面发布的代码更安全,原因如下:
In every single way, secretbox is more secure.在每一个方面,secretbox 都更加安全。 Support for libsodium is also near-ubiquitous .
对 libsodium 的支持也 几乎无处不在。
I am not an expert on the subject but I was able to solve it我不是这方面的专家,但我能够解决它
.NET - C# .NET - C#
private static readonly string key = "password";
public static string Encriptar(string texto)
{
byte[] keyArray;
byte[] Arreglo_a_Cifrar = Encoding.UTF8.GetBytes(texto);
byte[] ivArray = new byte[8];
//Algoritmo MD5
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//se guarda la llave para que se le realice hashing
keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
Array.Copy(keyArray, 0, ivArray, 0, 8);
//Algoritmo 3DES
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
IV = ivArray
};
//se empieza con la transformación de la cadena
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] ArrayResultado = cTransform.TransformFinalBlock(Arreglo_a_Cifrar,0, Arreglo_a_Cifrar.Length);
tdes.Clear();
return Convert.ToBase64String(ArrayResultado, 0, ArrayResultado.Length);
}
Angular - CryptoJS Angular - 加密JS
encriptarDatos(valor: string) {
var key = CryptoJS.MD5(CryptoJS.enc.Utf8.parse(environment.keyHash));
var iv = {...key};
iv.sigBytes = 8;
var mode = CryptoJS.mode.ECB;
var encrypted = CryptoJS.TripleDES.encrypt(valor, key, { iv, mode }).toString();
console.log('DATO ENC:'+encrypted);
return encrypted;
}
its very important that IV (initilization vecctor) must be part of the key and must be the same size in both framewors(Net - Angular)非常重要的是, IV (初始化向量)必须是密钥的一部分,并且在两个框架(Net - Angular)中必须具有相同的大小
My password was 10 characters long and i dont test another size我的密码有 10 个字符长,我没有测试其他尺寸
Help links:帮助链接:
CryptoJS-encryptUsingTripleDES CryptoJS-encryptUsingTripleDES 加密
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.