zoukankan      html  css  js  c++  java
  • 加密后字节数组和字符串相互转换

    加密结果直接转字符串

    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,符合预期。

  • 相关阅读:
    linux下so动态库一些不为人知的秘密(中二)
    linux下so动态库一些不为人知的秘密(中)
    linux下so动态库一些不为人知的秘密(上)
    Linux下gcc编译控制动态库导出函数小结
    解决Linux动态库版本兼容问题
    MySQL按天,按周,按月,按时间段统计【转载】
    MySQL统计函数记录——按月、按季度、按日、时间段统计以及MySQL日期时间函数大全
    RequestMapping中produces属性作用
    出现 java.net.ConnectException: Connection refused 异常的原因及解决方法
    Springboot应用中@EntityScan和@EnableJpaRepositories的用法
  • 原文地址:https://www.cnblogs.com/strongmore/p/13380418.html
Copyright © 2011-2022 走看看