Home > Software engineering >  Encrypting in Angular and decrypting on C# (.NET)
Encrypting in Angular and decrypting on C# (.NET)

Time:11-17

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+b+UNs/VGEcr/cxz3JmQcEgiojQ

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?

CodePudding user response:

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+b+UNs/VGEcr/cxz3JmQcEgiojQ";
    
    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= Decrypted: [email protected]

CodePudding user response:

I suggest you use EasyCrypto or NETCore.Encrypt and don't bother yourself

  • Related