简体   繁体   English

解密使用哈希密钥加密的消息

[英]Decrypting a message encrypted using a hashed key

I have a problem where I need to decrypt a message which was encrypted using AES=256. 我有一个问题,我需要解密使用AES = 256加密的消息。 I am already provided with a key and vector. 我已经提供了密钥和向量。 I have to hash the provided key using SHA-256 and then use this hash to encrypt a message. 我必须使用SHA-256对提供的密钥进行哈希处理,然后使用此哈希对消息进行加密。 The decryption code runs fine but the result is not the original String . 解密代码运行良好,但结果不是原始的String Result: ?m?>? ???????z?p???>??<3? 结果: ?m?>? ???????z?p???>??<3? ?m?>? ???????z?p???>??<3? (the exact text is different, but after copying and pasting it, it is different). (确切的文本是不同的,但是在复制和粘贴之后,它是不同的)。

My code below: 我的代码如下:

try {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hashBytes = digest.digest("someKey".getBytes(ENCODING_UTF8)); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("somevector".getBytes(ENCODING_UTF8));
    SecretKeySpec skeySpec = new SecretKeySpec(hashBytes, AES);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] cipherText = cipher.doFinal(plainText.getBytes(ENCODING_UTF8));          
    encrypted = EACECryptoUtils.base64Encode(cipherText);
    Cipher decryptCipher = Cipher.getInstance(TRANSFORMATION_TYPE);
    IvParameterSpec decryptIV = new IvParameterSpec("somevector".getBytes(ENCODING_UTF8));
    SecretKeySpec decryptSkeySpec = new SecretKeySpec(hashBytes, AES);
    decryptCipher.init(Cipher.DECRYPT_MODE, decryptSkeySpec, decryptIV);
    byte[] original = cipher.doFinal(EACECryptoUtils.base64Decode(encrypted));
    decrypted = new String(original);
    } catch (Exception e) {
            log.error(new LogRecord(FUNCTION_NAME + "Exception while encrypting the data", e));
            throw e;
        }
    }

byte[] original = cipher.doFinal(EACECryptoUtils. byte []原始= cipher.doFinal(EACECryptoUtils。

I believe you should use decryptCipher instead of cipher 我相信您应该使用decryptCipher而不是cipher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM