我有一个独立的Java程序,它从Mainframe_JCL触发。 Java程序有一个加密和解密字符串的代码。使用JDK 7使用AES 128时的问题
当我在我的本地运行该程序。加密后,当我解密时,该值是正确的(我得到了我提供的用于加密的字符串)。
但是,当这个由JCL执行这个在大型机上运行..我有越来越奇怪的解密值..种垃圾。
不知道最新的问题。任何帮助,将不胜感激。
我们使用以下JDK: IBM SDK针对z/OS,Java技术版,版本7
下面是用于加密和解密方法:
public static String encrypt(String text) throws UnsupportedEncodingException {
byte iv[] = new byte[16];
byte[] encrypted = null;
BASE64Encoder enc = new BASE64Encoder();
try {
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(), ivspec);
encrypted = cipher.doFinal(text.getBytes());
} catch (Exception ex) {
ex.printStackTrace();
}
return URLEncoder.encode(enc.encode(iv)+enc.encode(encrypted), "UTF-8");
}
public static String decrypt(String text) {
byte iv[] = new byte[16];
String decrypted = "";
byte[] splitText = null;
byte[] textToDecrypt = null;
BASE64Decoder dec = new BASE64Decoder();
try {
text = URLDecoder.decode(text, "UTF-8");
text = text.replaceAll(" ", "+");
splitText = dec.decodeBuffer(text);
splitText.toString();
for(int i=0;i<16;i++){
iv[i]=splitText[i];
}
textToDecrypt = new byte[splitText.length - 16];
for(int i=16;i<splitText.length;i++){
textToDecrypt[i-16]=splitText[i];
}
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKeySpec(), ivspec);
decrypted = new String(cipher.doFinal(textToDecrypt));
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
return decrypted;
}
static SecretKeySpec spec = null;
public static SecretKeySpec getKeySpec() throws IOException,
NoSuchAlgorithmException {
if (spec == null) {
String keyFile = "aes_key.key";
spec = null;
InputStream fis = null;
fis = Config.class.getClassLoader().getResourceAsStream(keyFile);
byte[] rawkey = new byte[16];
fis.read(rawkey);
fis.close();
spec = new SecretKeySpec(rawkey, "AES");
}
return spec;
}
你在同一台机器上进行加密和解密吗?你如何得到你的意见?读文件?你使用什么编码? –
是的,我正在同一台机器上进行加密和解密。我从表列值中获取输入。它的一个varchar设置为Java String变量。使用的编码是BASE64Encoder。 – Anand
你能提供一个不起作用的例子吗? – flo