Open TheColorRed opened 5 years ago
Hej man,
I've been struggling with this too. But I managed to get it to work with RijndaelManaged instead of AesManaged.
@TheColorRed - I have tried with CryptoJS AES with VB.Net my sample code may be help to you
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Module VBModule
Sub Main()
Dim encryptedText As String = ""
encryptedText = Encrypt("Hello There","SAMPLEENCRYPTIONKEY")
Console.WriteLine("Encrypted String : {0}",encryptedText)
End Sub
Public Function Encrypt(ByVal plainText As String, ByVal encryptionKey As String) As String
Dim iv As Byte() = New Byte(15) {} 'Initialization vector for AES
Dim key As Byte() = Encoding.UTF8.GetBytes(encryptionKey)
Dim aes As Aes = Aes.Create()
Using aes
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.PKCS7
aes.Key = key
aes.IV = iv
Using encryptor As ICryptoTransform = aes.CreateEncryptor()
Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
Dim cipherBytes As Byte() = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length)
Dim cipherText As String = Convert.ToBase64String(cipherBytes)
Return cipherText
End Using
End Using
End Function
const NewDecryptMethod = () => {
try {
const decryptionKey = 'SAMPLEENCRYPTIONKEY';
const encryptedText1 = 'syXwtX1sv0QS5itaEgJ+/w=='; // ENCRYPTED String from vb.net code
// Convert base64-encoded string to bytes
const cipherBytes = CryptoJS.enc.Base64.parse(encryptedText);
// Decrypt using AES with ECB mode and PKCS7 padding
const decryptedString = CryptoJS.AES.decrypt(
{ciphertext: cipherBytes},
CryptoJS.enc.Utf8.parse(decryptionKey),
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
},
).toString(CryptoJS.enc.Utf8);
console.log("Decrypted String : ", decryptedString);
} catch (error) {}
};
We have a website that uses CryptoJS to encode data. We encode it like this:
I need to take that data and decode it using C# I have tried doing this, but I am getting:
I was hoping that someone could help me get the C# version to work alongside the CryptoJS version. I have tried looking at the source, and have asked on Stackoverflow, both without any luck.