指定的初始化向量(IV)与此算法的块大小不匹配(Specified initialization vector(IV) does not match the block size for this algorithm)
我正在尝试使用c#创建加密系统。 这是加密的代码。
public static void EncryptFile(string inFile, string outFile, string @inkey) { try { UnicodeEncoding ue = new UnicodeEncoding(); byte[] key = ue.GetBytes(inkey); FileStream fsEncrypt = new FileStream(outFile, FileMode.Create); RijndaelManaged rmCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write); FileStream fsIn = new FileStream(inFile, FileMode.Open); int data; while((data=fsIn.ReadByte()) != 1){ cs.WriteByte((byte)data); } fsIn.Close(); cs.Close(); fsEncrypt.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message, "Fail to encrypt", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
现在,这个代码每次运行时抛出异常,说
指定的初始化向量(IV)与此算法的块大小不匹配
我已阅读其他有关此问题的讨论,并说字节数存在问题(传递给此函数的密钥长度为255)。 但我已经尝试使密钥只有16个字节仍然无法正常工作。
经过一些故障排除我发现这部分:
CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
抛出异常。 我不知道为什么。 有人可以帮忙吗?
I'm trying to make an encryption system with c#. This is the code for the encryption.
public static void EncryptFile(string inFile, string outFile, string @inkey) { try { UnicodeEncoding ue = new UnicodeEncoding(); byte[] key = ue.GetBytes(inkey); FileStream fsEncrypt = new FileStream(outFile, FileMode.Create); RijndaelManaged rmCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write); FileStream fsIn = new FileStream(inFile, FileMode.Open); int data; while((data=fsIn.ReadByte()) != 1){ cs.WriteByte((byte)data); } fsIn.Close(); cs.Close(); fsEncrypt.Close(); } catch(Exception ex) { MessageBox.Show(ex.Message, "Fail to encrypt", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
Now, this code throws exception every time I run it, says
Specified initialization vector(IV) does not match the block size for this algorithm
I have read on other discussion about this, saying that there is a problem with the number of bytes (my key length passed into this function is 255). But I have tried making the key only 16 bytes and still not working.
After some troubleshooting I found out that this part:
CryptoStream cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
throws the exception. I have no idea why. Anyone can help?
原文:https://stackoverflow.com/questions/15019236
最满意答案
Peter Wagenet的解决方案给了我一个缺失的拼图(
:type => nil
),它允许我在一行中完成:map.resources :posts, :path_names => { :new => 'new/:type' }, :requirements => { :type => nil }
当然,我仍然需要进入控制器并进行修复,以便从
:new
动作渲染new_index.html.erb
。(好吧,我想这不再是一行了。)
new_post_path # => '/posts/new/' new_post_path(:type => 'quip') # => '/posts/new/quip' new_post_path('quip') # => '/posts/new/quip'
Peter Wagenet's solution gave me the missing piece of the puzzle (
:type => nil
) which allows me to do it in one line:map.resources :posts, :path_names => { :new => 'new/:type' }, :requirements => { :type => nil }
Of course I still had to go into the controller and make a fix so it renders
new_index.html.erb
from the:new
action.(Well, I guess it's not one line anymore.)
new_post_path # => '/posts/new/' new_post_path(:type => 'quip') # => '/posts/new/quip' new_post_path('quip') # => '/posts/new/quip'
相关问答
更多-
没有路径名称,因为您没有指定路径名称。 如果您提供自定义路由而不是使用resources ,则需要使用:as来提供路径名: match '/new_user' => 'home#new_user', via: :get, as: :new_user 你也应该使用get而不是match... via: :get : get '/new_user' => 'home#new_user', as: :new_user 但是,根据您的路线集合,您最好的选择是继续使用resources ,但:only通过:onl ...
-
你可以这样写你的路线: resources :products, path: 'items' 这将使用ProductsController生成/items使用product_*命名助手的路径。 看看这部分的路由指南 。 You can write your route like this: resources :products, path: 'items' This will generate /items routes with product_* named helpers, using Prod ...
-
在@babar帮助@babar ,我们设法用小黑客修复错误: #config/routes.rb get "new", to: "storyboards#new" resources :storyboards, path: "" do resources :stories end With help from @babar, we managed to fix the error with a small hack: #config/routes.rb get "new", to: "storyb ...
-
Rails - 是否可以使用params重定向到自定义路径?(Rails - is it possible to redirect to a custom path with params?)[2021-08-10]
它工作,但它呈现'/ users'而不是'/ signup' 这是正常的行为。 /signup页面是UsersController的new操作的结果。 此页面包含表单。 提交此表单后,数据将从同一个控制器create操作,但是通过POST方法。 如果验证失败,控制器将呈现:new模板,因为您记得create操作有/users链接。 因此,您将看到:new /users链接中的:new模板。 这是UsersController路线图: GET /users(.:format) ... -
rails关注的自定义路径(rails custom path on concerns)[2023-04-29]
最后我想通了 concern :search do scope '/search', as: :search do get '/', to: 'users#search' get '/schedule/:id', to: 'schedules#user', as: :schedule end end concerns :search scope '/dashboard', as: :dashboard do concerns :search end 这是我的道路助手 s ... -
正如我们在这里看到的,我们可以使用Rails范围并为“注册”指定控制器。 像这样的东西: scope :settings do devise_scope :user do get '/profile' => 'devise/registrations#edit' end end As we can see here, we can use Rails scopes and specify a controller for 'registration', for example. Something like ...
-
如何获取rails路径中的路径(How to get path in rails routes)[2022-03-09]
您尚未设置路线名称。 这应该是诀窍: match 'proposals/:id/forkIt', :to => 'proposals#forkIt', :as => 'forkIt_proposal' 或稍微简洁: match 'proposals/:id/forkIt' => 'proposals#forkIt', :as => 'forkIt_proposal' You haven't set the route name. This should do the trick: match 'prop ... -
Rails自定义路由更改404资产导致的资产路径(Rails Custom Routes Changes Assets Paths Resulting In 404 For Assets)[2023-03-01]
使用相应的标记从布局文件中调用资产。 确保您的lcd_buyback控制器使用正确的布局文件。 在app / views / application.html.erb中,您会发现: <%= stylesheet_link_tag 'application' %> 在app / assets / stylesheets / application.css中,您将找到该文件以及它导入的所有其他样式表,如下例所示: *= require_self *= require bootstrap *= requi ... -
可以将自定义路径附加到Rails路由的“新”路径吗?(Can a custom path be appended to the 'new' path for Rails routes?)[2024-01-26]
Peter Wagenet的解决方案给了我一个缺失的拼图( :type => nil ),它允许我在一行中完成: map.resources :posts, :path_names => { :new => 'new/:type' }, :requirements => { :type => nil } 当然,我仍然需要进入控制器并进行修复,以便从:new动作渲染new_index.html.erb 。 (好吧,我想这不再是一行了。) new_post_path ... -
我认为你正在以不应该使用的方式使用to_param 。 如果你有一个to_param方法,那么也应该有一个from_param方法作为一个finder,它应该总是映射到一个令牌而不是一个路径。 所以即"#{news_category.slug}-#{id}-#{slug}"而不是"#{news_category.slug}/#{id}/#{slug}" 。 如果你不想按路径段分开,你需要把它放到你的news_path调用中,如下所示: news_path(news, :cat => news_categor ...