मैं वर्तमान में सी # में एईएस कार्यान्वयन पर काम कर रहा हूं। एन्क्रिप्शन विधि में दो पैरामीटर हैं: एक स्ट्रिंग और पासवर्ड। मैं आपूर्ति की गई स्ट्रिंग ले रहा हूं और इसे बाइट्स की एक सरणी में परिवर्तित कर रहा हूं, इसलिए मैं इसे बाद में BinaryWriter
के साथ डेटा लिखने के लिए उपयोग कर सकता हूं।एईएस एन्क्रिप्शन के लिए स्ट्रिंग से बाइट्स को सही तरीके से और लगातार कैसे प्राप्त करें?
समस्या यह है कि जब मैं Convert.FromBase64String(string)
का उपयोग मैं FormatException: Invalid length.
हो और जब मैं का उपयोग Encoding.UTF8.GetBytes(string)
मेरी डिक्रिप्शन विधि फेंकता है और अवैध PKCS7.Padding अपवाद नहीं है।
मैं पिछले कुछ दिनों से इस समस्या को हल करने का प्रयास कर रहा हूं। मैंने stackoverflow.com और अन्य वेबसाइटों में अनंत प्रश्नों के करीब पढ़ा है, लेकिन मुझे अभी भी पता नहीं है कि इस समस्या को हल करने का सबसे विश्वसनीय तरीका क्या है।
इस प्रोग्राम में उपयोग किए जाने वाले स्ट्रिंग्स वाक्यों तक सीमित हैं (उदा। "कुछ एन्क्रिप्ट करने के लिए।") और संख्याएं (उदा। "12345")।
अग्रिम धन्यवाद, यहाँ समय में कोड इस बिंदु पर मेरे पास है:
public class AESProvider {
public byte[] EncryptStringToBytes_Aes(string plainText, string Key)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
byte[] plainTextInBytes = Convert.FromBase64String(plainText);
byte[] encrypted;
//Create an Aes object
//with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.GenerateIV();
byte[] IV = aesAlg.IV;
//The Salt will be the first 8 bytes of the IV.
byte[] theSalt = new byte[8];
Array.Copy(IV,theSalt,8);
//A key for AES is generated by expanding the password using the following method.
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt);
byte[] aesKey = keyGen.GetBytes(16);
aesAlg.Key = aesKey;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (BinaryWriter swEncrypt = new BinaryWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainTextInBytes);
}
encrypted = msEncrypt.ToArray();
}
}
// Prepend the IV to the ciphertext so it can be used in the decryption process.
using (MemoryStream ivPlusCipher = new MemoryStream())
{
using (BinaryWriter tBinaryWriter = new BinaryWriter(ivPlusCipher))
{
tBinaryWriter.Write(IV);
tBinaryWriter.Write(encrypted);
tBinaryWriter.Flush();
}
return ivPlusCipher.ToArray();
}
}
}
public byte[] DecryptStringFromBytes_Aes(byte[] cipherText, string Key)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
byte[] decrypted;
// Create an Aes object
// with the specified key and IV.
// Create the streams used for decryption.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
//Grab IV from ciphertext
byte[] IV = new byte[16];
Array.Copy(cipherText,0,IV,0,16);
//Use the IV for the Salt
byte[] theSalt = new byte[8];
Array.Copy(IV,theSalt,8);
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt);
byte[] aesKey = keyGen.GetBytes(16);
aesAlg.Key = aesKey;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, IV);
using (MemoryStream msDecrypt = new MemoryStream())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
using (BinaryWriter srDecrypt = new BinaryWriter(csDecrypt))
{
//Decrypt the ciphertext
srDecrypt.Write(cipherText, IV.Length, (cipherText.Length - IV.Length));
}
decrypted = msDecrypt.ToArray();
return decrypted;
}
}
}
}
}
आपकी 'डिक्रिप्टस्ट्रिंगफ्रॉमबाइट्स' एक स्ट्रिंग नहीं लौटाती है, क्यों? –
सबसे महत्वपूर्ण सवाल यह है और रहेगा: इनपुट स्ट्रिंग में क्या रखा जाता है जिसे आप डिक्रिप्ट करना चाहते हैं। मेरा मतलब है, अगर यह हेक्साडेसिमल है तो कोई जवाब सही नहीं होगा। कृपया एक उदाहरण प्रदान करें! –
इनपुट के लिए धन्यवाद। स्ट्रिंग के उदाहरण जहां पहले से ही प्रश्न में जोड़ा गया है! – Mandos