ASP VBSCRIPT VBA RSA 公钥加密 私钥解密 私钥签名 公钥验签

<% 
'@title: Class_Crypt_Rsa
'@author: ekede.com
'@date: 2020-10-28
'@description: RSA 公钥加密->私钥解密 , 私钥签名->公钥验签
Class Class_Crypt_Rsa
		Private TAsc,objRsa
		Private PrivateKey_,PublicKey_
        '@PrivateKey: Your personal private key.  Keep this hidden. Need C# format.
		Public Property Get PrivateKey
			PrivateKey = PrivateKey_
		End Property
		Public Property Let PrivateKey(Value)
			PrivateKey_ = Value
			objRsa.FromXmlString (PrivateKey_)
            PublicKey_ = objRsa.ToXmlString(False)
		End Property
        '@PublicKey: Key for others to encrypt data with.
		Public Property Get PublicKey
			PublicKey = PublicKey_
		End Property
		Public Property Let PublicKey(Value)
			PublicKey_ = Value
			objRsa.FromXmlString (PublicKey_)
		End Property
		Private Sub Class_Initialize()
			Set TAsc = Server.CreateObject("System.Text.UTF8Encoding")
			Set objRsa = Server.CreateObject("System.Security.Cryptography.RSACryptoServiceProvider")
			CreateKey()
		End Sub
		Private Sub Class_Terminate()
			Set objRsa = Nothing
			Set TAsc = Nothing
		End Sub
		Public Sub CreateKey()
			PrivateKey_ = objRsa.ToXmlString(True)
			PublicKey_ = objRsa.ToXmlString(False)
		End Sub
		'@Encrypt(ByRef Str): 公钥加密
		Public Function Encrypt(ByRef Str)
			Dim Bytes
			Bytes = TAsc.GetBytes_4(Str)
			Encrypt = Bytes2Base64(RsaEncrypt((Bytes)))
		End Function
		Private Function RsaEncrypt(ByRef Bytes)
			RsaEncrypt = objRsa.Encrypt((Bytes),False)
		End Function
		'@Decrypt(ByRef Bytes): 私钥解密
		Public Function Decrypt(ByRef Str)
			Dim Bytes
			Bytes=RsaDecrypt(Base642Bytes(Str))
			Decrypt = TAsc.GetString((Bytes))
		End Function
		Private Function RsaDecrypt(ByRef Bytes)
			 RsaDecrypt = objRsa.Decrypt((Bytes), False)
		End Function
		'@SignData(ByRef Str,ByRef Hash): 私钥签名 Hash(MD5 SHA1 SHA256)
		Public Function SignData(ByRef Str,ByRef Hash)
			Dim Bytes
			Bytes = TAsc.GetBytes_4(Str)
			SignData = Bytes2Base64(SignHash(Bytes, Hash))
		End Function
		Private Function SignHash(ByRef Bytes,ByRef Hash)
			Dim MapNameToOID
			If Hash="MD5" Then
				MapNameToOID = "1.2.840.113549.2.5"
				Bytes = Md5(Bytes)
				SignHash = objRsa.SignHash((Bytes),MapNameToOID)
			End If
			If Hash="SHA1" Then
				MapNameToOID = "1.3.14.3.2.26"
				Bytes = SHA1(Bytes)
				SignHash = objRsa.SignHash((Bytes),MapNameToOID)
			End If
			If Hash="SHA256" Then
				MapNameToOID = "2.16.840.1.101.3.4.2.1"
				Bytes = SHA256(Bytes)
				SignHash = objRsa.SignHash((Bytes),MapNameToOID)
			End If
		End Function
		'@VerifyData(ByRef str,ByRef Hash,ByRef StrSign): 公钥验签
		Public Function VerifyData(ByRef str,ByRef Hash,ByRef StrSign)
			Dim Bytes,BytesSign
			Bytes = TAsc.GetBytes_4(Str)
			BytesSign = Base642Bytes(StrSign)
			VerifyData = objRsa.VerifyData((Bytes),Hash,(BytesSign))
		End Function
		'Hash
		Public Function Md5(ByRef Bytes)
			Dim En
			Set En = Server.CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
			Md5 = En.ComputeHash_2((Bytes))
			Set En = Nothing
		End Function
		Public Function SHA1(ByRef Bytes)
			Dim En
			Set En = Server.CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
			SHA1 = En.ComputeHash_2((Bytes))
			Set En = Nothing
		End Function
		Public Function SHA256(ByRef Bytes)
			Dim En
			Set En = Server.CreateObject("System.Security.Cryptography.SHA256CryptoServiceProvider")
			SHA256 = En.ComputeHash_2((Bytes))
			Set En = Nothing
		End Function
		'Base64
		Public Function Base642Bytes(str)
			Dim objXML, objXMLNode
			Set objXML = Server.CreateObject("msxml2.domdocument")
			Set objXMLNode = objXML.createelement("b64")
				objXMLNode.datatype = "bin.base64"
				objXMLNode.text = str
				Base642Bytes = objXMLNode.nodetypedvalue
			Set objXMLNode = Nothing
			Set objXML = Nothing
		End Function
		Public Function Bytes2Base64(bytes)
			Dim objXML, objXMLNode
			Set objXML = Server.CreateObject("msxml2.domdocument")
			Set objXMLNode = objXML.createelement("b64")
				objXMLNode.datatype = "bin.base64"
				objXMLNode.nodetypedvalue = bytes
				Bytes2Base64 = objXMLNode.text
			Set objXMLNode = Nothing
			Set objXML = Nothing
		End Function
End Class
%> 

使用

privatekey_pem="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC7wJL6+hjchXCbZCMkWDi7JThNf8f9wK4b7xSAavSqy9fo5SfEq7xgbXyEoYH/vL+cHCVzSZgfw9KQOZPZIZHnQszPZ7+vvIGS9rOXKS6XTOpVPAkcA6u1cNUrxBkwf+8XNE9mu+vVJvAIw+snlkZG198+ZP/sxWTzJZCj1eUCrYAHPrZrHA7UULpNlzc+mYT7ymvXz4CfMRqLRMKOrc1oLbxBavcsYkM798a1P9tbPS4Gtg+EIDF5jJawOiSJfQ1TSLFQHY+6Uaq+zO2OvFlhA+xkgiBnWUzP/jeut8caSu2Y54JLbn5T9uXN9lJEXpYNUiFPAErVwVFvi1WB3XTvAgMBAAECggEBALEYV0tmxjaTo4DfNoqcsH5OAEqRohnHOjNdEvCCcl/8QJ8ML7PB7cDi5RXRpeaMqgvdPLH/E/+6XQ3vUXb4xD/n8XodOWDRJUNzcji9/pV2Vn6pT0peaAOP93L92Gi389TmYZLc5Pk8biNGcbP4ejdufcPDzucN1kfHAiSXqBkZ4G/Fp8ImaG9EY2KdZ65cDUHPbx786oI4U/UYcQ2BYd5tjH0A2WVXna1Ok6Qz51gS5h8pen2ga24FZn5IuGgm9ZXVRjJXH16bmLz9Bxj7qHVmkyAUUwNQelvGnmpF0JKfPvs5yhyavMPqEnAxwwv3pGkNYnYFbvX7z387mWy/9jECgYEA9C9bAbM87egP+dLaA8IQA2lJEBG6b5pJidw40lm0E2Ey1v2NMCEhd3stEU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PSl5oA4/3cv3YaLfz1OZhktzk+TxuI0Zxs/h6N259w8PO5w3WR8cCJJeoGRngb8WnwiZob0RjYp1nrlwNQc9vHvzqgjhT9RhxDYFh3m2MfQDZZVedrU6TpDPnWBLmHyl6faBrbgVmfki4aCb1ldVGMlcfXpuYvP0HGPuodWaTIBRTA1B6W8aeakXQkp8++znKHJq8w+oScDHDC/ekaQ1idgVu9fvPfzuZbL/2MQ==
" Set r1= New Class_Crypt_Rsa r1.Privatekey=privatekey_csharp '设置私钥 'r1.Publickey="xxxx" '设置公钥 Publickey=r1.Publickey '也可通过私钥获得公钥 a="Hello WTS" b=r1.Encrypt(a) '加密 c=r1.Decrypt(b) '解密 d=r1.SignData(a,"SHA1") '签名支持SHA1 SHA256 MD5 e=r1.VerifyData(a,"SHA1",d) '验签 Set r1=Nothing

 github源: https://github.com/ekede/WTS-Classic-ASP-MVC-Framework/blob/master/inc/class/crypt/rsa.asp

关于使用环境,比较尴尬,本人windows2003,2008服务器下会有报错,另外win7和建虚拟机跑2008又可以,.vbs直接运行都是可以的,猜测跟IIS配置或权限有关系,个人能力有限,具体也搞不明白.

使用c#特有地秘钥格式,pem格式使用前要先想办法转成c# xml格式。另外为了方便做了个简单格式转换软件,大家下载测试下

https://download.csdn.net/download/todaygods/13097578

ASP VBSCRIPT VBA RSA 公钥加密 私钥解密 私钥签名 公钥验签_第1张图片

 

 

你可能感兴趣的:(c#,rsa,加密解密,openssl,base64)