ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

加密后字节数组和字符串相互转换

2020-07-26 15:03:49  阅读:303  来源: 互联网

标签:BLOWFISH 加密 字节 Cipher static 字符串 byte cipher String


加密结果直接转字符串

public class Client {
// 加密算法
  private static final String BLOWFISH = "Blowfish";
// 加密秘钥
  private static final String SECRET = "test";

  public static void main(String[] args) throws Exception {
    String source = "hello";
    byte[] encrypt = encrypt(source);
    byte[] decrypt = decrypt(new String(encrypt));
    System.out.println(new String(decrypt));
  }

// 加密
  private static byte[] encrypt(String data) throws Exception {
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    SecretKeySpec myskeys = new SecretKeySpec(SECRET.getBytes(), BLOWFISH);
    cipher.init(Cipher.ENCRYPT_MODE, myskeys);
    return cipher.doFinal(data.getBytes());
  }
// 解密
  private static byte[] decrypt(String data) throws Exception {
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    SecretKeySpec myskeys = new SecretKeySpec(SECRET.getBytes(), BLOWFISH);
    cipher.init(Cipher.DECRYPT_MODE, myskeys);
    return cipher.doFinal(data.getBytes());
  }
}

运行结果为

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
	at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(BlowfishCipher.java:319)
	at javax.crypto.Cipher.doFinal(Cipher.java:2164)
	at com.imooc.sourcecode.java.base.java8.base64.test2.Client.decrypt(Client.java:28)
	at com.imooc.sourcecode.java.base.java8.base64.test2.Client.main(Client.java:13)

这是因为解密之后得到的字节数组是不符合UTF8,GBK等编码规则的,转成字符串再转成字节数组,数据已经改变了,所以解密会出错。

将加密结果转为Base64再转为字符串

public class Client {
  private static final String BLOWFISH = "Blowfish";
  private static final String SECRET = "test";

  public static void main(String[] args) throws Exception {
    String source = "hello";
    byte[] encrypt = encrypt(source);
    String encodeToString = Base64.getEncoder().encodeToString(encrypt);
    byte[] decode = Base64.getDecoder().decode(encodeToString);
    byte[] decrypt = decrypt(decode);
    System.out.println(new String(decrypt));
  }

  private static byte[] encrypt(String data) throws Exception {
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    SecretKeySpec myskeys = new SecretKeySpec(SECRET.getBytes(), BLOWFISH);
    cipher.init(Cipher.ENCRYPT_MODE, myskeys);
    return cipher.doFinal(data.getBytes());
  }

  private static byte[] decrypt(byte[] data) throws Exception {
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    SecretKeySpec myskeys = new SecretKeySpec(SECRET.getBytes(), BLOWFISH);
    cipher.init(Cipher.DECRYPT_MODE, myskeys);
    return cipher.doFinal(data);
  }
}

结果为hello,符合预期。

标签:BLOWFISH,加密,字节,Cipher,static,字符串,byte,cipher,String
来源: https://www.cnblogs.com/strongmore/p/13380418.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有