以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]是否能够使用密钥对,对文件进行加解密  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=71964)

--  作者:jspta
--  发布时间:2015/7/21 14:49:00
--  [求助]是否能够使用密钥对,对文件进行加解密
能否使用PGP生成的公钥,对文件进行加密,不是字符串。
类似 RSACryptoServiceProvider这个类?求教

--  作者:大红袍
--  发布时间:2015/7/21 15:03:00
--  
 foxtable 不是有加密函数么 http://www.foxtable.com/help/topics/1346.htm
--  作者:大红袍
--  发布时间:2015/7/21 15:05:00
--  
RSACryptoServiceProvider 类,不是已经提供了生成方法了么

--  作者:jspta
--  发布时间:2015/7/21 15:13:00
--  
以下是引用大红袍在2015/7/21 15:05:00的发言:
RSACryptoServiceProvider 类,不是已经提供了生成方法了么
试了好久没成功。一个简单的场景
有个密钥,在C:\\pgp\\A1.asc 
需要加密的文件,在C:\\pgp\\test.txt
输出文件,在C:\\pgp\\test.txt.pgp
用这个类如何操作?


--  作者:大红袍
--  发布时间:2015/7/21 16:21:00
--  

mark 公钥 私钥

 

1、生成独一无二的密钥

 

Dim rsa As New System.Security.Cryptography.RSACryptoServiceProvider()
Using writer As New IO.StreamWriter("d:\\test\\PrivateKey.xml")
writer.WriteLine(rsa.ToXmlString(True))
End Using
Using writer As New IO.StreamWriter("d:\\test\\PublicKey.xml")
writer.WriteLine(rsa.ToXmlString(False))
End Using

 

2、对文件进行加密

 

\'读取文件
Dim file As new IO.FileStream("d:\\test\\test.txt", IO.FileMode.Open)
Dim data(file.Length-1) As Byte
file.Read(data, 0, data.Length)
file.Close ()
\'加密
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\\test\\PublicKey.xml")
Dim publickey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(publickey)
Dim AOutput As Byte() = oRSA.Encrypt(data ,False)
\'生成加密文件
file = new IO.FileStream("d:\\test\\test加密.txt", IO.FileMode.Create)
file.Write(AOutput, 0, AOutput.Length)
file.Flush()
file.Close()

 

3、对加密文件进行加密

 


\'读取文件
Dim file As new IO.FileStream("d:\\test\\test加密.txt", IO.FileMode.Open)
Dim data(file.Length-1) As Byte
file.Read(data, 0, data.Length)
file.Close ()
\'解密
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\\test\\PrivateKey.xml")
Dim PrivateKey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(PrivateKey)
Dim AInput As Byte() = oRSA.Decrypt(data ,False)
\'生成加密文件
file = new IO.FileStream("d:\\test\\test解密.txt", IO.FileMode.Create)
file.Write(AInput, 0, AInput.Length)
file.Flush()
file.Close()


--  作者:jspta
--  发布时间:2015/7/24 10:52:00
--  
这个方法好像只能加密少量字节的文件?我加密一个10几KB的EXCEL,就发生错误了。

那我如何使用PGP desk生成的key进行加密?

--  作者:大红袍
--  发布时间:2015/7/24 11:05:00
--  

 我是一次性获取,大文件的话,一段一段的获取、加密、写入


--  作者:大红袍
--  发布时间:2015/7/24 12:06:00
--  

分段加密处理

 

\'获取key
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\\test\\PublicKey.xml")
Dim publickey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(publickey)

\'读取和写入文件文件
Dim read As new IO.FileStream("d:\\test\\test.txt", IO.FileMode.Open)
Dim maxLength As Integer = oRSA.KeySize/8-11
Dim data(maxlength-1) As Byte
Dim write As new IO.FileStream("d:\\test\\test加密.txt", IO.FileMode.Create)
Dim dataSize As Integer = read.Read(data, 0, data.Length)
Do While dataSize = maxLength
    Dim AOutput As Byte() = oRSA.Encrypt(data ,False)
    write.Write(AOutput, 0, AOutput.Length)
    write.Flush()
    dataSize = read.Read(data, 0, data.Length)
    If dataSize < maxLength
        \'最后一次的情况
        Dim lsData(dataSize-1) As Byte
        Array.Copy(data, 0, lsData, 0, dataSize)
        Dim lsAOutput As Byte() = oRSA.Encrypt(lsData,False)
        write.Write(lsAOutput, 0, lsAOutput.Length)
        write.Flush()
    End If
Loop

read.Close ()
write.Close()

 

分段解密处理

 

\'获取key
Dim oRSA As New System.Security.Cryptography.RSACryptoServiceProvider()
Dim sr As New IO.StreamReader("d:\\test\\PrivateKey.xml")
Dim PrivateKey = sr.ReadToEnd
sr.Close()
oRSA.FromXmlString(PrivateKey)

\'读取和写入文件文件
Dim read As new IO.FileStream("d:\\test\\test加密.txt", IO.FileMode.Open)
Dim data(oRSA.KeySize/8-1) As Byte
Dim write As new IO.FileStream("d:\\test\\test解密.txt", IO.FileMode.Create)

Do While read.Read(data, 0, data.Length) > 0
    Dim AInput As Byte() = oRSA.Decrypt(data ,False)
    write.Write(AInput, 0, AInput.Length)
    write.Flush()
Loop
read.Close ()
write.Close()


--  作者:jspta
--  发布时间:2015/7/24 14:55:00
--  
非常感谢!
那有没有办法加解密如下格式的key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: PGP Desktop 9.6.3 (Build 3017) - not licensed for commercial use:


-----END PGP PUBLIC KEY BLOCK-----

--  作者:大红袍
--  发布时间:2015/7/24 15:43:00
--  
 9楼什么意思?不理解。