我是角色2的新手。我想用c#中的密钥加密我的图像,并用打字稿解密。那可能吗。如果有,任何人都可以帮助我。 提前谢谢。
加密代码
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;
}
时出错
var bytesCypherText = csp.Encrypt(imageBytes, false);
长度不好
答案 0 :(得分:1)
在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));
}
TypeScript中的Descrypt
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;
此处c#中的Encryptionkey和打字稿中的键是相同的
答案 1 :(得分:-1)
您好,您可以在两侧使用任何加密引擎。
例如,您可以使用:
对于TypeScript方: RSA library with angular
对于C#Side: C# RSA encryption/decryption with transmission
对于打字稿方: 使用https://github.com/ricmoo/aes-js,然后执行:
import 'aes-js'; declare var aesjs: any;
对于C#方: 更多信息,请Using AES encryption in C#
由于所有加密算法都有许多配置参数,因此请注意在C#和Typescript端配置匹配。