【发布时间】:2015-11-09 06:37:01
【问题描述】:
我正在使用 AES 加密一个字符串,并想在其他电脑上解密它。如果我在同一台电脑上执行加密和解密工作。但是对于其他电脑上的解密,我的加密算法正在生成一个“密码密码”,这是在另一侧与密钥一起解密所需要的。我不确定如何将密码传输到另一端。
这是我的代码
public class AESCrypt {
static String plainText;
static byte[] plainBytesDecrypted = new byte[1024];
static byte[] cipherBytes = new byte[1024];
static SecretKey key;
static Cipher cipher;
public AESCrypt() throws NoSuchAlgorithmException {
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
key = generator.generateKey();
}
public static byte[] encryption(String plainBytes) {
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherBytes = cipher.doFinal(plainBytes.getBytes());
System.out.println("Encrypted data : " + new String(cipherBytes));
} catch (Exception ex) {
ex.printStackTrace();
}
return cipherBytes;
}
private static String bytesToHex(byte[] hash) {
return DatatypeConverter.printHexBinary(hash);
}
public static String Decryption() throws InvalidKeyException, IllegalBlockSizeException, InvalidAlgorithmParameterException, BadPaddingException {
cipher.init(Cipher.DECRYPT_MODE, key, cipher.getParameters());
plainBytesDecrypted = cipher.doFinal(cipherBytes);
System.out.println("Decrypted data : " + new String(plainBytesDecrypted));
return (new String(plainBytesDecrypted));
}
}
【问题讨论】:
-
你听说过Public-key cryptography吗?
标签: java encryption aes