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.
C#
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 = 'a' ? tempCharValue : tempCharValue + 26);
h += Convert.ToInt32(temp[temp.Length - 1]);
}
for (int i = 0; i = 'a' ? charValue : charValue + 26);
}
return decryptedText;
}
}
}
Amit MohantyRank #112 · 20K reputationPosted Feb 20, 2024, 9:29 AM EST
Try this
Sub Main() Console.WriteLine("Enter the encrypted text:") Dim plainText As String = Console.ReadLine() Dim encryptedText As String = Encrypt(plainText) Console.WriteLine("Encrypted Text VB: " & encryptedText) Dim decryptedText As String = Encrypt(encryptedText) Console.WriteLine("Decrypted Text VB: " & decryptedText) Console.ReadLine() End Sub Private Function Encrypt(clearText As String) As String Dim EncryptionKey As String = "MAKV2SPBNI99212" Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText) Using encryptor As Aes = Aes.Create() Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}) encryptor.Key = pdb.GetBytes(32) encryptor.IV = pdb.GetBytes(16) Using ms As New MemoryStream() Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write) cs.Write(clearBytes, 0, clearBytes.Length) cs.Close() End Using clearText = Convert.ToBase64String(ms.ToArray()) End Using End Using Return clearText End Function Private Function Decrypt(cipherText As String) As String Dim EncryptionKey As String = "MAKV2SPBNI99212" Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText) Using encryptor As Aes = Aes.Create() Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76}) encryptor.Key = pdb.GetBytes(32) encryptor.IV = pdb.GetBytes(16) Using ms As New MemoryStream() Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write) cs.Write(cipherBytes, 0, cipherBytes.Length) cs.Close() End Using cipherText = Encoding.Unicode.GetString(ms.ToArray()) End Using End Using Return cipherText End Functionstatic void Main(string[] args) { Console.WriteLine("Enter the encrypted text:"); string plainText = Console.ReadLine(); string encryptedText = Encrypt(plainText); Console.WriteLine("Encrypted Text: " + decryptedText); string decryptedText = Decrypt(encryptedText); Console.WriteLine("Decrypted Text C#: " + decryptedText); } private static string Encrypt(string clearText) { string EncryptionKey = "MAKV2SPBNI99212"; byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()); } } return clearText; } private static string Decrypt(string cipherText) { string EncryptionKey = "MAKV2SPBNI99212"; byte[] cipherBytes = Convert.FromBase64String(cipherText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return cipherText; }Bhavesh RavalRank #1,245 · 1.4K reputationPosted Feb 20, 2024, 9:18 AM EST
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:
using System; class Program { static void Main(string[] args) { int a = 10; int b = 20; Console.WriteLine("{0} {1}", a, b); } }VB.NET Console Application:
Module Program Sub Main() Dim a As Integer = 10 Dim b As Integer = 20 Console.WriteLine("{0} {1}", a, b) End Sub End ModuleBoth 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.
Bhavesh RavalRank #1,245 · 1.4K reputationPosted Feb 20, 2024, 9:20 AM EST
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:
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 = "" 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 NamespaceC# Console Application:
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 = 'a' ? tempCharValue : tempCharValue + 26); h += Convert.ToInt32(temp[temp.Length - 1]); } for (int i = 0; i = 'a' ? charValue : charValue + 26); } return decryptedText; } } }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.