#regionRsa Public Static byte[] GetBytes (String num) {BigInteger n=NewBigInteger (NUM,Ten); String s= N.tostring (2); if(S.length%8>0) {s=NewString ('0',8-S.length%8) +s; } byte[] data =New byte[S.length/8]; String Ocetstr; for(inti =0; I < data. Length; i++) {Ocetstr= S.substring (8I8); Data[i]= Convert.tobyte (Ocetstr,2); } returndata; } /// <summary> ///byte array to 16 binary string/// </summary> /// <param name= "bytes" ></param> /// <returns></returns> Public StaticString Convbytearraytohex (byte[] data) {String s=""; for(inti =0; I < data. Length; i++) {s+ = Convert.ToString (Data[i], -); } returnS.toupper (); } Public Static stringRsaencrypt (stringModulus,stringExponent,stringcontent) { varparam =NewRSAParameters (); Param. Exponent=GetBytes (Exponent); Param. Modulus=GetBytes (modulus); RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); Rsa. ImportParameters (param); intCelllength =NewBigInteger (param. modulus). Bitcount ()/8; byte[] Binput =Encoding.UTF8.GetBytes (content); intMaxLength = Celllength- One; intGrouplength = (int) math.ceiling (Binput.length *1.00/MaxLength); byte[] ciphertext =New byte[Grouplength *Celllength]; for(inti =0; i < grouplength; i++) { intLen = MaxLength < Binput.length-maxlength * I?MaxLength:bInput.Length-MaxLength *i; byte[] Inbyte =New byte[Len]; Buffer.blockcopy (Binput, MaxLength* I, Inbyte,0, Len); byte[] temp = RSA. Encrypt (Inbyte,false); Buffer.blockcopy (temp,0, Ciphertext, I *celllength, celllength); } //ciphertext = RSA. Encrypt (Binput, false); //return convert.tobase64string (ciphertext); //return Tohex (ciphertext); returnBitconverter.tostring (ciphertext). Replace ("-",""); } #endregion
RSA Plus decryption