gpt4 book ai didi

javascript - JS 中的 RSACryptoServiceProvider 等效项

转载 作者:行者123 更新时间:2023-12-03 11:17:34 24 4
gpt4 key购买 nike

我一直在开发一个 ASP.NET WEB API RESTFUL 服务,供 angularjs 客户端使用。现在,我正在确保其安全,并决定实现 RSA 加密来获取它。因此,在服务器端,我使用 RSACryptoServiceProvider 方法,并将公钥和私钥存储在文件中。这几乎足够了,但是,在客户端的第一次调用中,它会发送一串用户名和密码进行身份验证并获取 token ,因此我必须对该调用进行加密。

有人知道有关如何在 JS 中实现与我在 C# 中使用的类似功能的教程或手册吗?

下面是WEB API中的一段加密代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace MvcPrueba.Models
{
public class Cryptography
{
#region Types

#region Enum

#endregion

#region Class

#endregion

#endregion

#region Variables

#endregion

#region Properties

#endregion

#region Constructors/Destructors
#region Constructors
protected Cryptography()
{
}
#region Instantiate

#endregion

#endregion

#region Destructors
public void Dispose()
{
throw new NotImplementedException();
}
#endregion

#endregion

#region Class Logic
// Generate a new key pair
public static void GenerateKeys(string publicKeyFileName, string privateKeyFileName)
{
// Variables
CspParameters cspParams = null;
RSACryptoServiceProvider rsaProvider = null;
StreamWriter publicKeyFile = null;
StreamWriter privateKeyFile = null;
string publicKey = "";
string privateKey = "";

try
{
// Create a new key pair on target CSP
cspParams = new CspParameters();
cspParams.ProviderType = 1; // PROV_RSA_FULL
//cspParams.ProviderName; // CSP name
cspParams.Flags = CspProviderFlags.UseArchivableKey;
cspParams.KeyNumber = (int)KeyNumber.Exchange;
rsaProvider = new RSACryptoServiceProvider(cspParams);

// Export public key
publicKey = rsaProvider.ToXmlString(false);

// Write public key to file
publicKeyFile = File.CreateText(publicKeyFileName);
publicKeyFile.Write(publicKey);

// Export private/public key pair
privateKey = rsaProvider.ToXmlString(true);

// Write private/public key pair to file
privateKeyFile = File.CreateText(privateKeyFileName);
privateKeyFile.Write(privateKey);
}
catch (Exception ex)
{
// Any errors? Show them
Console.WriteLine("Exception generating a new key pair! More info:");
Console.WriteLine(ex.Message);
}
finally
{
// Do some clean up if needed
if (publicKeyFile != null)
{
publicKeyFile.Close();
}
if (privateKeyFile != null)
{
privateKeyFile.Close();
}
}

} // Keys

// Encrypt a file
public static void Encrypt(string publicKeyFileName, string plainFileName, string encryptedFileName)
{
// Variables
CspParameters cspParams = null;
RSACryptoServiceProvider rsaProvider = null;
StreamReader publicKeyFile = null;
StreamReader plainFile = null;
FileStream encryptedFile = null;
string publicKeyText = "";
string plainText = "";
byte[] plainBytes = null;
byte[] encryptedBytes = null;

try
{
// Select target CSP
cspParams = new CspParameters();
cspParams.ProviderType = 1; // PROV_RSA_FULL
//cspParams.ProviderName; // CSP name
rsaProvider = new RSACryptoServiceProvider(cspParams);

// Read public key from file
publicKeyFile = File.OpenText(publicKeyFileName);
publicKeyText = publicKeyFile.ReadToEnd();

// Import public key
rsaProvider.FromXmlString(publicKeyText);

// Read plain text from file
plainFile = File.OpenText(plainFileName);
plainText = plainFile.ReadToEnd();

// Encrypt plain text
plainBytes = Encoding.Unicode.GetBytes(plainText);
encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

// Write encrypted text to file
encryptedFile = File.Create(encryptedFileName);
encryptedFile.Write(encryptedBytes, 0, encryptedBytes.Length);
}
catch (Exception ex)
{
// Any errors? Show them
Console.WriteLine("Exception encrypting file! More info:");
Console.WriteLine(ex.Message);
}
finally
{
// Do some clean up if needed
if (publicKeyFile != null)
{
publicKeyFile.Close();
}
if (plainFile != null)
{
plainFile.Close();
}
if (encryptedFile != null)
{
encryptedFile.Close();
}
}

} // Encrypt

// Decrypt a file
public static void Decrypt(string privateKeyFileName, string encryptedFileName, string plainFileName)
{
// Variables
CspParameters cspParams = null;
RSACryptoServiceProvider rsaProvider = null;
StreamReader privateKeyFile = null;
FileStream encryptedFile = null;
StreamWriter plainFile = null;
string privateKeyText = "";
string plainText = "";
byte[] encryptedBytes = null;
byte[] plainBytes = null;

try
{
// Select target CSP
cspParams = new CspParameters();
cspParams.ProviderType = 1; // PROV_RSA_FULL
//cspParams.ProviderName; // CSP name
rsaProvider = new RSACryptoServiceProvider(cspParams);

// Read private/public key pair from file
privateKeyFile = File.OpenText(privateKeyFileName);
privateKeyText = privateKeyFile.ReadToEnd();

// Import private/public key pair
rsaProvider.FromXmlString(privateKeyText);

// Read encrypted text from file
encryptedFile = File.OpenRead(encryptedFileName);
encryptedBytes = new byte[encryptedFile.Length];
encryptedFile.Read(encryptedBytes, 0, (int)encryptedFile.Length);

// Decrypt text
plainBytes = rsaProvider.Decrypt(encryptedBytes, false);

// Write decrypted text to file
plainFile = File.CreateText(plainFileName);
plainText = Encoding.Unicode.GetString(plainBytes);
plainFile.Write(plainText);
}
catch (Exception ex)
{
// Any errors? Show them
Console.WriteLine("Exception decrypting file! More info:");
Console.WriteLine(ex.Message);
}
finally
{
// Do some clean up if needed
if (privateKeyFile != null)
{
privateKeyFile.Close();
}
if (encryptedFile != null)
{
encryptedFile.Close();
}
if (plainFile != null)
{
plainFile.Close();
}
}

} // Decrypt
#endregion
}
}

提前致谢。

最佳答案

@m.dorian - 这是一个非常糟糕的主意!如果您重视安全性和最佳实践,请不要尝试以任何方式实现这一点。我建议您仔细阅读该主题。此外,您应该始终对用户的密码进行哈希处理,而不是对其进行加密。密码应该是单向散列等。

如果您不了解数据安全的具体情况,我建议您退一步,要么与知道自己在做什么的人交谈,要么进行 self 教育。尤其是在过去几年报告的所有数据泄露之后。可以找到 Troy Hunt 的有用帖子 here这应该只是开始!

关于javascript - JS 中的 RSACryptoServiceProvider 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27266120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com