ฉันต้องถอดรหัสภาพที่มาจากบริการออนไลน์ (ซึ่งไม่ใช่ของฉัน ดังนั้นฉันจึงต้องใช้วิธีการเข้ารหัสนี้)
รูปภาพนี้ถูกเข้ารหัสโดยใช้ AES/ECB ด้วยคีย์ซิงโครนัสเดียวและเสริมด้วย PKCS5
ฉันลองหลายวิธีเพื่อให้บรรลุเป้าหมายนี้ แต่ก็ไม่ได้ผลเลย ฉันใช้ไลบรารีการเข้ารหัส BoucyCastle
นี่คือรหัสถอดรหัสของฉัน:
public static byte[] Decrypt(string input)
{
var cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5Padding");
cipher.Init(false, new KeyParameter(Encoding.UTF8.GetBytes(KEY)));
byte[] todo = Encoding.UTF8.GetBytes(Pad(input));
byte[] bytes = cipher.ProcessBytes(todo);
byte[] final = cipher.DoFinal();
// Write the decrypt bytes & final to memory...
var decryptedStream = new MemoryStream(bytes.Length);
decryptedStream.Write(bytes, 0, bytes.Length);
decryptedStream.Write(final, 0, final.Length);
decryptedStream.Flush();
var decryptedData = new byte[decryptedStream.Length];
decryptedStream.Read(decryptedData, 0, (int)decryptedStream.Length);
return decryptedData;
}
private static string Pad(string data)
{
int len = data.Length;
int toAdd = (16 - len % 16);
for (int i = 0; i < toAdd; i++)
{
data += (char)toAdd;
}
return data;
}
เมื่อฉันลอง มันทำให้เกิด InvalidCipherTextExpression พร้อมข้อความ "pad block เสียหาย" ที่บรรทัด byte[] final = cipher.DoFinal();
ฉันทดสอบฟังก์ชันการเติมและดูเหมือนว่าจะทำงานได้อย่างที่คาดไว้
ฉันพยายามดูภายในซอร์สโค้ด BouncyCastle เพื่อค้นหาข้อผิดพลาดของฉัน และสิ่งที่ฉันพบคือบล็อกสุดท้ายไม่มีช่องว่างภายใน และนั่นคือสิ่งที่ทำให้เกิดข้อผิดพลาด ดังนั้นฉันสงสัยว่าฉันกำลังทำอะไรผิดที่อื่นหรือเปล่าเพราะมันอาจไม่มาจากช่องว่างภายใน
อาจเป็นสตริงอินพุตซึ่งดึงมาจากเซิร์ฟเวอร์ http ด้วยสิ่งนี้:
// grab te response and print it out to the console along with the status code
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return new StreamReader(response.GetResponseStream()).ReadToEnd();
สิ่งที่ฉันต้องการบรรลุคือสิ่งเดียวกันกับที่นี่ทุกประการ: C# Decrypting AES /ECB Paddded โดยใช้ PKCS#5
แต่ไม่มีผู้แจ้งเนื่องจากผู้ถามไม่ได้ลองอะไรเลย..
ขอบคุณล่วงหน้า และฉันขอโทษจริงๆ สำหรับภาษาอังกฤษที่ไม่ดีของฉัน