brix / crypto-js

JavaScript library of crypto standards.
Other
15.82k stars 2.39k forks source link

Using CryptoJS AES alongside C# AES #186

Open TheColorRed opened 5 years ago

TheColorRed commented 5 years ago

We have a website that uses CryptoJS to encode data. We encode it like this:

let saveData = CryptoJS.AES.encrypt(data || '', gs.cryptoSecret).toString()

I need to take that data and decode it using C# I have tried doing this, but I am getting:

Invalid input block size.

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.

using System.Security.Cryptography;
using System;
using System.IO;
using System.Text;
using UnityEngine;

  public class AES {

    public static string Decrypt(string message, string secret, string salt = "zAvR2NI87bBx746n") {
      return Encoding.UTF8.GetString(AESDecryptBytes(
        Encoding.UTF8.GetBytes(message),
        Encoding.UTF8.GetBytes(secret),
        Encoding.UTF8.GetBytes(salt)
      ));
    }

    private static byte[] AESDecryptBytes(byte[] cryptBytes, byte[] passBytes, byte[] saltBytes) {
      byte[] clearBytes = null;

      // create a key from the password and salt, use 32K iterations
      var key = new Rfc2898DeriveBytes(passBytes, saltBytes, 32768);

      using (Aes aes = new AesManaged()) {
        // set the key size to 256
        aes.KeySize = 256;
        aes.Padding = PaddingMode.PKCS7;
        aes.Mode = CipherMode.CBC;
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);

        using (MemoryStream ms = new MemoryStream()) {
          using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) {
            cs.Write(cryptBytes, 0, cryptBytes.Length);
            cs.Close();
          }
          clearBytes = ms.ToArray();
        }
      }
      return clearBytes;
    }
  }
alex254 commented 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.

KevMac19 commented 1 year ago

@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) {}
  };