当前位置: 首页 > wzjs >正文

做设计的网站平邑网站定制

做设计的网站,平邑网站定制,室内设计网站 知乎,济南网站建设92jzh一、对称加密算法 对称加密算法采用相同的密钥来进行加密和解密操作。其优点是加密和解密速度快,不过密钥的管理和分发存在一定的安全风险。 1.1、DES(已不推荐使用) 这是早期的对称加密算法,密钥长度为 56 位。但由于密钥长度较短,如今已不…

一、对称加密算法

        对称加密算法采用相同的密钥来进行加密和解密操作。其优点是加密和解密速度快,不过密钥的管理和分发存在一定的安全风险。

1.1、DES(已不推荐使用)

这是早期的对称加密算法,密钥长度为 56 位。但由于密钥长度较短,如今已不太安全。

优点

  1. 历史悠久,算法公开,研究较为透彻,有很多相关的实现和工具。
  2. 加密和解密速度相对较快,在早期计算机性能有限的情况下具有一定优势。

缺点

  1. 密钥长度较短,只有 56 位,在现代计算能力下,容易受到暴力破解攻击。
  2. 安全性逐渐降低,目前已不推荐用于对安全性要求较高的场景。

加密

 // DES 加密public static String desEncrypt(String plainText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

public static String desDecrypt(String encryptedText, String key) throws Exception {DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(desKeySpec);Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String desKey = "66666666"; // DES 密钥长度必须为 8 字节// DES 测试String desEncrypted = desEncrypt(plainText, desKey);String desDecrypted = desDecrypt(desEncrypted, desKey);System.out.println("DES 加密: " + desEncrypted);System.out.println("DES 解密: " + desDecrypted);}DES 加密: 7LqxGfmOgresnxwwXzlGBWqFt8hXuqd6E4BC0mLxaBAohvYPdvaZXSO45z9XA5GH
DES 解密: 感谢关注,精华内容将持续更新!
    • 1.2、AES

        目前应用广泛的对称加密算法,支持 128 位、192 位和 256 位的密钥长度,安全性较高,效率也不错。

优点

1.安全性高,支持 128 位、192 位和 256 位的密钥长度,能够有效抵御各种已知的攻击。

2.加密和解密速度快,性能优越,在硬件和软件实现上都有很好的表现。

3.被广泛应用于各种领域,成为了对称加密算法的主流选择。

缺点

1.对于一些对资源要求极高的小型设备,可能会有一定的性能开销。

加密

// AES 加密public static String aesEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

 // AES 解密public static String aesDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String aesKey = "1234567812345678"; // AES 密钥长度可以为 16、24 或 32 字节// AES 测试String aesEncrypted = aesEncrypt(plainText, aesKey);String aesDecrypted = aesDecrypt(aesEncrypted, aesKey);System.out.println("AES 加密: " + aesEncrypted);System.out.println("AES 解密: " + aesDecrypted);}AES 加密: PUI7SM6+J4XvSDnioVneLtQDkBXchZlIF7k9v3fqe5Nwk8Polh+pBxR5RQmbHa7v
AES 解密: 感谢关注,精华内容将持续更新!
    1. 1.3、3DES

        它是 DES 的增强版本,通过多次使用 DES 算法来增加密钥长度,进而提高安全性。

优点

1.在 DES 的基础上进行了改进,通过多次使用 DES 算法,增加了密钥长度,提高了安全性。

2.兼容性好,由于是基于 DES 算法,在一些旧系统中可以继续使用。

缺点

1.加密和解密速度较慢,因为需要进行多次 DES 运算。

2.密钥长度相对较长,管理和存储成本较高。

加密

 // 3DES 加密public static String tripleDesEncrypt(String plainText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// 3DES 解密public static String tripleDesDecrypt(String encryptedText, String key) throws Exception {DESedeKeySpec desEdeKeySpec = new DESedeKeySpec(key.getBytes(StandardCharsets.UTF_8));SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");SecretKey secretKey = keyFactory.generateSecret(desEdeKeySpec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKey);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String tripleDesKey = "0123456789abcdef01234567"; // 3DES 密钥长度必须为 24 字节// 3DES 测试String tripleDesEncrypted = tripleDesEncrypt(plainText, tripleDesKey);String tripleDesDecrypted = tripleDesDecrypt(tripleDesEncrypted, tripleDesKey);System.out.println("3DES 加密: " + tripleDesEncrypted);System.out.println("3DES 解密: " + tripleDesDecrypted);}
    1. 1.4、Blowfish

        这是一种可变密钥长度的对称分组密码算法,密钥长度可以从 32 位到 448 位,具有较高的加密速度和安全性,适用于对速度要求较高的场景。

优点

1.密钥长度可变,范围从 32 位到 448 位,可以根据不同的安全需求进行调整。

2.加密和解密速度快,尤其是在 64 位块的加密操作上表现出色。

3.算法简单,易于实现,没有专利限制。

缺点

1.块大小固定为 64 位,在处理大数据时可能效率不如其他算法。

2.应用范围相对较窄,不如 AES 等算法普及。

加密

// Blowfish 加密public static String blowfishEncrypt(String plainText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// Blowfish 解密public static String blowfishDecrypt(String encryptedText, String key) throws Exception {SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Blowfish");Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String blowfishKey = "abcdefghijklmnop";// Blowfish 测试String blowfishEncrypted = blowfishEncrypt(plainText, blowfishKey);String blowfishDecrypted = blowfishDecrypt(blowfishEncrypted, blowfishKey);System.out.println("Blowfish 加密: " + blowfishEncrypted);System.out.println("Blowfish 解密: " + blowfishDecrypted);}Blowfish 加密: HEKXerp2DpkPIHeIIt/cPBxub0z7jWWYKxZImxB2VOfDWIrH/dHVNqSP7gyHwyiU
Blowfish 解密: 感谢关注,精华内容将持续更新!
    1. 1.5、Twofish

        作为 Blowfish 算法的继任者,Twofish 同样是对称分组加密算法。它支持 128 位、192 位和 256 位的密钥长度,设计上考虑了安全性和性能的平衡,并且具有良好的灵活性和可扩展性。

优点

1.安全性高,设计上考虑了各种攻击方式,具有较好的抗攻击能力。

2.支持多种密钥长度,包括 128 位、192 位和 256 位。

3.算法灵活性高,可扩展性好。

缺点

1.实现相对复杂,需要一定的技术成本。

2.应用不如 AES 广泛,相关的工具和库相对较少。

引入依赖

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.70</version>
</dependency>

加密

// Twofish 加密(需要 Bouncy Castle 库)public static String twofishEncrypt(String plainText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));return Base64.getEncoder().encodeToString(encryptedBytes);}

解密

// Twofish 解密(需要 Bouncy Castle 库)public static String twofishDecrypt(String encryptedText, String key) throws Exception {Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "Twofish");Cipher cipher = Cipher.getInstance("Twofish/ECB/PKCS5Padding", "BC");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);byte[] decryptedBytes = cipher.doFinal(decodedBytes);return new String(decryptedBytes, StandardCharsets.UTF_8);}

结果

public static void main(String[] args) throws Exception {String plainText = "感谢关注,精华内容将持续更新!";String twofishKey = "0123456789abcdef0123456789abcdef";// Twofish 测试String twofishEncrypted = twofishEncrypt(plainText, twofishKey);String twofishDecrypted = twofishDecrypt(twofishEncrypted, twofishKey);System.out.println("Twofish 加密: " + twofishEncrypted);System.out.println("Twofish 解密: " + twofishDecrypted);}Twofish 加密: Z9UhJb9JZT0++Do6zauZPwf5tOMc6gX2o4LE7pZLXcwTL4PJ6m4LGvobb9k7Uv1e
Twofish 解密: 感谢关注,精华内容将持续更新!

通过以上内容便可轻轻松松使用是对称加密算法.是不是超级简单.有任何问题欢迎留言哦!!!

重点!重点!重点!

遇到问题不用怕不如来我的知识库找找看,也许有意想不到的收获!!!

易网时代-易库资源-易库教程:.NET开发、Java开发、PHP开发、SqlServer技术、MySQL技术-开发资料大全-易网时代-易库资源-易库教程 (escdns.com)

http://www.dtcms.com/wzjs/555039.html

相关文章:

  • 最新淘宝客网站程序北京网站建设laitang
  • 静态购物网站模版企业没有网站怎样做推广方案
  • 企业网站设计的要求微网站怎么自己做
  • 做汽车销售要了解的网站网站备案的具体流程
  • 包头网站建设多少钱他达拉非和西地那非的区别
  • 吉林做网站公司云商城搭建
  • 网站如何做404wordpress app发表
  • 网站开发服务 退款帮企业建设网站和推广网站
  • 中小网站建设设计网页公司哪里好
  • 网站建设中网站图片如何修改wordpress 删除自己的评论
  • 网站正在开发中公众号登录入口在哪
  • 网站信息备案查询系统支付网站怎么做的
  • 注册公司网站怎么做平面设计论坛有哪些
  • 生意网官方网站深圳住建局工程交易中心
  • 成都市城乡建设局网站有没有专门做艺术的网站
  • 建设网站需要用到哪些技术人员怎样建设一个公司网站
  • 学网站建设软件开发郑州经济技术开发区属于什么区
  • 网站建设方案销售网站基站的建设
  • 制作网站和制作网页的分别建设免费网站制作
  • 黄陂网站建设手机怎么制作h5作品
  • 黄浦专业做网站seo外链自动群发工具
  • 做电影网站看电影算网站流量吗免费网站软件下载大全2018
  • 盐城哪家做网站的正规千万别自学软件编程
  • 手机网站 尺寸网页制作与网站建设作业
  • 关于申请建设网站的请示一千元左右最好的手机
  • 做网站0基础写代码网站 没有域名需要备案吗
  • 莘县网站开发用微信小程序怎么做网站
  • 仿制网站侵权吗福州网站推广
  • 网页网站设计公司排行榜wordpress wshk
  • 信誉好的盐城网站开发安阳手机网站建设