Hi Team
How can achieve same output when writing a c# encryption? I have a vb.net console app that does the same, but the problem the output are not the same with c# console app and want the output when a user insert any string text for their password the decryption be same with c# console.
Imports System
Namespace Decryption
Class Program
Shared Sub Main(args As String())
Console.WriteLine("Enter the encrypted text:")
Dim encryptedText As String = Console.ReadLine()
Dim decryptedText As String = Decrypt(encryptedText)
Console.WriteLine("Decrypted Text: " & decryptedText)
End Sub
Shared Function Decrypt(encryptedText As String) As String
Dim pi As Double = 3.1415927
Dim temp As String = ""
Dim h As Integer = 0
Dim decryptedText As String = ""
' Decrypting loop
For i As Integer = 0 To encryptedText.Length - 1
Dim tempCharValue As Double = Math.Sin((AscW(Char.ToLower(encryptedText(i))) - AscW("a"c)) / 26.0 * pi) * Math.Cos((i + 1) / CDbl(encryptedText.Length) * 0.25 * pi) * 25 + AscW("a"c)
temp += ChrW(If(tempCharValue >= AscW("a"c), tempCharValue, tempCharValue + 26))
h += AscW(temp(temp.Length - 1))
Next
For i As Integer = 0 To encryptedText.Length - 1
Dim charValue As Double = AscW("a"c) + ((AscW(Char.ToLower(temp(i))) - AscW("a"c) - h) + 26) Mod 26
decryptedText += ChrW(If(charValue >= AscW("a"c), charValue, charValue + 26))
Next
Return decryptedText
End Function
End Class
End Namespace
// C# console app.
using System;
namespace Decryption
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the encrypted text:");
string encryptedText = Console.ReadLine();
string decryptedText = Decrypt(encryptedText);
Console.WriteLine("Decrypted Text: " + decryptedText);
}
static string Decrypt(string encryptedText)
{
double pi = 3.1415927;
string temp = "";
int h = 0;
string decryptedText = "";
for (int i = 0; i < encryptedText.Length; i++)
{
double tempCharValue = Math.Sin((Convert.ToInt32(char.ToLower(encryptedText[i])) - Convert.ToInt32('a')) / 26.0 * pi) * Math.Cos((i + 1) / (double)encryptedText.Length * 0.25 * pi) * 25 + Convert.ToInt32('a');
temp += (char)(tempCharValue >= 'a' ? tempCharValue : tempCharValue + 26);
h += Convert.ToInt32(temp[temp.Length - 1]);
}
for (int i = 0; i < encryptedText.Length; i++)
{
double charValue = 'a' + ((Convert.ToInt32(char.ToLower(temp[i])) - 'a' - h) + 26) % 26;
decryptedText += (char)(charValue >= 'a' ? charValue : charValue + 26);
}
return decryptedText;
}
}
}
Amit MohantyPosted Feb 20, 2024, 2:29 PM
Try this
Bhavesh RavalPosted Feb 20, 2024, 2:20 PM
To achieve the same output in both C# and VB.NET console applications when decrypting a string, you need to ensure that the decryption algorithm and logic are identical in both languages. From your provided code, it seems that the decryption algorithm is the same in both programs, but there might be differences in character encoding and other language-specific behaviors that affect the outcome.
Here's a modified version of your code that ensures consistency between C# and VB.NET:
VB.NET Console Application:
C# Console Application:
Ensure that both programs use the same encoding, character manipulation methods, and loop structures to produce consistent results. This should help you achieve the same output when decrypting a string in both C# and VB.NET console applications.
Bhavesh RavalPosted Feb 20, 2024, 2:18 PM
To achieve the same output between a C# console application and a VB.NET console application, you need to ensure that both applications use the same logic to produce the desired output. Both C# and VB.NET provide similar functionalities for console output.
Here's an example to demonstrate how to achieve the same output in both languages:
C# Console Application:
VB.NET Console Application:
Both of these programs will output:
10 20Ensure that you have similar logic and formatting in both C# and VB.NET applications to achieve the same output.