c# - Bouncy Castle, RSA: transforming keys into a String format -


i'm using rsa (bouncy castle api) in c# project. generated keypair method:

rsakeypairgenerator r = new rsakeypairgenerator(); r.init(new keygenerationparameters(new securerandom(), 1024)); asymmetriccipherkeypair keys = r.generatekeypair();  asymmetrickeyparameter private_key = keys.private; asymmetrickeyparameter public_key = keys.public; 

now want save them in txt file problem can't convert them string format. read in post keys must serialized using:

privatekeyinfo k = privatekeyinfofactory.createprivatekeyinfo(private_key); byte[] serializedkey = k.toasn1object().getderencoded(); 

is right way? if yes, should after this? convert them byte[] string?

you use pemwriter store them in pem format:

textwriter textwriter = new stringwriter(); pemwriter pemwriter = new pemwriter(textwriter); pemwriter.writeobject(keys.private); pemwriter.writer.flush();  string privatekey = textwriter.tostring(); 

now privatekey contain this:

-----begin rsa private key----- miicxqibaakbgqdfhb3xi1azsmskvt7rz7gp2o/vd49zon89il1envkkph7ofxa2 ew/hjzbav33lhnffla/va5sdcbggryu1/smjfupjfezfbgcpluo1sw6z+bwxiifp qncotiw11c0fbwk+kb2y4e1oklv5f9atlnlekb4wbn8bmbfytu6lgwjkfqidaqab aoganpomtelm3hsgmsmkzoss4fb5cde/sb5w8ka805erx8wwuhuxtdypukwzwvme mngldags5f7f1kfetrogdw0lv4/t/fwabpgh8o2cpkydy4zxl8tmrh9dtyn/824l velxdgnjhwo5omvutsdmjc3tbg2ua3kmv4vam5qotlrinuecqqdo4zvi5e81zwns knruarx80t432mozyr0rvaavjfbnqpjl0h0xp5lkp74hvpvo9wdjvj0m4z4qrmgo bm2hsfz5akea2r469yxxgpldg/lvuqqfrl8ji9+rip7eqp7is/dt66nms31/hymt +hscez3qglnquyyyrr2rgqdhdju42hny/qjbakbptf1dxsmgbovyuauu84jacw17 b++6dd6kdrr7x7mvo2loar50rwiy0h8cv6esyctziqy9vhigryp0goqfkxecqa8t uvzpab7hnnah45qggvabhocwrhhfpghzeu/jk7/srbun7vd0czf7ixtagxkhaayv auw/zkzdrvhxe858heucqqcgaaag8gwm0qis0nhrtlldu4higjkn7sk0z46plfwr oqpctup4ehx85eihqccofng6ttr6axsgnmzvervxdbid -----end rsa private key----- 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -