在Java中使用对称密钥字节[]加密消息

在Java中使用对称密钥字节[]加密消息,java,encryption,cryptography,Java,Encryption,Cryptography,我想获取一段纯文本,一个字节[]形式的对称密钥(来自上一次计算),然后输出一个密码文本密文=加密(明文、共享密文) 如何合并纯文本和共享机密 public static String encrypt(String plainText, byte[] sharedSecret){ String cipherText = ""; //combining the sharedSecret with the plainText return cipherT

我想获取一段纯文本,一个字节[]形式的对称密钥(来自上一次计算),然后输出一个密码文本密文=加密(明文、共享密文) 如何合并纯文本和共享机密

public static String encrypt(String plainText, byte[] sharedSecret){
        String cipherText = "";
        //combining the sharedSecret with the plainText
        return cipherText;
    }

检查此代码的对称密钥加密。您可以重构以适合您的加密方法

import java.security.AlgorithmParameters; 
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
public class SymmEncryption {
public static void main(String[] args) throws InvalidAlgorithmParameterException {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256); // 56 is the keysize. Fixed for DES
        //Initialization Vector
        byte[] iv = "1234567812345678".getBytes();
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        SecretKey secretKey = keyGenerator.generateKey();
        System.out.println(secretKey.getFormat());
        Cipher desCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//algorithm/mode/padding
        //Electronic Codebook (ECB)
        System.out.format("Secret Key: %s--%s--%s%n", secretKey.getAlgorithm(), secretKey.getFormat(), secretKey.getEncoded());
        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        // sensitive information
        byte[] text = "No body can see me".getBytes();
        System.out.println("Hex text: " + byteArrayToHex(text));
        System.out.println("Text [Byte Format] : " + text);
        System.out.println("Text : " + new String(text));
        // Encrypt the text
        byte[] textEncrypted = desCipher.doFinal(text);
        System.out.println("Text Encryted : " + textEncrypted);
        System.out.println("Hex Encrypted text: " + byteArrayToHex(textEncrypted));
        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
        // Decrypt the text
        byte[] textDecrypted = desCipher.doFinal(textEncrypted);
        System.out.println("Text Decryted : " + new String(textDecrypted));
        System.out.println("Hex Decrypted text: " + byteArrayToHex(textDecrypted));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
}
static String byteArrayToHex(byte[] a) {
    StringBuilder sb = new StringBuilder();
    for (byte b : a)
        sb.append(String.format("%02x", b & 0xff));
    return sb.toString();
}
}

查看密码类。在这个网站和其他地方有成千上万的例子。你也需要静脉注射。你能找到解决方案吗?