0

I am having the code to encrypt data in Angular, but I don't know how decrypt it on the server side.

Angular code

public static getEncryptedInfo(dataString): string {
    let password = environment.encryptionKey;
    var text = dataString.toString(CryptoJS.enc.Utf8)
    var encrypted = CryptoJS.AES.encrypt(text, password).toString();
    const encodedComponent = encodeURIComponent(encrypted).toString();
    return encodedComponent;
}

Using encryptionKey=12345

Encrypted As

U2FsdGVkX19klqZyBO7JyNzfxCBxAizFPkVYX%2Bb%2BUNs%2FVGEcr%2Fcxz3JmQcEgiojQ

in Angular, and I pass this encrypted value to the server side.

I try this reference also decrypting it in C# but I am getting an error

public static string Decrypt(string plainText)
{
    RijndaelManaged aes = new RijndaelManaged();
    string decodeString = System.Web.HttpUtility.UrlDecode(plainText);
    var base64Decode = Encoding.UTF8.GetString(Convert.FromBase64String(decodeString));

    // var inputArray = Convert.FromBase64String(decodeString);
    ICryptoTransform AESDecrypt = aes.CreateDecryptor();

    var de = AESDecrypt.TransformFinalBlock(base64Decode, 0, base64Decode.Length);
    return encoding.GetString(de);
}

public static string DecryptText(string EncryptedText)
{
    if (string.IsNullOrEmpty(EncryptedText))
    {
        return "";
    }
    else
    {
        RijndaelManaged aes = new RijndaelManaged();
        string decodeString = System.Web.HttpUtility.UrlDecode(EncryptedText);
        var base64Decode = Encoding.UTF8.GetString(Convert.FromBase64String(decodeString));
        Byte[] outputBytes = StringToByteArray(base64Decode);
        var bytesToDecrypt = Convert.FromBase64String(EncryptedText);
        CryptoJS.AES.decrypt(encodedBytes, Secretkey).toString(CryptoJS.enc.Utf8)
        var actualText = Encoding.UTF8.GetString(base64Decode);

        var  de = base64Decode.Substring(0, base64Decode.Length - DecryptionKey.Length);

        return de;
    }
}

Did I miss anything?

2
  • 2
    I really can't follow what these two separate Decrypt methods are. Anyway: the key and initialization vector have to match what was used for encryption for decryption to be successful. I believe CryptoJS derives a key from a passphrase if a string is passed as the encryption key. Commented Nov 17, 2022 at 5:09
  • The code is either incomplete or won’t compile. encodedBytes is used without declaring or giving a value, etc. Please provide a minimal reproducible example and explain what error you’re getting. Commented Nov 17, 2022 at 5:10

1 Answer 1

4

I suggest you see this https://dotnetfiddle.net/Y7TFl0 this perfect c# code for Encryption and Decryption. For JS please check this out https://stackblitz.com/edit/cryptojs-aes-encrypt-decrypt-vfr5py?file=index.js

both are different

my c# code this

string pw = "12345"; string data = Encrypt("[email protected]", pw);

    string angularDecSring = "U2FsdGVkX19klqZyBO7JyNzfxCBxAizFPkVYX%2Bb%2BUNs%2FVGEcr%2Fcxz3JmQcEgiojQ";
    
    string decodeString = HttpUtility.UrlDecode(angularDecSring);
    
    
    Console.WriteLine("UrldecodeString: " + decodeString);
    
    Console.WriteLine("decrypt Value:" + HttpUtility.UrlEncode(data));
    
    Console.WriteLine("Decrypted: " + Decrypt(data, pw));

here is the output :

IV Byte Array: 68 37 67 33 65 34 6D 33 74 35 73 74 35 7A 6A 77 (Size: 16) Blocksize: 128-Bit UrldecodeString: U2FsdGVkX19klqZyBO7JyNzfxCBxAizFPkVYX+b+UNs/VGEcr/cxz3JmQcEgiojQ decrypt Value:IKVfDJLHlkrqoHNTYEabnkwwi8DiAKki4QZ6q9OndAA%3d Decrypted: [email protected]

Sign up to request clarification or add additional context in comments.

1 Comment

Bro i send a LinkedIn connection request plz accept and contact me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.