- 浏览: 18190 次
- 性别:
- 来自: 上海
最新评论
什么是对称加密和非对称加密?
对称加密是指:有一个密钥,加密用它来加密,解密也需要用到它。因为加密解密都是用同一个密钥所以叫对称加密。
非对称加密是指:就是有 2 个密钥,一个是公钥,一个是私钥。私钥是自己的,不能随便给人,公钥随便给,无所谓。一般是别人用你的公钥加密,然后把密文给你,你用你的私钥解密,这样一样加密和解密不是同一个密钥,所以叫非对称。
对称和非对称的优劣?
对称加密有一个坏处只要拥有密钥的人都可以解密。
非对称的好处是假如没有私钥,别人是无法解密的,就算加密的那个人他把数据加密了他也无法解密,加密者把密文和公钥随便给那个人都无法解密。
什么是数字签名?
数字签名是指:用一段密码与原文进行加密,检测原文有没有被 篡改。意义就是这些数据与原文数据比对是否修改过,这个解释有点麻烦,一般是用自己的私钥对数据进行签名,然后用公钥去验证这个数据是否修改过
1、用 RSACryptoServiceProvider 加密解密
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
'1. 产生公匙和私匙,两个数据都要保存
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider
Dim privateKey As String = oRsa.ToXmlString(True)
Dim publicKey As String = oRsa.ToXmlString(False)
'2. 用公匙加密数据
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
'导入公匙
oRsaEncode.FromXmlString(publicKey)
'输出加密后的数据
Dim resultByte As Byte() = oRsaEncode.Encrypt(messageBytes, False)
'3. 用私匙解密数据
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入私匙
oRsaDecode.FromXmlString(privateKey)
'输出解密后的数据
messageBytes = oRsaDecode.Decrypt(resultByte, False)
'结果
Return Text.Encoding.UTF8.GetString(messageBytes)
2、用 RSACryptoServiceProvider 签名验签
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
'1. 产生公匙和私匙,两个数据都要保存
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider
Dim privateKey As String = oRsa.ToXmlString(True)
Dim publicKey As String = oRsa.ToXmlString(False)
'2. 用私钥匙签名数据
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
'导入私钥
oRsaEncode.FromXmlString(privateKey)
'输出签名后的数据
Dim resultByte As Byte() = oRsaEncode.SignData(messageBytes, 'SHA1')
'3. 用公钥验证数据
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入公钥
oRsaDecode.FromXmlString(publicKey)
'输出验证后的结果,是否验证成功
Return oRsaDecode.VerifyData(resultByte, 'SHA1', messageBytes)
3、用证书进行签名
因为一般证书的私钥是不可以导出的,所以用第 2 种方法导入私钥的来进行签名行不通。
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
Dim certificatePath As String = 'E:\Certificate.P12'
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, 'Import Password')
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
'对要签名的数据进行哈希
Dim sha1 As New Security.Cryptography.SHA1CryptoServiceProvider
Dim hashBytes As Byte() = sha1.ComputeHash(messageBytes)
'签名对象
Dim signe As New Security.Cryptography.RSAPKCS1SignatureFormatter
'设置签名用到的私钥
signe.SetKey(privateKey)
'设置签名算法
signe.SetHashAlgorithm('SHA1')
'返回结果
Dim reslutBytes As Byte() = signe.CreateSignature(hashBytes)
'3. 用公钥验证数据,与第2方法相同
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入公钥
oRsaDecode.FromXmlString(publicKey)
'输出验证后的结果,是否验证成功
Return oRsaDecode.VerifyData(resultByte, 'SHA1', messageBytes)
4、用证书加密解密
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
Dim certificatePath As String = 'E:\Certificate.P12'
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, 'Import Password')
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
'证书公钥加密
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
oRsaEncode.FromXmlString(publicKey)
Dim resultBytes As Byte() = oRsaEncode.Encrypt(messageBytes, False)
'证书私钥解密
Dim oRsaDecode As Security.Cryptography.RSACryptoServiceProvider = DirectCast(privateKey, Security.Cryptography.RSACryptoServiceProvider)
messageBytes = oRsaDecode.Decrypt(resultBytes, False)
'返回结果
Return Text.Encoding.UTF8.GetString(messageBytes)
5、用证书对文件加密解密
因为文件可能特别大 所以需要用流和 Buffer 的方式,如果把文件全部读到 Byte() 里进行加密,当文件很大,比如 5G,那全部读到 Byte() 里崩溃掉
''' 用证书加密文件
''' 证书路径
''' 证书导入密码
''' 需要加密的文件路径
''' 加密后的文件路径
'''
Public Sub Encrypt(ByVal certificatePath As String, ByVal certificatePassword As String, ByVal sourcePath As String, ByVal targetPath As String)
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, certificatePassword)
'Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider()
oRsa.FromXmlString(publicKey)
Dim sourceStream As New System.IO.FileStream(sourcePath, System.IO.FileMode.Open)
Dim targetStream As New System.IO.FileStream(targetPath, System.IO.FileMode.OpenOrCreate)
Dim blockSize As Integer = 0
If oRsa.KeySize = 1024 Then
blockSize = 16
Else
blockSize = 8
End If
Dim K As Integer = 1
While K > 0
Dim buffer As Byte() = New Byte(blockSize - 1) {}
K = sourceStream.Read(buffer, 0, buffer.Length)
If K > 0 Then
Dim buffer1 As Byte(), encryblock As Byte()
If blockSize = K Then
encryblock = oRsa.Encrypt(buffer, False)
targetStream.Write(encryblock, 0, encryblock.Length)
Else
buffer1 = New Byte(K - 1) {}
For i As Integer = 0 To K - 1
buffer1(i) = buffer(i)
Next
encryblock = oRsa.Encrypt(buffer1, False)
targetStream.Write(encryblock, 0, encryblock.Length)
End If
Else
sourceStream.Close()
targetStream.Close()
End If
End While
End Sub
''' 用证书解密文件
''' 证书路径
''' 证书导入密码
''' 需要解密的文件路径
''' 解密后的文件路径
'''
Public Sub Decrypt(ByVal certificatePath As String, ByVal certificatePassword As String, ByVal sourcePath As String, ByVal targetPath As String)
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, certificatePassword)
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
'Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
Dim oRsa As Security.Cryptography.RSACryptoServiceProvider = DirectCast(privateKey, Security.Cryptography.RSACryptoServiceProvider)
Dim sourceStream As New System.IO.FileStream(sourcePath, System.IO.FileMode.Open)
Dim targetStream As New System.IO.FileStream(targetPath, System.IO.FileMode.OpenOrCreate)
Dim blockSize As Integer = oRsa.KeySize / 8
Dim buffer1 As Byte(), encryblock As Byte()
Dim K As Integer = 1
While K > 0
Dim buffer As Byte() = New Byte(blockSize - 1) {}
K = sourceStream.Read(Buffer, 0, Buffer.Length)
If K > 0 Then
If blockSize = K Then
encryblock = oRsa.Decrypt(buffer, False)
targetStream.Write(encryblock, 0, encryblock.Length)
Else
buffer1 = New Byte(K - 1) {}
For i As Integer = 0 To K - 1
buffer1(i) = buffer(i)
Next
encryblock = oRsa.Decrypt(buffer1, False)
targetStream.Write(encryblock, 0, encryblock.Length)
End If
Else
sourceStream.Close()
targetStream.Close()
End If
End While
End Sub
6、用证书对文件进行签名验签
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
x509 = New X509Certificate2(Path, '12345678')
SignFile('1.txt', '11.txt')
VerifyFile('1.txt', '11.txt')
End Sub
Private Function VerifyFile(ByVal FileName As String, ByVal SignedFileName As String) As Boolean
Dim reslut As Boolean = True
Dim objread As New System.IO.StreamReader(FileName)
Dim objreadSigned As New System.IO.StreamReader(SignedFileName)
Dim VeryRsa As New RSACryptoServiceProvider()
VeryRsa.FromXmlString(x509.PublicKey.Key.ToXmlString(False))
Dim Inblocksize As Integer = 0
Dim Signedblocksize As Integer = 0
If VeryRsa.KeySize = 1024 Then
Inblocksize = 16
Else
Inblocksize = 8
End If
Signedblocksize = VeryRsa.KeySize / 8
Dim Closed As Boolean = True
Dim Buffer As Byte()
'原文缓存区
Dim InBuffer As Byte()
'原文缓存区
Dim Buffer1 As Byte()
'签名文件缓存区
While Closed
Buffer = Nothing
Buffer = New Byte(Inblocksize - 1) {}
Dim k As Integer = objread.BaseStream.Read(Buffer, 0, Buffer.Length)
If k > 0 Then
If Inblocksize = k Then
'读出来的长度和缓存区一样大
Buffer1 = New Byte(Signedblocksize - 1) {}
objreadSigned.BaseStream.Read(Buffer1, 0, Buffer1.Length)
reslut = VeryRsa.VerifyData(Buffer, 'SHA1', Buffer1)
If Not reslut Then
Closed = False
End If
Else
'意思是Buffer没满,只有k个字节,k字节后面全是空所以不需要验签
InBuffer = New Byte(k - 1) {}
For i As Integer = 0 To k - 1
InBuffer(i) = Buffer(i)
Next
Buffer1 = New Byte(Signedblocksize - 1) {}
objreadSigned.BaseStream.Read(Buffer1, 0, Buffer1.Length)
reslut = VeryRsa.VerifyData(InBuffer, 'SHA1', Buffer1)
If Not reslut Then
Closed = False
End If
End If
Else
'这里的意思是原文已经读完毕了,并且已经和签名文件对应验签成功,那么
'签名文件也必须读完毕了。
If objreadSigned.BaseStream.Position <> objreadSigned.BaseStream.Length Then
reslut = False
End If
objreadSigned.Close()
objread.Close()
Closed = False
End If
End While
Return reslut
End Function
Private Sub SignFile(ByVal InFileName As String, ByVal OutFileName As String)
Dim SignRsa As RSACryptoServiceProvider = DirectCast(x509.PrivateKey, RSACryptoServiceProvider)
Dim objread As New System.IO.StreamReader(InFileName)
Dim objwrite As New System.IO.StreamWriter(OutFileName, False)
Dim blocksize As Integer = 0
If SignRsa.KeySize = 1024 Then
blocksize = 16
Else
blocksize = 8
End If
Dim Closed As Boolean = True
Dim Buffer As Byte() = New Byte(blocksize - 1) {}
Dim buffer1 As Byte(), SignBytes As Byte()
While Closed
Dim k As Integer = objread.BaseStream.Read(Buffer, 0, Buffer.Length)
If k > 0 Then
If k = blocksize Then
SignBytes = SignRsa.SignData(Buffer, 'SHA1')
objwrite.BaseStream.Write(SignBytes, 0, SignBytes.Length)
Else
buffer1 = New Byte(k - 1) {}
For i As Integer = 0 To k - 1
buffer1(i) = Buffer(i)
Next
SignBytes = SignRsa.SignData(buffer1, 'SHA1')
objwrite.BaseStream.Write(SignBytes, 0, SignBytes.Length)
End If
Else
Closed = False
objread.Close()
objwrite.Close()
End If
End While
End Sub
原文来源:“激情燃烧的木炭” http://www.woodcoal.cn/
原文标题:非对称加密算法(包含证书)加密解密签名验签 - Visual Basic[激情燃烧的木炭]
原文地址:http://www.woodcoal.cn/technology/visual-basic/2009331-18230-14.html
对称加密是指:有一个密钥,加密用它来加密,解密也需要用到它。因为加密解密都是用同一个密钥所以叫对称加密。
非对称加密是指:就是有 2 个密钥,一个是公钥,一个是私钥。私钥是自己的,不能随便给人,公钥随便给,无所谓。一般是别人用你的公钥加密,然后把密文给你,你用你的私钥解密,这样一样加密和解密不是同一个密钥,所以叫非对称。
对称和非对称的优劣?
对称加密有一个坏处只要拥有密钥的人都可以解密。
非对称的好处是假如没有私钥,别人是无法解密的,就算加密的那个人他把数据加密了他也无法解密,加密者把密文和公钥随便给那个人都无法解密。
什么是数字签名?
数字签名是指:用一段密码与原文进行加密,检测原文有没有被 篡改。意义就是这些数据与原文数据比对是否修改过,这个解释有点麻烦,一般是用自己的私钥对数据进行签名,然后用公钥去验证这个数据是否修改过
1、用 RSACryptoServiceProvider 加密解密
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
'1. 产生公匙和私匙,两个数据都要保存
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider
Dim privateKey As String = oRsa.ToXmlString(True)
Dim publicKey As String = oRsa.ToXmlString(False)
'2. 用公匙加密数据
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
'导入公匙
oRsaEncode.FromXmlString(publicKey)
'输出加密后的数据
Dim resultByte As Byte() = oRsaEncode.Encrypt(messageBytes, False)
'3. 用私匙解密数据
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入私匙
oRsaDecode.FromXmlString(privateKey)
'输出解密后的数据
messageBytes = oRsaDecode.Decrypt(resultByte, False)
'结果
Return Text.Encoding.UTF8.GetString(messageBytes)
2、用 RSACryptoServiceProvider 签名验签
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
'1. 产生公匙和私匙,两个数据都要保存
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider
Dim privateKey As String = oRsa.ToXmlString(True)
Dim publicKey As String = oRsa.ToXmlString(False)
'2. 用私钥匙签名数据
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
'导入私钥
oRsaEncode.FromXmlString(privateKey)
'输出签名后的数据
Dim resultByte As Byte() = oRsaEncode.SignData(messageBytes, 'SHA1')
'3. 用公钥验证数据
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入公钥
oRsaDecode.FromXmlString(publicKey)
'输出验证后的结果,是否验证成功
Return oRsaDecode.VerifyData(resultByte, 'SHA1', messageBytes)
3、用证书进行签名
因为一般证书的私钥是不可以导出的,所以用第 2 种方法导入私钥的来进行签名行不通。
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
Dim certificatePath As String = 'E:\Certificate.P12'
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, 'Import Password')
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
'对要签名的数据进行哈希
Dim sha1 As New Security.Cryptography.SHA1CryptoServiceProvider
Dim hashBytes As Byte() = sha1.ComputeHash(messageBytes)
'签名对象
Dim signe As New Security.Cryptography.RSAPKCS1SignatureFormatter
'设置签名用到的私钥
signe.SetKey(privateKey)
'设置签名算法
signe.SetHashAlgorithm('SHA1')
'返回结果
Dim reslutBytes As Byte() = signe.CreateSignature(hashBytes)
'3. 用公钥验证数据,与第2方法相同
Dim oRsaDecode As New Security.Cryptography.RSACryptoServiceProvider
'导入公钥
oRsaDecode.FromXmlString(publicKey)
'输出验证后的结果,是否验证成功
Return oRsaDecode.VerifyData(resultByte, 'SHA1', messageBytes)
4、用证书加密解密
'要加密的数据
Dim messageBytes As Byte() = Text.Encoding.UTF8.GetBytes('激情燃烧的木炭[http://www.woodcoal.cn/]')
Dim certificatePath As String = 'E:\Certificate.P12'
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, 'Import Password')
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
'证书公钥加密
Dim oRsaEncode As New Security.Cryptography.RSACryptoServiceProvider
oRsaEncode.FromXmlString(publicKey)
Dim resultBytes As Byte() = oRsaEncode.Encrypt(messageBytes, False)
'证书私钥解密
Dim oRsaDecode As Security.Cryptography.RSACryptoServiceProvider = DirectCast(privateKey, Security.Cryptography.RSACryptoServiceProvider)
messageBytes = oRsaDecode.Decrypt(resultBytes, False)
'返回结果
Return Text.Encoding.UTF8.GetString(messageBytes)
5、用证书对文件加密解密
因为文件可能特别大 所以需要用流和 Buffer 的方式,如果把文件全部读到 Byte() 里进行加密,当文件很大,比如 5G,那全部读到 Byte() 里崩溃掉
''' 用证书加密文件
''' 证书路径
''' 证书导入密码
''' 需要加密的文件路径
''' 加密后的文件路径
'''
Public Sub Encrypt(ByVal certificatePath As String, ByVal certificatePassword As String, ByVal sourcePath As String, ByVal targetPath As String)
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, certificatePassword)
'Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
Dim oRsa As New Security.Cryptography.RSACryptoServiceProvider()
oRsa.FromXmlString(publicKey)
Dim sourceStream As New System.IO.FileStream(sourcePath, System.IO.FileMode.Open)
Dim targetStream As New System.IO.FileStream(targetPath, System.IO.FileMode.OpenOrCreate)
Dim blockSize As Integer = 0
If oRsa.KeySize = 1024 Then
blockSize = 16
Else
blockSize = 8
End If
Dim K As Integer = 1
While K > 0
Dim buffer As Byte() = New Byte(blockSize - 1) {}
K = sourceStream.Read(buffer, 0, buffer.Length)
If K > 0 Then
Dim buffer1 As Byte(), encryblock As Byte()
If blockSize = K Then
encryblock = oRsa.Encrypt(buffer, False)
targetStream.Write(encryblock, 0, encryblock.Length)
Else
buffer1 = New Byte(K - 1) {}
For i As Integer = 0 To K - 1
buffer1(i) = buffer(i)
Next
encryblock = oRsa.Encrypt(buffer1, False)
targetStream.Write(encryblock, 0, encryblock.Length)
End If
Else
sourceStream.Close()
targetStream.Close()
End If
End While
End Sub
''' 用证书解密文件
''' 证书路径
''' 证书导入密码
''' 需要解密的文件路径
''' 解密后的文件路径
'''
Public Sub Decrypt(ByVal certificatePath As String, ByVal certificatePassword As String, ByVal sourcePath As String, ByVal targetPath As String)
'导入证书
Dim x509 As New Security.Cryptography.X509Certificates.X509Certificate2(certificatePath, certificatePassword)
Dim privateKey As Security.Cryptography.RSACryptoServiceProvider = x509.PrivateKey
'Dim publicKey As String = x509.PublicKey.Key.ToXmlString(False)
Dim oRsa As Security.Cryptography.RSACryptoServiceProvider = DirectCast(privateKey, Security.Cryptography.RSACryptoServiceProvider)
Dim sourceStream As New System.IO.FileStream(sourcePath, System.IO.FileMode.Open)
Dim targetStream As New System.IO.FileStream(targetPath, System.IO.FileMode.OpenOrCreate)
Dim blockSize As Integer = oRsa.KeySize / 8
Dim buffer1 As Byte(), encryblock As Byte()
Dim K As Integer = 1
While K > 0
Dim buffer As Byte() = New Byte(blockSize - 1) {}
K = sourceStream.Read(Buffer, 0, Buffer.Length)
If K > 0 Then
If blockSize = K Then
encryblock = oRsa.Decrypt(buffer, False)
targetStream.Write(encryblock, 0, encryblock.Length)
Else
buffer1 = New Byte(K - 1) {}
For i As Integer = 0 To K - 1
buffer1(i) = buffer(i)
Next
encryblock = oRsa.Decrypt(buffer1, False)
targetStream.Write(encryblock, 0, encryblock.Length)
End If
Else
sourceStream.Close()
targetStream.Close()
End If
End While
End Sub
6、用证书对文件进行签名验签
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
x509 = New X509Certificate2(Path, '12345678')
SignFile('1.txt', '11.txt')
VerifyFile('1.txt', '11.txt')
End Sub
Private Function VerifyFile(ByVal FileName As String, ByVal SignedFileName As String) As Boolean
Dim reslut As Boolean = True
Dim objread As New System.IO.StreamReader(FileName)
Dim objreadSigned As New System.IO.StreamReader(SignedFileName)
Dim VeryRsa As New RSACryptoServiceProvider()
VeryRsa.FromXmlString(x509.PublicKey.Key.ToXmlString(False))
Dim Inblocksize As Integer = 0
Dim Signedblocksize As Integer = 0
If VeryRsa.KeySize = 1024 Then
Inblocksize = 16
Else
Inblocksize = 8
End If
Signedblocksize = VeryRsa.KeySize / 8
Dim Closed As Boolean = True
Dim Buffer As Byte()
'原文缓存区
Dim InBuffer As Byte()
'原文缓存区
Dim Buffer1 As Byte()
'签名文件缓存区
While Closed
Buffer = Nothing
Buffer = New Byte(Inblocksize - 1) {}
Dim k As Integer = objread.BaseStream.Read(Buffer, 0, Buffer.Length)
If k > 0 Then
If Inblocksize = k Then
'读出来的长度和缓存区一样大
Buffer1 = New Byte(Signedblocksize - 1) {}
objreadSigned.BaseStream.Read(Buffer1, 0, Buffer1.Length)
reslut = VeryRsa.VerifyData(Buffer, 'SHA1', Buffer1)
If Not reslut Then
Closed = False
End If
Else
'意思是Buffer没满,只有k个字节,k字节后面全是空所以不需要验签
InBuffer = New Byte(k - 1) {}
For i As Integer = 0 To k - 1
InBuffer(i) = Buffer(i)
Next
Buffer1 = New Byte(Signedblocksize - 1) {}
objreadSigned.BaseStream.Read(Buffer1, 0, Buffer1.Length)
reslut = VeryRsa.VerifyData(InBuffer, 'SHA1', Buffer1)
If Not reslut Then
Closed = False
End If
End If
Else
'这里的意思是原文已经读完毕了,并且已经和签名文件对应验签成功,那么
'签名文件也必须读完毕了。
If objreadSigned.BaseStream.Position <> objreadSigned.BaseStream.Length Then
reslut = False
End If
objreadSigned.Close()
objread.Close()
Closed = False
End If
End While
Return reslut
End Function
Private Sub SignFile(ByVal InFileName As String, ByVal OutFileName As String)
Dim SignRsa As RSACryptoServiceProvider = DirectCast(x509.PrivateKey, RSACryptoServiceProvider)
Dim objread As New System.IO.StreamReader(InFileName)
Dim objwrite As New System.IO.StreamWriter(OutFileName, False)
Dim blocksize As Integer = 0
If SignRsa.KeySize = 1024 Then
blocksize = 16
Else
blocksize = 8
End If
Dim Closed As Boolean = True
Dim Buffer As Byte() = New Byte(blocksize - 1) {}
Dim buffer1 As Byte(), SignBytes As Byte()
While Closed
Dim k As Integer = objread.BaseStream.Read(Buffer, 0, Buffer.Length)
If k > 0 Then
If k = blocksize Then
SignBytes = SignRsa.SignData(Buffer, 'SHA1')
objwrite.BaseStream.Write(SignBytes, 0, SignBytes.Length)
Else
buffer1 = New Byte(k - 1) {}
For i As Integer = 0 To k - 1
buffer1(i) = Buffer(i)
Next
SignBytes = SignRsa.SignData(buffer1, 'SHA1')
objwrite.BaseStream.Write(SignBytes, 0, SignBytes.Length)
End If
Else
Closed = False
objread.Close()
objwrite.Close()
End If
End While
End Sub
原文来源:“激情燃烧的木炭” http://www.woodcoal.cn/
原文标题:非对称加密算法(包含证书)加密解密签名验签 - Visual Basic[激情燃烧的木炭]
原文地址:http://www.woodcoal.cn/technology/visual-basic/2009331-18230-14.html
发表评论
-
xml和java bean的转换
2011-12-30 04:47 1333最近项目中用到了xml和java bean的转换 用 ... -
apache session stick上线部署
2011-11-10 18:01 1759一、 apache session stick ... -
Tomcat6配置使用SSL双向认证(使用openssl生成证书)
2011-11-10 17:58 162一:生成CA证书 目前不使用第三方权威机构的CA来认证,自己充 ... -
concurrent ThreadPoolExecutor的参数说明
2011-11-10 09:58 1581public ThreadPoolExecutor(int c ...
相关推荐
SM2算法可以生成密钥和公钥,支持SM2的签名和验签;AES加密解密,支持各种常用的堆成非对称的加密算法的加密解密和验签
本文将详细讲解基于MIRACL大数库实现的SM2加密解密与签名验签工具在Delphi 7中的应用。SM2是中国国家商用密码算法标准之一,它是一种基于椭圆曲线密码学(ECC)的非对称加密算法,主要用于确保数据传输的安全性和...
本案例聚焦于"C#与Java平台RSA加密解密签名验签互通"的问题,这涉及到两个主要的技术点:RSA加密算法和跨平台兼容性。下面将详细阐述这两个知识点。 首先,RSA是一种非对称加密算法,由Ron Rivest、Adi Shamir和...
在这个项目中,开发者使用C语言和Visual Studio 2010创建了三个工具:ECC密钥生成器、ECC加密解密工具以及ECC签名验签工具,这些工具都基于OpenSSL库进行关键操作。 首先,ECC密钥生成器是用于创建ECC密钥对的核心...
原创 python国密算法SM2 + 加解密及签名验签 + 调用jar包demo 新鲜出炉,亲测可用。疑问可留言咨询。
在Android开发中,数据安全是至关重要的,尤其是在...通过以上步骤,您可以在Android应用中实现RSA加密解密和签名验签功能,确保数据的安全传输和验证。记住,安全无小事,合理的加密策略是保障应用安全的重要一环。
RSA是一种广泛使用的公钥加密算法,它结合了非对称加密和数字签名技术,确保数据的机密性和完整性。本文将深入探讨如何使用OpenSSL库在C++中实现RSA加密、解密以及签名和验签功能,特别关注SHA256WithRSA这一安全...
6. 压缩包中的`signature`文件可能包含的是一个示例程序,演示了如何在Java中使用RSA进行签名、验签、加密和解密。这个程序可能会包括以下关键部分: - 导入必要的安全库 - 创建并初始化`KeyPairGenerator` - ...
总结一下,C#中处理RSA密钥文件,如pfx和cer,以及进行签名、验签、加密和解密,主要依赖于`System.Security.Cryptography`命名空间。此外,为了实现C#和Java之间的公钥私钥互转,我们需要理解不同的密钥表示形式并...
在C#中实现RSA加密解密和签名验签,我们可以利用.NET框架提供的System.Security.Cryptography命名空间中的相关类。 1. RSA加密原理: RSA基于数论中的大数因子分解难题,即给定一个大合数N,找到它的两个素数因子p...
国密算法功能 ... SM2 加密解密、公钥私钥生成、签名与验签; 2. SM4 加密解密; 3. SM3加密 4. 代码实现、调用案例源码 代码经过本人测试通过,调用BouncyCastle.Crypto.dll的全部实现代码,源码分享。
Java实现SM2、SM3和SM4加密解密签名验签工具包是针对我国自主设计的密码算法标准的编程实践。这些算法在信息安全领域扮演着关键角色,主要用于数据的加密、完整性保护以及身份验证。 首先,SM2算法是一种基于椭圆...
这款RSA签名验签工具的Windows版本V1.3可能包含了以下特性: 1. 用户友好的界面:提供直观的操作步骤,使用户无需深入理解复杂的加密原理即可使用。 2. 密钥管理:支持密钥对的生成、导出和导入,以及密钥的安全...
其中,SM2算法是基于椭圆曲线密码学(ECC)的一种公钥加密算法,广泛应用于数字证书、数据加解密和数字签名等领域。本文将详细探讨SM2算法的原理、使用场景,以及在前端js中实现加解密和签名验签的方法,同时参考sm-...
实现了sm2加密功能,sm2解密功能,sm2签名功能,sm2验签功能,SM9算法加解密功能,SM9签名验签功能
**PGP 加解密及签名验签示例** PGP(Pretty Good Privacy)是一种广泛使用的加密软件,主要用于电子邮件、文件的加密以及数字签名。它基于公开密钥加密算法,如RSA和IDEA,确保数据在传输过程中的安全性和完整性。...
RSA加密演算法是一种非对称加密演算法。在公开密钥加密和电子商业中RSA被广泛使用。 典型的应用 1. 苹果App签名, iOS App 签名的原理; 2. 支付宝签名验证 ; 2. HTTPS 加密连接; 3. 程序直接用RSA+AES加密通信 ...
用于python与java之间,使用国密算法SM2加解密及签名验签
RSA算法是一种非对称加密算法,由Ron Rivest、Adi Shamir和Leonard Adleman在1977年提出,是目前最广泛使用的公钥加密技术之一。该算法基于大数因子分解的困难性,即对于一个大的合数,找到它的质因数分解非常困难,...