vb.net中使用字符串作为关键字的Secure(ish)加密/解密算法 [英] Secure(ish) encryption/decryption algorithms in vb.net using a string as a key

查看:128
本文介绍了vb.net中使用字符串作为关键字的Secure(ish)加密/解密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在VB.net中使用另一个字符串作为密钥来加密/解密文本(字符串形式)?意思是说,如果一个用户使用该密钥加密,另一个用户需要使用相同的密钥解密?



我不是指公钥和私钥加密或任何类型的



有人可以帮助我为这些建立两个代码?



如果没有,第二好的方式是加密/解密数据没有公钥/私钥?



我想做一个简单的方法来安全地发送消息。



感谢您的帮助!

解决方案

如上述答案所述,对称算法(其中单个密钥为用于加密和解密)可以工作。我碰巧使用DES算法。该加密例程返回加密过程(并且解密具有输入)的输出,该输出是base64编码的字符串而不是字节数组(这是框架加密类的自然输出)。

 私钥()As Byte = {} 
Private IV()As Byte = {& H12,& H34,& H56,& ; H78& H90& HAB& HCD& HEF}
Private Const EncryptionKey As String =abcdefgh
公共函数解密(ByVal stringToDecrypt As String)As String
尝试
Dim inputByteArray(stringToDecrypt.Length)As Byte
key = System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey,8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(stringToDecrypt)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms,des.CreateDecryptor(key,IV),CryptoStreamMode.Write)
cs.Write (inputByteArray,0,inputByteArray.Length)
cs.FlushFinalBlock()
Dim编码作为System.Text.Encoding = System.Text.Encoding.UTF8
返回encoding.GetString(ms.ToArray())
捕获ex As Exception
'oops - 添加你的异常逻辑
结束尝试
结束函数

公共函数加密(ByVal stringToEncrypt As String)As String
尝试
键= System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey,8))
Dim des As New DESCryptoServiceProvider
Dim inputByteArray()As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
Dim Ms As New MemoryStream
Dim cs As New CryptoStream(ms,des.CreateEncryptor(key,IV),CryptoStreamMode.Write)
cs.Write(inputByteArray,0,inputByteArray.Length)
cs.FlushFinalBlock()
返回Convert.ToBase64String(ms.ToArray())
捕获ex作为异常
'oops - 添加您的异常逻辑
结束尝试
结束功能

已编辑添加:

这是我在该模块中的导入:

 导入系统
导入System.IO
导入System.Xml
导入System.Text
导入系统。 Security.Cryptography

DES密钥长度为56位(短于8字节或字符)。在大图中,这几天不被认为是非常安全的(请参阅关于维度的维基百科文章),但正如你所描述的安全的,也许没关系。如果你需要一个更安全的加密,你应该调查一个更安全的算法。



上述例程中的加密密钥是私有的常量EncryptionKey。将该值更改为所需的密钥。或者您可以实现自己的密钥管理(从文件输入,询问用户等)。



不知道为什么左边和转换将被破坏。 Left是Microsoft.VisualBasic.Strings的成员,Convert是System的成员。



我强烈建议您阅读Remus Rusanu链接的文章,以及与其相关的其他文章。他们将在框架类中为您提供加密背景。


Is there a way to encrypt/decrypt text (string form) in VB.net using another string as a key? Meaning, if one user encrypts using this key, the other user needs to decrypt using the same key?

I do NOT mean public and private key encryption or anything of the sort.

Can someone help me build two subs for these?

If not, what is the second best way to encrypt/decrypt data without public/private keys?

I want to make a simple way to send messages securely.

Thanks for the help!

解决方案

As described in the previous answer, a symmetric algorithm (where a single secret key is used to encrypt and decrypt) could work. I happen to have on hand a usage of the DES algorithm. This encrypt routine returns the output of the encrypting process (and the decrypt has as input) a base64 encoded string rather than a byte array (which is the 'natural' output of the framework encryption classes).

Private key() As Byte = {}
Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Private Const EncryptionKey As String = "abcdefgh"
Public Function Decrypt(ByVal stringToDecrypt As String) As String
    Try
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        key = System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey, 8))
        Dim des As New DESCryptoServiceProvider
        inputByteArray = Convert.FromBase64String(stringToDecrypt)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
        Return encoding.GetString(ms.ToArray())
    Catch ex As Exception
        'oops - add your exception logic
    End Try
End Function

Public Function Encrypt(ByVal stringToEncrypt As String) As String
    Try
        key = System.Text.Encoding.UTF8.GetBytes(Left(EncryptionKey, 8))
        Dim des As New DESCryptoServiceProvider
        Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Return Convert.ToBase64String(ms.ToArray())
    Catch ex As Exception
        'oops - add your exception logic
    End Try
End Function

Edited to add:
Here are the Imports that I have in that module:

Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography

A DES key is 56 bits in length (just short of 8 bytes or characters). In a "big picture", that's not considered very secure these days (see this Wikipedia article on key sizes), but as you described 'secure-ish', perhaps that's ok. If you do need a more secure encryption, you should investigate using one of the more secure algorithms.

The encryption key in the above routines is in the private constant EncryptionKey. Change that value to your desired key. Or you can implement your own key management (input from file, ask the user, etc).

Not sure why Left and Convert would be broke. Left is a member of Microsoft.VisualBasic.Strings and Convert is a member of System.

I highly recommend that you read the articles linked to by Remus Rusanu, as well as the further articles linked from those. They will provide you with much background on encryption in the framework classes.

这篇关于vb.net中使用字符串作为关键字的Secure(ish)加密/解密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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