指定的密钥不是此算法的有效大小。 [英] Specified key is not a valid size for this algorithm.

查看:142
本文介绍了指定的密钥不是此算法的有效大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码加密文件,但是当我输入密钥(例如:12345678)并构建错误时指定密钥不是此算法的有效大小。

这是我的代码:

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.

推荐答案

根据错误,很明显所提供的密钥长度不是所需的长度。您尚未共享传递给上述方法的密钥。看一下类似的问题/讨论:指定密钥不是此算法的有效大小 [ ^ ]
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[^]


64位或8个字节是唯一有效的长度。
64 bit or 8 bytes is the only valid length.


这篇关于指定的密钥不是此算法的有效大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆