Click here to Skip to main content
11,477,625 members (73,963 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
I use this code to encrypt a file but when i input the key (example :12345678) and building an error "Specified key is not a valid size for this algorithm."
This is my code:
public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
        {
            FileStream fsInput = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);
            FileStream fsEncrypted = new FileStream                     (sOutputFilename,FileMode.Create,FileAccess.Write);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted,desencrypt,CryptoStreamMode.Write);
            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
        }
How to solve this problem?
Sorry if my english is not good.
Posted 24-Feb-13 3:21am
Edited 24-Feb-13 3:27am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Based on the error, it's clear that the length of the key provided is not of desired length. You have not shared your key that you pass to method above. Have a look at the similar question/discussion: Specified key is not a valid size for this algorithm[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

64 bit or 8 bytes is the only valid length.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 274
1 Richard Deeming 245
2 _duDE_ 175
3 Tomas Takac 150
4 Sascha Lefèvre 145
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,256
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 14 Dec 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100