Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webapi中的加密问题_C#_Asp.net Mvc_Vb.net_Asp.net Web Api2 - Fatal编程技术网

C# webapi中的加密问题

C# webapi中的加密问题,c#,asp.net-mvc,vb.net,asp.net-web-api2,C#,Asp.net Mvc,Vb.net,Asp.net Web Api2,我正在为我的web应用程序开发一个API。在我的API中,我连接的数据库与我的web应用程序中使用的数据库相同。我需要加密API中的密码,并将其与数据库中已存在的加密格式的密码进行比较。现在,由于我是这个项目的新手,问题是我不知道在数据库中存储密码时密码是如何被加密的,所以我在API中使用什么格式来加密密码呢。 之前用于加密密码的函数如下所示: Public Function Encrypt(ByVal input() As Byte) As Byte() Dim i As Integer

我正在为我的web应用程序开发一个API。在我的API中,我连接的数据库与我的web应用程序中使用的数据库相同。我需要加密API中的密码,并将其与数据库中已存在的加密格式的密码进行比较。现在,由于我是这个项目的新手,问题是我不知道在数据库中存储密码时密码是如何被加密的,所以我在API中使用什么格式来加密密码呢。 之前用于加密密码的函数如下所示:

Public Function Encrypt(ByVal input() As Byte) As Byte()
  Dim i As Integer = 0
  Dim iLen As Integer = input.Length
  Dim output(0) As Byte
  Dim newInput() As Byte
  Dim inBuffer(BlockSize - 1) As Byte
  Dim buffer(BlockSize - 1) As Byte
  Dim count As Integer = 0
  Try
     count = GetArraySize(input.Length)
     output = New Byte(count- 1) {}
     newInput - New Byte(count - 1) {}
     System.Array.copy(input, 0, newInput, 0, input.Length)
     For i = 0 To count - BlockSize 
         System.Array.Copy(newInput, i, inBuffer, 0, BlockSize)
         System.Array.copy(Cipher(inBuffer), 0, output, i, Blocksize)
     Next i
   Catch excep As Exception
      Throw
   End Try
   Return output
End Function


Private Function Cipher(ByVal input() As Byte) As Byte()

Dim buffer1 As Byte() = New Byte(16 - 1) {}
Try
    Me.State = New Byte(4 - 1, Me.Nb - 1) {}
    Dim num1 As Integer
    For num1 = 0 To (4 * Me.Nb) - 1
        Me.State((num1 Mod 4), Int(num1 / 4)) = input(num1)
    Next num1
    Me.AddRoundKey(0)
    Dim num2 As Integer = 1
    Do While (num2 <= (Me.Nr - 1))
        Me.SubBytes()
        Me.ShiftRows()
        Me.MixColumns()
        Me.AddRoundKeys(num2)
        num2 +=1
     Loop
     Me.SubBytes()
     Me.ShiftRows()
     Me.AddRoundKey(Me.Nr)
     Dim num3 As Integer
     For num3 = 0 To (4 * Me.Nb) - 1
         buffer1(num3) = Me.State((num3 Mod 4), Int(num3 / 4))
     Next num3

  Catch exception As Exception
      Throw
  End Try
  Return buffer1
End Function
Public函数加密(ByVal input()作为字节)作为字节()
尺寸i为整数=0
Dim iLen作为整数=输入.Length
Dim输出(0)为字节
Dim newInput()作为字节
Dim inBuffer(块大小-1)作为字节
Dim缓冲区(块大小-1)作为字节
Dim计数为整数=0
尝试
count=GetArraySize(input.Length)
输出=新字节(计数-1){}
新输入-新字节(计数-1){
System.Array.copy(输入,0,newInput,0,输入.长度)
对于i=0进行计数-块大小
System.Array.Copy(newInput、i、inBuffer、0、BlockSize)
System.Array.copy(密码(inBuffer)、0、输出、i、块大小)
接下来我
捕获例外作为例外
扔
结束尝试
返回输出
端函数
专用函数密码(ByVal input()作为字节)作为字节()
Dim buffer1 As Byte()=新字节(16-1){}
尝试
Me.State=新字节(4-1,Me.Nb-1){}
作为整数的Dim num1
对于num1=0到(4*Me.Nb)-1
Me.State((num1 Mod 4),Int(num1/4))=输入(num1)
下一个num1
Me.AddRoundKey(0)
Dim num2为整数=1
照我所理解的那样做(num2)

如果您将项目从Vb迁移到C#Web API和 你的数据库还是一样的

然后,您将面临一个问题,即如何在c#中编码以解密密码,而c#中的密码已经在vb中加密,反之亦然

意味着您需要与Vb中使用的相同的c#加密或解密算法

那么对于这个

在这里,我为您在问题中添加的vb encrypt和cipher方法添加了C#等效代码

1) 用于加密

//Encrypt

public byte[] Encrypt(byte[] input)
{
    int i = 0;
    int iLen = input.Length;
    byte[] output = new byte[1];
    byte[] newInput;
    byte[] inBuffer = new byte[BlockSize - 1 + 1];
    byte[] buffer = new byte[BlockSize - 1 + 1];
    int count = 0;
    try
    {
        count = GetArraySize(input.Length);
        output = new byte[count - 1 + 1];
        newInput[-new byte[count - 1 + 1]];
        System.Array.copy(input, 0, newInput, 0, input.Length);
        for (i = 0; i <= count - BlockSize; i++)
        {
            System.Array.Copy(newInput, i, inBuffer, 0, BlockSize);
            System.Array.copy(Cipher(inBuffer), 0, output, i, Blocksize);
        }
    }
    catch (Exception excep)
    {
        throw;
    }
    return output;
}
//加密
公共字节[]加密(字节[]输入)
{
int i=0;
int iLen=输入长度;
字节[]输出=新字节[1];
字节[]新输入;
字节[]inBuffer=新字节[块大小-1+1];
byte[]buffer=新字节[BlockSize-1+1];
整数计数=0;
尝试
{
count=GetArraySize(input.Length);
输出=新字节[计数-1+1];
新输入[-new byte[count-1+1]];
System.Array.copy(输入,0,newInput,0,输入.长度);

对于(i=0;i为什么要加密密码?你不应该这样做。如果你想知道为什么不加密的原因,请阅读。如果你在散列后加密,请忽略我的评论。如果你存储的密码是加密的,而不是散列的。你应该立即改为使用密码散列,并回顾性地散列现有密码。@john-先生,如果我不这样做的话't t t加密我的文本密码,我将如何将其与存储在我的数据库中的加密密码进行比较。如果我错了,请更正我。我需要继续登录页面。阅读以了解哈希。给定值的哈希每次都将返回相同的哈希值。使用salt值对密码进行哈希,然后存储哈希后的密码和salt。当用户登录时,您可以将他们提供的密码与存储密码所用的salt进行散列。然后您可以将存储的散列与计算的散列进行比较。如果两者匹配,则密码是正确的。这是一个简单的版本,但我相信您会欣赏这个想法。@TausifKhan听说过散列密码,然后验证其是否正确?请检查libnaude for.NET,以获得一个简单而安全的实现。您不需要出于任何原因知道或检索用户的明文密码。您需要访问该密码的唯一时间是在对其进行哈希之前。@Tausif Khan,请检查答案,如果有帮助,请投票并接受答案:)
//Cypher

private byte[] Cipher(byte[] input)
{
    byte[] buffer1 = new byte[16] { };
    try
    {
        this.State = new byte[4, this.Nb - 1 + 1];
        int num1;
        for (num1 = 0; num1 <= (4 * this.Nb) - 1; num1++)
            this.State((num1 % 4), Conversion.Int(num1 / (double)4)) = input[num1];
        this.AddRoundKey(0);
        int num2 = 1;
        while ((num2 <= (this.Nr - 1)))
        {
            this.SubBytes();
            this.ShiftRows();
            this.MixColumns();
            this.AddRoundKeys(num2);
            num2 += 1;
        }
        this.SubBytes();
        this.ShiftRows();
        this.AddRoundKey(this.Nr);
        int num3;
        for (num3 = 0; num3 <= (4 * this.Nb) - 1; num3++)
            buffer1[num3] = this.State((num3 % 4), Conversion.Int(num3 / (double)4));
    }
    catch (Exception exception)
    {
        throw;
    }
    return buffer1;
}