FAQ |
Calendar |
![]() |
|
Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman. |
![]() |
|
Thread Tools |
#1
|
|||
|
|||
![]()
Gan, ane punya masalah nih. Ceritanya, ane mo coding program buat encrypt ama decrypt file pake RSA. Waktu proses Enkripsi gak masalah gan. Masalahnya muncul waktu proses dekripsi. Begitu sampe line yang ane bold, programnya langsung ngeluarin exception bad data. Parahnya, tu exception tidak selalu terjadi. Pernah berhasil 1-2 kali, sisanya keluar exception tadi. Succesful rate kira2 2% lah. Ntu kira2 salahnya dimana ya gan? Any help will be appreciated. Thx. Code: namespace enryptdecrypt { public class clsRSA:ApplicationException { private string mPrivateKey; private string mPublicKey; private RSACryptoServiceProvider objRSA = new RSACryptoServiceProvider(); public string PrivateKey { get { return mPrivateKey; } set { //string value; mPrivateKey = value; } } public string PublicKey { get { return mPublicKey; } set { //string value; mPublicKey = value; } } public object CreateNewKeys() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Generate Private Key mPrivateKey = RSA.ToXmlString(true); //Generate Public Key mPublicKey = RSA.ToXmlString(false); return null; } public void SaveKeyToFile(string PrivateKeyPath, string PublicKeyPath) { try { SaveTextToFile(PrivateKey, PrivateKeyPath); SaveTextToFile(PublicKey, PublicKeyPath); //return null; } catch (Exception ex) { Interaction.MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error !!!"); //return null; } } public void LoadKeyFromFile(string PrivateKeyPath, string PublickeyPath) { try { PrivateKey = GetFileContents(PrivateKeyPath); PublicKey = GetFileContents(PublickeyPath); //return null; } catch (Exception ex) { Interaction.MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error !!!"); //return null; } } public string StringEncrypt(string strSource) { byte[] bufSource = null; string strEncrypted = null; try { //use public key to encrypt objRSA.FromXmlString(PublicKey); //convert Source String into byte array and encrypt it bufSource = objRSA.Encrypt(Encoding.Unicode.GetBytes(strSource ),false); //convert Crypted byte array into string strEncrypted = Encoding.Unicode.GetString(bufSource); return(strEncrypted); } catch (Exception ex) { Interaction.MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error"); return null; } } public string StringDecrypt(string strSource) { byte[] bufSource = null; string strDecrypted = null; try { //use private key to decrypt objRSA.FromXmlString(PrivateKey); //convert source string into byte array and decrypte it bufSource = objRSA.Decrypt(Encoding.Unicode.GetBytes(strSource ), false); //convert decrypted byte array into string strDecrypted = Encoding.Unicode.GetString(bufSource); return (strDecrypted); } catch(Exception ex) { throw(ex); } } public bool FileEncrypt(string InName, string OutName) { string sContents = null; string strEncrypted = null; sContents = GetFileContents(InName); strEncrypted = StringEncrypt(sContents); byte[] bufSource; bufSource = Encoding.UTF8.GetBytes(strEncrypted); strEncrypted = System.Convert.ToBase64String(bufSource); if (SaveTextToFile(strEncrypted, OutName) == true) { return true; } else return false; } public bool FileDecrypt(string InName, string OutName) { string sContents = null; string strEncrypted = null; byte[] bufSource; sContents = GetFileContents(InName); bufSource = System.Convert.FromBase64String(sContents); sContents = Encoding.UTF8.GetString(bufSource); strEncrypted = StringDecrypt(sContents); if (SaveTextToFile(strEncrypted, OutName) == true) { return true; } else return false; } //private string[] arrFiles1; private string GetFileContents(string FullPath) { string strContents = null; StreamReader objReader = default(StreamReader); try { objReader = new StreamReader(FullPath); strContents = objReader.ReadToEnd(); objReader.Close(); return strContents; } catch (Exception Ex) { throw new ApplicationException("Read Input File Error !!!"); } } private bool SaveTextToFile(string strData, string FullPath) { string Contents = null; bool bAns = false; StreamWriter objReader = default(StreamWriter); try { objReader = new StreamWriter(FullPath); objReader.Write(strData); objReader.Close(); bAns = true; } catch (Exception Ex) { throw new ApplicationException("Write Output File Error!!!"); } return bAns; } /*private long FillRecursiveArray(String strSourcePath) { int Cmptr = 0; ArrayList lstStringFolders = new ArrayList(); string[] strSubFolders = null; ArrayList lstSortedFolders = new ArrayList(); string bufFolder = null; string bufFile = null; try { //Set initial Path lstStringFolders.Add(strSourcePath); //String[] arrFiles1 = new String; String[] arrFiles2 = new String[0]; Array.Copy(arrFiles1, arrFiles2, arrFiles2.Length); while (!(Cmptr == lstStringFolders.Count)) { strSubFolders = System.IO.Directory.GetDirectories(lstStringFolder s.Item(Cmptr)); lstStringFolders.AddRange(strSubFolders); Cmptr = Cmptr+1; //return Cmptr; } } catch (Exception ex) { throw new ApplicationException("Folder Recursion Error !!!"); } try { //Sort, to get sub-folde under his mother folder lstStringFolders.Sort(); foreach (var bufFolder in lstStringFolders) { lstStringFolders.Add(bufFolder); } } catch(Exception ex) { throw new ApplicationException("Folder Sort Error"); } try { //for each folder foreach (var bufFolder in lstStringFolders) { //for each File foreach (var bufFile in Directory.GetFiles(bufFolder)) { //set File Path into private array Array.Resize(ref arrFiles1, Information.UBound(arrFiles1) + 2); arrFiles1(Information.UBound(arrFiles1)) = bufFile; } } } catch (Exception ex) { throw new ApplicationException("File Recursion Error !!!"); } return arrFiles1.Length; } private long FillNonRecursiveArray(string strSourcePath) { string bufFile = null; //File Name Buffer //ReDim //File Path Array(Only in initial path) try { //for each file in the initial folder foreach (var bufFile in Directory.GetFiles(strSourcePath)) { //set FIle Path in private array Array.Resize(ref arrFiles1, Information.UBound(arrFiles1) + 2); arrFiles1(Information.UBound(arrFiles1)) = bufFile; } } catch (Exception ex) { throw new ApplicationException("Error, can't list Initial Path File"); } return arrFiles1.Length; }*/ } } Terkait:
|
![]() |
|
|