I create project for a encrypt and decrypt files with AES method and i have a problem with exception "Padding is invalid and cannot be removed". If you have any idea for solution of my problem, please tell me.
/// <summary>
/// Decrypts specified ciphertext using Rijndael symmetric key algorithm.
/// </summary>
/// <param name="kriptiranoBesedilo">Ciphertext</param>
/// <param name="aGeslo">Hash algorithm(SHA1)</param>
/// <param name="stInteracij">Number of iterations</param>
/// <param name="iVektor">Initialization vector</param>
/// <param name="velikostKljuca">Size of encryption key in bits(128,192,256)</param>
/// <param name="kljuc">Key</param>
/// <returns></returns>
public string dekriptiraj(string kriptiranoBesedilo, string aGeslo, int stInteracij, string iVektor, int velikostKljuca,string kljuc)
{
byte[] inciVektorBayt = Encoding.ASCII.GetBytes(iVektor);
byte[] kriptiranoBesediloBayt = Convert.FromBase64String(kriptiranoBesedilo);
//convert string into bytes
byte[] kljucBayt = new byte[kljuc.Length / 8];
kljucBayt = Encoding.ASCII.GetBytes(kljuc);
//create uninitialized rijndael encryption object
RijndaelManaged simetricniKljuc = new RijndaelManaged();
//it is reasonable to set encryption mode to cipher block chaining
simetricniKljuc.Mode = CipherMode.CBC;
ICryptoTransform deKriptiraj = simetricniKljuc.CreateDecryptor(kljucBayt, inciVektorBayt);
//define memory and crypto stream
MemoryStream podatkovniTokPomnilnika1 = new MemoryStream(kriptiranoBesediloBayt);
CryptoStream podatkovniTokKriptiranja1 = new CryptoStream(podatkovniTokPomnilnika1, deKriptiraj, CryptoStreamMode.Read);
//plain text in bytes
byte[] besedilo = new byte[kriptiranoBesediloBayt.Length];
//start decrypting
int stevecDektiptiranja = podatkovniTokKriptiranja1.Read(besedilo,0,besedilo.Length);
//close strams
podatkovniTokPomnilnika1.Close();
podatkovniTokKriptiranja1.Close();
//convert decrypted data into a string
string dekriptiranoBesedilo = ASCIIEncoding.ASCII.GetString(besedilo,0,stevecDektiptiranja);
//return decrypted data
return dekriptiranoBesedilo;
}
The problem arises in row:
int stevecDektiptiranja = podatkovniTokKriptiranja1.Read(besedilo,0,besedilo.Length);
Thanks in advance!

New Topic/Question
Reply




MultiQuote



|