2
Answers

How to implement Deffi Helmen algorithm in VB.NET?

Photo of Shalini Juneja

Shalini Juneja

13y
2.4k
1
How to implement Deffi Helmen algorithm in VB.NET?

Answers (2)

1
Photo of Christopher Walker
NA 18 20 2y

IDEA (International Data Encryption Algorithm) is a symmetric key encryption algorithm that uses a 128-bit key and operates on 64-bit blocks. Here is an example implementation of the IDEA algorithm in VB.NET:

Imports System.Numerics Public Class IDEA Private Shared ReadOnly SubKeysCount As Integer = 52 Private Shared ReadOnly KeyLength As Integer = 16 Private SubKeys(SubKeysCount - 1) As UShort Public Sub New(ByVal key() As Byte) If key.Length <> KeyLength Then Throw New ArgumentException("The key length must be 16 bytes.") End If Dim keyWords(KeyLength \ 2 - 1) As UShort For i As Integer = 0 To KeyLength \ 2 - 1 keyWords(i) = BitConverter.ToUInt16(key, i * 2) Next Dim subKeys(SubKeysCount - 1) As UShort For i As Integer = 0 To SubKeysCount - 1 subKeys(i) = keyWords(i Mod (KeyLength \ 2)) Next For i As Integer = 0 To SubKeysCount - 1 Step 6 Dim m As BigInteger = 0 Dim n As BigInteger = 0 For j As Integer = 0 To 3 m = (m << 16) Or subKeys(i + j) n = (n << 16) Or subKeys(i + 5 - j) Next If m = 0 Then m = BigInteger.One End If Dim invM As BigInteger = BigInteger.ModPow(m, BigInteger.Pow(2, 16) - 2, BigInteger.Pow(2, 16)) subKeys(i) = CType(n * invM Mod BigInteger.Pow(2, 16), UShort) subKeys(i + 1) = CType(m * invM Mod BigInteger.Pow(2, 16), UShort) Dim k As BigInteger = 0 For j As Integer = 0 To 3 k = (k << 16) Or subKeys(i + 2 + j) Next subKeys(i + 2) = CType(k Xor subKeys(i), UShort) subKeys(i + 3) = CType(k Xor subKeys(i + 1), UShort) subKeys(i + 4) = CType(n Xor subKeys(i + 2), UShort) subKeys(i + 5) = CType(m Xor subKeys(i + 3), UShort) Next Me.SubKeys = subKeys End Sub Public Function Encrypt(ByVal data() As Byte) As Byte() If data.Length Mod 8 <> 0 Then Throw New ArgumentException("The data length must be a multiple of 8 bytes.") End If Dim blocks(data.Length \ 8 - 1, 1) As UShort For i As Integer = 0 To data.Length \ 8 - 1 For j As Integer = 0 To 1 blocks(i, j) = BitConverter.ToUInt16(data, i * 8 + j * 2) Next Next For i As Integer = 0 To blocks.GetLength(0) - 1 Dim x1 As UShort = blocks(i, 0) Dim x2 As UShort = blocks(i, 1) For j As Integer = 0 To SubKeysCount - 1 Step 6 Dim y1 As U

http://dotnetslackers.com/articles/security/TheDiffieHellmanKeyAgreementStandard.aspx
http://www.codeproject.com/KB/security/DiffieHellmanExample.aspx