什么是BlowFish对称加密算法?

2025-04-06 21:49:27
推荐回答(2个)
回答1:

对称加密算法  简介:
  对称加密算法 对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。
  特点:
  对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。
  不足之处是,交易双方都使用同样钥匙,安全性得不到保证。此外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的惟一钥匙,这会使得发收信双方所拥有的钥匙数量成几何级数增长,密钥管理成为用户的负担。对称加密算法在分布式网络系统上使用较为困难,主要是因为密钥管理困难,使用成本较高。而与公开密钥加密算法比起来,对称加密算法能够提供加密和认证却缺乏了签名功能,使得使用范围有所缩小。在计算机专网系统中广泛使用的对称加密算法有DES和IDEA等。美国国家标准局倡导的AES即将作为新标准取代DES。
  具体算法:
  3DES算法,Blowfish算法,RC5算法。 对称加密算法-原理及应用  对称加密算法的优点在于加解密的高速度和使用长密钥时的难破解性。假设两个用户需要使用对称加密方法加密然后交换数据,则用户最少需要2个密钥并交换使用,如果企业内用户有n个,则整个企业共需要n×(n-1) 个密钥,密钥的生成和分发将成为企业信息部门的恶梦。对称加密算法的安全性取决于加密密钥的保存情况,但要求企业中每一个持有密钥的人都保守秘密是不可能的,他们通常会有意无意的把密钥泄漏出去——如果一个用户使用的密钥被入侵者所获得,入侵者便可以读取该用户密钥加密的所有文档,如果整个企业共用一个加密密钥,那整个企业文档的保密性便无从谈起。DESCryptoServiceProvider
  RC2CryptoServiceProvider
  RijndaelManaged
  TripleDESCryptoServiceProvider
  //例加密文本文件(RijndaelManaged )
  byte[] key = { 24, 55, 102,24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24,98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
  byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24,55, 102, 24, 98, 26, 67, 29, 99 };
  RijndaelManaged myRijndael = new RijndaelManaged();
  FileStream fsOut = File.Open(strOutName, FileMode.Create,FileAccess.Write);//strOutName文件名及路径 FileStream fsIn = File.Open(strPath, FileMode.Open,FileAccess.Read);
  CryptoStream csDecrypt=new CryptoStream(fsOut,myRijndael.CreateEncryptor(key, IV),CryptoStreamMode.Write);//读加密文本
  BinaryReader br = new BinaryReader(fsIn);
  csDecrypt.Write(br.ReadBytes((int)fsIn.Length),0, (int)fsIn.Length);
  csDecrypt.FlushFinalBlock();
  csDecrypt.Close();
  fsIn.Close();
  fsOut.Close();
  //解密文件
  byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118,104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92};
  byte[] IV ={ 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26,67, 29, 99 };
  RijndaelManaged myRijndael = new RijndaelManaged();
  FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
  CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key,IV), CryptoStreamMode.Read);
  StreamReader sr = new StreamReader(csDecrypt);//把文件读出来
  StreamWriter sw = new StreamWriter(strInName);//解密后文件写入一个新的文件
  sw.Write(sr.ReadToEnd());
  sw.Flush();
  sw.Close();
  sr.Close();f
  sOut.Close();
  用图片加密(RC2CryptoServiceProvider )
  FileStreamfsPic = new FileStream(pictureBox1.ImageLocation,FileMode.Open, FileAccess.Read);
  //加密文件流(textBox1.Text是文件名及路径)
  FileStream fsText = new FileStream(textBox1.Text, FileMode.Open,FileAccess.Read);
  byte[] bykey = new byte[16]; //初始化
  Key IVbyte[] byIv = new byte[8];
  fsPic.Read(bykey, 0, 16);
  fsPic.Read(byIv, 0, 8);
  RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//desc进行加密
  BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容
  FileStream fsOut = File.Open(strLinPath,FileMode.Create, FileAccess.Write); // strLinPath临时加密文件路径CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey,byIv), CryptoStreamMode.Write);//写入临时加密文件
  cs.Write(br.ReadBytes((int)fsText.Length),0, (int)fsText.Length);//写入加密流
  cs.FlushFinalBlock();
  cs.Flush();
  cs.Close();
  fsPic.Close();
  fsText.Close();
  fsOut.Close();
  用图片解密
  FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read); //图片流FileStream fsOut = File.Open(textBox1.Text,FileMode.Open, FileAccess.Read);//解密文件流
  byte[] bykey = new byte[16]; //初始化
  Key IVbyte[] byIv = new byte[8];
  fsPic.Read(bykey, 0, 16);
  fsPic.Read(byIv, 0, 8);
  string strPath = textBox1.Text;//加密文件的路径
  int intLent = strPath.LastIndexOf("\\")+ 1;
  int intLong = strPath.Length;
  string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称
  string strLinPath = "C:\\"+ strName;//临时解密文件路径
  FileStream fs = new FileStream(strLinPath, FileMode.Create,FileAccess.Write);
  RC2CryptoServiceProvider desc = newRC2CryptoServiceProvider();//desc进行解密
  CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey,byIv), CryptoStreamMode.Read);
  //读出加密文件
  BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容
  BinaryWriter sw = new BinaryWriter(fs);//写入解密流
  sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));
  //sw.Flush();
  sw.Close();
  sr.Close();
  fs.Close();
  fsOut.Close();
  fsPic.Close();
  csDecrypt.Flush();
  File.Delete(textBox1.Text.TrimEnd());//删除原文件
  File.Copy(strLinPath, textBox1.Text);//复制加密文件

回答2:

BlowFish算法基于Feistel网络,加密函数迭代执行16轮,分组长度为64位,密钥长度可以从32位到448位。算法由两部分组成,密钥扩展部分和数据加密部分,密钥扩展部分将最长最长为448位的密钥转化成共4168字节长度的子密钥数组,其中,数据加密由一个16轮的Feistel网络完成,每轮由一个密钥相关置换和一个密钥与数据相关的替换组成。