【问题标题】:Encryption issue in Web APIWeb API 中的加密问题
【发布时间】:2018-07-19 06:53:37
【问题描述】:

我正在为我的 Web 应用程序开发 API。在我的 API 中,我连接的数据库与我的 Web 应用程序中使用的数据库相同。我需要加密并将我的 API 中的密码与我的数据库中已经以加密格式存在的密码进行比较。现在因为我是这个项目的新手,问题是我不知道密码是如何加密的,而之前将它存储在 db 中,所以我在 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 是带有 MVC 的 API。

【问题讨论】:

  • 为什么要加密密码?你不应该这样做。如果您想要一个很好的理由,请阅读this。如果您在散列后加密,请忽略我的评论。如果您正在存储加密的密码,而不是散列。您应该立即更改为使用密码哈希并追溯哈希现有密码。
  • @john - 先生,如果我不加密我的文本密码,我将如何将它与存储在我的数据库中的加密密码进行比较。如果我错了,请纠正我。我需要继续我的登录页面。
  • 阅读here 了解散列。给定值的哈希每次都会返回相同的哈希值。您使用盐值散列密码,并存储散列的密码和盐。当用户登录时,您将他们提供的密码与您用于存储密码的盐进行哈希处理。然后您可以将存储的哈希与计算的哈希进行比较。如果它们匹配,则密码正确。这是一个简单的版本,但我相信你会欣赏这个想法。
  • @TausifKhan 听说过散列密码然后验证它是否正确?查看适用于 .NET 的 libsodium,以轻松安全地实施。
  • 您无需出于任何原因知道或检索用户的明文密码。您唯一需要访问该密码的时间是在散列之前。

标签: c# asp.net-mvc vb.net asp.net-web-api2


【解决方案1】:

据我所知,

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

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

表示您希望在 c# 中使用与在 Vb 中使用的相同的加密或解密算法。

所以对于这个

在这里,我为您在问题中添加的 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

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

尝试一次可能对你有帮助。

【讨论】:

  • @Tausif Khan,请检查答案,如果有帮助,请投票并接受答案:)
猜你喜欢
  • 2020-10-29
  • 1970-01-01
  • 2023-03-17
  • 2020-04-18
  • 2017-04-24
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多