public byte[] EncryptText(EncryptTextMessage message) { byte[] encryptedText = null; //Odradi se enkripcija if (message.Algorithm == AlgorithmType.RC4) { RC4 rc = new RC4(message.Key, message.IV); encryptedText = rc.Crypt(message.Data); } else if (message.Algorithm == AlgorithmType.RC4CTR) { RC4 rc = new RC4(message.Key, message.IV); encryptedText = rc.CryptWithCTR(message.Data); } else if (message.Algorithm == AlgorithmType.A52 || message.Algorithm == AlgorithmType.A52CTR) { A52 alg = new A52(); alg.SetKey(message.Key); alg.SetF(message.FKeyA52); if (message.Algorithm == AlgorithmType.A52) { encryptedText = alg.Crypt(message.Data); } else { alg.SetIV(message.IV); encryptedText = alg.CryptWithCTR(message.Data); } } else if (message.Algorithm == AlgorithmType.RSA) { RSA rsa = new RSA(); rsa.E = new BigInteger(message.Key); rsa.P = new BigInteger(message.P); rsa.Q = new BigInteger(message.Q); rsa.GenerateRSA(); //BigInteger result = rsa.Crypt(new BigInteger(message.Data)); //encryptedText = result.ToByteArray(); encryptedText = rsa.Crypt(message.Data); } else if (message.Algorithm == AlgorithmType.TigerHash) { TigerHash th = new TigerHash(); byte[] msg = message.Data; encryptedText = th.ComputeHash(message.Data); } return(encryptedText); }
public EncryptFileResponse Encrypt(EncryptFileMessage cryptMessage) { EncryptFileResponse response = new EncryptFileResponse(); //Odradi se enkripcija if (cryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4 || cryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4CTR) { try { using (var bw = new BinaryWriter(File.Open (Path.Combine(fileCloudPath, cryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None))) { try { BinaryReader br = new BinaryReader(cryptMessage.Data); RC4 rc4; bool ctr; if (cryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4) { ctr = false; rc4 = new RC4(cryptMessage.MetaData.Key); } else { ctr = true; rc4 = new RC4(cryptMessage.MetaData.Key, cryptMessage.MetaData.IV); } int buffSize = 2048; byte[] buff = new byte[buffSize]; int bytesRead; while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0) { if (bytesRead < buffSize) { byte[] tmp_buff = new byte[bytesRead]; Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead); byte[] output; if (ctr) { output = rc4.CryptWithCTR(tmp_buff); } else { output = rc4.Crypt(tmp_buff); } bw.Write(output); } else { byte[] output; if (ctr) { output = rc4.CryptWithCTR(buff); } else { output = rc4.Crypt(buff); } bw.Write(output); } } response.Finished = true; } catch (Exception ex) { Console.Write("asd"); response.Finished = false; } } } catch (Exception e) { } } else if (cryptMessage.MetaData.AlgorithmType == AlgorithmType.A52 || cryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR) { try { using (var bw = new BinaryWriter(File.Open (Path.Combine(fileCloudPath, cryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None))) { try { BinaryReader br = new BinaryReader(cryptMessage.Data); A52 a52 = new A52(); a52.SetKey(cryptMessage.MetaData.Key); a52.SetF(cryptMessage.MetaData.FKeyA52); bool ctr = false; if (cryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR) { a52.SetIV(cryptMessage.MetaData.IV); ctr = true; } int buffSize = 2048; byte[] buff = new byte[buffSize]; int bytesRead; while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0) { if (bytesRead < buffSize) { byte[] tmp_buff = new byte[bytesRead]; Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead); byte[] output; if (ctr) { output = a52.CryptWithCTR(tmp_buff); } else { output = a52.Crypt(tmp_buff); } bw.Write(output); } else { byte[] output; if (ctr) { output = a52.CryptWithCTR(buff); } else { output = a52.Crypt(buff); } bw.Write(output); } } response.Finished = true; } catch (Exception ex) { response.Finished = false; } } } catch (Exception ex) { } } else { response.Finished = false; } return(response); }