public bool RSAImport(byte[] exportedkey) { RSAReader reader = new RSAReader(exportedkey); eRSAKeyFormat format = (eRSAKeyFormat)reader.ReadByte(); RSAParam key = new RSAParam(); /* input modulus and exponent*/ key.Modulus = reader.ReadBignum(); key.Exponent = reader.ReadBignum(); if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE) { key.D = reader.ReadBignum(); } if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED) { key.DQ = reader.ReadBignum(); key.DP = reader.ReadBignum(); key.pQ = reader.ReadBignum(); key.qP = reader.ReadBignum(); key.P = reader.ReadBignum(); key.Q = reader.ReadBignum(); } //skip version at end of buffer this.ImportParam(key); return(true); }
private byte[] RSAExport(RSAParam key, eRSAKeyFormat format) { RSAWriter writer = new RSAWriter(); /* output key type */ writer.WriteByte((byte)format); /* output modulus and exponent*/ writer.WriteBignum(key.Modulus); writer.WriteBignum(key.Exponent); if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE) { writer.WriteBignum(key.D); } if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED) { writer.WriteBignum(key.DQ); writer.WriteBignum(key.DP); writer.WriteBignum(key.pQ); writer.WriteBignum(key.qP); writer.WriteBignum(key.P); writer.WriteBignum(key.Q); } return(writer.GetBuffer()); }
public static string RSAdecrypt(string inputString) { RSAParam param = GetRSAParamDecrypt(); // do Add Proper Exception Handlers RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(param.dwKeySize); rsaCryptoServiceProvider.FromXmlString(param.xmlString); int base64BlockSize = ((param.dwKeySize / 8) % 3 != 0) ? (((param.dwKeySize / 8) / 3) * 4) + 4 : ((param.dwKeySize / 8) / 3) * 4; int iterations = inputString.Length / base64BlockSize; ArrayList arrayList = new ArrayList(); for (int i = 0; i < iterations; i++) { byte[] encryptedBytes = Convert.FromBase64String( inputString.Substring(base64BlockSize * i, base64BlockSize)); // Be aware the RSACryptoServiceProvider reverses the order of // encrypted bytes after encryption and before decryption. // If you do not require compatibility with Microsoft Cryptographic // API (CAPI) and/or other vendors. // Comment out the next line and the corresponding one in the // EncryptString function. Array.Reverse(encryptedBytes); arrayList.AddRange(rsaCryptoServiceProvider.Decrypt( encryptedBytes, true)); } return(Encoding.UTF32.GetString(arrayList.ToArray( Type.GetType("System.Byte")) as byte[])); }
public static string RSAencrypt(string inputString) { RSAParam param = GetRSAParamEncrypt(); // do Add Proper Exception Handlers RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(param.dwKeySize); rsaCryptoServiceProvider.FromXmlString(param.xmlString); int keySize = param.dwKeySize / 8; byte[] bytes = Encoding.UTF32.GetBytes(inputString); // The hash function in use by the .NET RSACryptoServiceProvider here // is SHA1 // int maxLength = ( keySize ) - 2 - // ( 2 * SHA1.Create().ComputeHash( rawBytes ).Length ); int maxLength = keySize - 42; int dataLength = bytes.Length; int iterations = dataLength / maxLength; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i <= iterations; i++) { byte[] tempBytes = new byte[ (dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i]; Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length); byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true); // Be aware the RSACryptoServiceProvider reverses the order of // encrypted bytes. It does this after encryption and before // decryption. If you do not require compatibility with Microsoft // Cryptographic API (CAPI) and/or other vendors. Comment out the // next line and the corresponding one in the DecryptString function. Array.Reverse(encryptedBytes); // Why convert to base 64? // Because it is the largest power-of-two base printable using only // ASCII characters stringBuilder.Append(Convert.ToBase64String(encryptedBytes)); } return(stringBuilder.ToString()); }
public byte[] Export(eRSAKeyFormat type) { RSAWriter writer = new RSAWriter(); byte[] exported_key_buffer; /* store packet header */ /* store version number */ writer.WriteByte(0x91); writer.WriteByte(0x00); //low endian of version /* store section and subsection */ writer.WriteByte(0x00); writer.WriteByte(0x00); RSAParam param = this.ExportParam(false); exported_key_buffer = RSAExport(param, eRSAKeyFormat.PK_PRIVATE_OPTIMIZED); int outlen = exported_key_buffer.Length; writer.WriteShort((ushort)outlen); writer.WriteShort((ushort)outlen); //need to be get out? writer.Write(exported_key_buffer, 0, exported_key_buffer.Length); //protocole version writer.WriteByte(0); writer.WriteByte(0); writer.WriteByte(0); writer.WriteByte(1); writer.WriteByte(0); writer.WriteByte(1); return(writer.GetBuffer()); }