Web 中的加密问题 API

Encryption issue in Web API

我正在为我的网络应用程序开发 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

此外,之前的代码在 VB.NET 中,而我的 API 是 API with MVC。

据我了解,

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

然后你面临一个问题,即如何在 c# 中编写解密密码,其中密码已在 vb 中加密,反之亦然。

意味着您想要在 Vb.

中使用的 c# 中相同的加密或解密算法

所以对于这个

我在此处为您在问题中添加的 vb 加密和加密方法添加了 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;
}

2) 对于 Cypher

//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;
}

试试看对你有帮助