字符串加密工作,byte []数组类型的加密不起作用 [英] Encryption of Strings works, encryption of byte[] array type does not work

查看:148
本文介绍了字符串加密工作,byte []数组类型的加密不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下 LINK 用于加密,并尝试使用Strings,它的工作。然而,由于我正在处理图像,我需要使用字节数组来进行加密/解密过程。所以我修改了以下链接的代码:

  public class AESencrp {

private static final String ALGO =AES;
private static final byte [] keyValue =
new byte [] {'T','h','e','B','e','s','t',$ b'b's','e','c','r','e','t','K','e','y'};

public static byte [] encrypt(byte [] Data)throws异常{
Key key = generateKey();
密码c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE,key);
byte [] encVal = c.doFinal(Data);
// String encryptedValue = new BASE64Encoder()。encode(encVal);
return encVal;
}

public static byte [] decrypt(byte [] encryptedData)throws Exception {
Key key = generateKey();
密码c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE,key);

byte [] decValue = c.doFinal(encryptedData);
return decValue;
}

private static Key generateKey()throws Exception {
Key key = new SecretKeySpec(keyValue,ALGO);
返回键;

}



而checker类是:

  public class Checker {

public static void main(String [] args)throws Exception {

byte [] array = new byte [] {127,-128,0};
byte [] arrayEnc = AESencrp.encrypt(array);
byte [] arrayDec = AESencrp.decrypt(arrayEnc);

System.out.println(Plain Text:+ array);
System.out.println(Encrypted Text:+ arrayEnc);
System.out.println(解密文本:+ arrayDec);
}
}

但是我的输出是:

 纯文本:[B @ 1b10d42 
加密文本:[B @ dd87b2
解密文本:[B @ 1f7d134

所以解密的文本不是纯文本。我应该怎么做才能解决这个问题,我知道我在原始链接中尝试过这个例子,并且与Strings一起使用。

解决方案

're see是数组的toString()方法的结果。这不是字节数组的内容。

[B 是类型(字节数组),而 1b10d42 是数组的hashCode。


I am using the following LINK for encryption and tried it with Strings and it worked. However, since I am dealing with images, I needed the encryption/decryption process to happen with byte arrays. So I modified the code in that link to the following:

public class AESencrp {

 private static final String ALGO = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static byte[] encrypt(byte[] Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data);
    //String encryptedValue = new BASE64Encoder().encode(encVal);
    return encVal;
}

public static byte[] decrypt(byte[] encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);

    byte[] decValue = c.doFinal(encryptedData);
    return decValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;

}

and the checker class is:

public class Checker {

public static void main(String[] args) throws Exception {

   byte[] array = new byte[]{127,-128,0};
    byte[] arrayEnc = AESencrp.encrypt(array);
    byte[] arrayDec = AESencrp.decrypt(arrayEnc);

    System.out.println("Plain Text : " + array);
    System.out.println("Encrypted Text : " + arrayEnc);
    System.out.println("Decrypted Text : " + arrayDec);
}
}

However my output is:

Plain Text : [B@1b10d42
Encrypted Text : [B@dd87b2
Decrypted Text : [B@1f7d134

so decrypted text is not plain text. What should i do to fix this knowing that I tried the example in the original link and it worked with Strings.

解决方案

What you're seeing is the result of the array's toString() method. It's not the content of the byte array. Use java.util.Arrays.toString(array) to display the content of the arrays.

[B is the type (array of bytes), and 1b10d42 is the hashCode of the array.

这篇关于字符串加密工作,byte []数组类型的加密不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆