java - Java 密码在不同的环境中表现不同
问题描述
我已经编写了一个带有加密的 netty 服务器-客户端系统,但是如果我在笔记本电脑上使用该程序,它就无法工作,没有错误,所以我不知道问题出在哪里,我的加密工具类:
public static ByteBuf decryptWithRSA(ByteBuf encrypted, PrivateKey mykey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, mykey);
return wrightbytes(encrypted.alloc().buffer(), cipher.doFinal(readbytes(encrypted)));
}
public static ByteBuf encryptWithRSA(ByteBuf encrypted, PublicKey mykey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, mykey);
return wrightbytes(encrypted.alloc().buffer(), cipher.doFinal(readbytes(encrypted)));
}
public static ByteBuf encryptWithAES(SecretKey key, ByteBuf message) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return wrightbytes(message.alloc().buffer(), cipher.doFinal(readbytes(message)));
}
public static ByteBuf dycryptWithAES(SecretKey key, ByteBuf message) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return wrightbytes(message.alloc().buffer(), cipher.doFinal(readbytes(message)));
}
public static SecretKey generateAESKey(int size) throws NoSuchAlgorithmException {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(size);
return kgen.generateKey();
}
private static ByteBuf wrightbytes(ByteBuf wright, byte[] data) {
int i = 0;
while (i < data.length) {
wright.writeByte(data[i]);
i++;
}
return wright;
}
private static byte[] readbytes(ByteBuf buf) {
int read = buf.readableBytes();
byte[] data = new byte[read];
int i = buf.readerIndex();
while (i < read) {
data[i] = buf.readByte();
i++;
}
return data;
}
也许有人发现我没有得到的错误?那是你的帮助
解决方案
推荐阅读
- c# - 如何合并日期 + 时间并传递给 CRM 日期时间字段
- python - Androguard: 'NameError: name 'AnalyzeAPK' is not defined' 错误
- c# - 如何将多个相似的方法组合成一个返回三个结果的方法?
- node.js - res.redirect 永远不会在 NodeJS 中使用 express 和 passport
- mysql-workbench - 在 unicenta 中连接多个用户 mysql 数据库
- git - 如何将 Git Bash 中的文件 cp 到新目录而无需 mv?
- r - ggplot2:将变量值传递给文本框
- flutter - Flutter ListView.builder - 如何实现复杂的布局
- php - 队列 Laravel 的问题(异步函数)
- typescript - 如何能够区分基于联合类型的两个对象?