1

Am new to angular 2. I would like to encrypt my image with secret key in c# and decrypt it in typescript. Is that possible. If yes can anyone help me out. Thanks in advance.

Code to encrypt

public string EncryptImage(byte[] imageBytes)
    {
        var csp = new RSACryptoServiceProvider(2048);

        var privKey = csp.ExportParameters(true);

        var pubKey = csp.ExportParameters(false);


        string pubKeyString;
        {
            var sw = new System.IO.StringWriter();

            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));

            xs.Serialize(sw, pubKey);

            pubKeyString = sw.ToString();

        }
        {
            var sr = new System.IO.StringReader(pubKeyString);

            var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));

            pubKey = (RSAParameters)xs.Deserialize(sr);
        }

        csp = new RSACryptoServiceProvider();
        csp.ImportParameters(pubKey);

        var bytesCypherText = csp.Encrypt(imageBytes, false);

        var cypherText = Convert.ToBase64String(bytesCypherText);

        return cypherText;
    }

Getting error at

    var bytesCypherText = csp.Encrypt(imageBytes, false); 

as bad length

Navya Sri
  • 31
  • 1
  • 2
  • 8
  • 1
    _"would like to encrypt my image with secret key"_ You _encrypt_ with the public key and _decrypt_ with the secret key, usually. But what makes you think you need this? – Fildor Apr 11 '18 at 07:33

2 Answers2

1

Encrypting in C#

public string EncryptData(string imageBytesBase64, string Encryptionkey)
    {
        RijndaelManaged objrij = new RijndaelManaged();

        objrij.Mode = CipherMode.CBC;

        objrij.Padding = PaddingMode.PKCS7;

        byte[] keyBytes = Encoding.UTF8.GetBytes(Encryptionkey);

        byte[] ivBytes = Encoding.UTF8.GetBytes(Encryptionkey.Substring(0, 16));

        int len = keyBytes.Length;

        if (len > ivBytes.Length)
            len = ivBytes.Length;

        Array.Copy(keyBytes, ivBytes, len);

        objrij.Key = keyBytes;

        objrij.IV = ivBytes;

        ICryptoTransform objtransform = objrij.CreateEncryptor();

        byte[] textDataByte = Encoding.UTF8.GetBytes(imageBytesBase64);

        return Convert.ToBase64String(objtransform.TransformFinalBlock(textDataByte, 0, textDataByte.Length));

    }

Descrypt in TypeScript

var decryptedTxt = CryptoJS.AES.decrypt(encryptedText, key, {
        keySize: 128 / 8,
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });

    this.decryptedText = decryptedTxt.toString(CryptoJS.enc.Utf8)

    this.ImageSrc = 'data:image/jpg;base64,' + this.decryptedText;

Here Encryptionkey in c# and key in typescript are same

-1

Hello you can use any Crypto engine in both side.

For exemple you can use:

RSA (Public / Private Key)

For TypeScript side : RSA library with angular

For C# Side : C# RSA encryption/decryption with transmission

AES (Password) :

For typescript side : use https://github.com/ricmoo/aes-js and just do :

import 'aes-js'; declare var aesjs: any;

For C# side : More information here Using AES encryption in C#

Because all crypto algo have many configuration parameter, be careful to have matching configuration in C# and Typescript side.

Yanis-git
  • 7,737
  • 3
  • 24
  • 41