您的位置:程序门 -> vb ->



关于rsa非对称加密编程问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于rsa非对称加密编程问题
发表于:2007-07-16 20:51:33 楼主
<!--   以下内容由sohu社区为您保存   -->
imports   system
imports   system.security.cryptography
imports   system.text

class   rsacspsample
        shared   sub   main()
                try
                        'create   a   unicodeencoder   to   convert   between   byte   array   and   string.
                        dim   byteconverter   as   new   unicodeencoding()

                        'create   byte   arrays   to   hold   original,   encrypted,   and   decrypted   data.
                        dim   datatoencrypt   as   byte()   =   byteconverter.getbytes( "data   to   encrypt ")
                        dim   encrypteddata()   as   byte
                        dim   decrypteddata()   as   byte

                        'create   a   new   instance   of   rsacryptoserviceprovider   to   generate
                        'public   and   private   key   data.
                        dim   rsa   as   new   rsacryptoserviceprovider()

                        'pass   the   data   to   encrypt,   the   public   key   information  
                        '(using   rsacryptoserviceprovider.exportparameters(false),
                        'and   a   boolean   flag   specifying   no   oaep   padding.
                        encrypteddata   =   rsaencrypt(datatoencrypt,   rsa.exportparameters(false),   false)

                        'pass   the   data   to   decrypt,   the   private   key   information  
                        '(using   rsacryptoserviceprovider.exportparameters(true),
                        'and   a   boolean   flag   specifying   no   oaep   padding.
                        decrypteddata   =   rsadecrypt(encrypteddata,   rsa.exportparameters(true),   false)

                        'display   the   decrypted   plaintext   to   the   console.  
                        console.writeline( "decrypted   plaintext:   {0} ",   byteconverter.getstring(decrypteddata))
                catch   e   as   argumentnullexception
                        'catch   this   exception   in   case   the   encryption   did
                        'not   succeed.
                        console.writeline( "encryption   failed. ")
                end   try
        end   sub


        public   shared   function   rsaencrypt(byval   datatoencrypt()   as   byte,   byval   rsakeyinfo   as   rsaparameters,   byval   dooaeppadding   as   boolean)   as   byte()
                try
                        'create   a   new   instance   of   rsacryptoserviceprovider.
                        dim   rsa   as   new   rsacryptoserviceprovider()

                        'import   the   rsa   key   information.   this   only   needs
                        'toinclude   the   public   key   information.
                        rsa.importparameters(rsakeyinfo)

                        'encrypt   the   passed   byte   array   and   specify   oaep   padding.    
                        'oaep   padding   is   only   available   on   microsoft   windows   xp   or
                        'later.    
                        return   rsa.encrypt(datatoencrypt,   dooaeppadding)
                        'catch   and   display   a   cryptographicexception    
                        'to   the   console.
                catch   e   as   cryptographicexception
                        console.writeline(e.message)

                        return   nothing
                end   try
        end   function


        public   shared   function   rsadecrypt(byval   datatodecrypt()   as   byte,   byval   rsakeyinfo   as   rsaparameters,   byval   dooaeppadding   as   boolean)   as   byte()
                try
                        'create   a   new   instance   of   rsacryptoserviceprovider.
                        dim   rsa   as   new   rsacryptoserviceprovider()

                        'import   the   rsa   key   information.   this   needs
                        'to   include   the   private   key   information.
                        rsa.importparameters(rsakeyinfo)

                        'decrypt   the   passed   byte   array   and   specify   oaep   padding.    
                        'oaep   padding   is   only   available   on   microsoft   windows   xp   or
                        'later.    
                        return   rsa.decrypt(datatodecrypt,   dooaeppadding)
                        'catch   and   display   a   cryptographicexception    
                        'to   the   console.
                catch   e   as   cryptographicexception
                        console.writeline(e.tostring())

                        return   nothing
                end   try
        end   function
end   class


我想问的是   在以上程序中   哪个函数模块功能是解密程序,哪个是加秘程序,哪个对数据进行加密的过程.   如何解密得到原字符串


发表于:2007-07-16 21:35:151楼 得分:0
rsaencrypt 加秘
rsadecrypt 解密


快速检索

最新资讯
热门点击