Java 这个业余的密钥工厂和加密代码有什么问题
我不明白我做错了什么,这个代码有三个步骤Java 这个业余的密钥工厂和加密代码有什么问题,java,encryption,Java,Encryption,我不明白我做错了什么,这个代码有三个步骤 创建公钥和私钥(保存到磁盘) 加密文本“hello hello” 解密文本“hello hello” 但是这个代码的输出是 [B@7455d93d(加密后) [B@3bc0f2e5(解密后) 我犯了一些非常业余的错误,但我想不出来 cipherData1.toString()不会做您认为它会做的事情 您可能希望新字符串(cipherData1)使用java.util.array.toString(byte[])来显示字节数组。byte[].toSt
- 创建公钥和私钥(保存到磁盘)
- 加密文本“hello hello”
- 解密文本“hello hello”
[B@3bc0f2e5(解密后) 我犯了一些非常业余的错误,但我想不出来
cipherData1.toString()
不会做您认为它会做的事情
您可能希望
新字符串(cipherData1)
使用java.util.array.toString(byte[])
来显示字节数组。byte[].toString()
返回数组的类型([B
),后跟其哈希代码
)
您还可以使用Base64将字节数组编码为ASCII字符串。Apache commons codec有一个免费的实现。您的cipherData和cipherData1是以字节数组的形式出现的,您需要将它们转换为字符串,array类可用的.toString()是泛型的.toString()它不会以字符串格式返回字节数组,为此,需要执行
System.out.println(新字符串(cipherData))
和System.out.println(新字符串(chiperData1))
,这比在打印之前将其分配到内存中的字符串要好
编辑:
另外,如果您正在考虑使用此进行跨平台通信,请说明编码,即:-
新字符串(cipherData,“UTF-8”)
-
新字符串(cipherData,“ISO-8859-1”)
除了这是二进制数据。可能先是Base64。@Oli就是这样。。非常感谢。它正在处理新字符串(cipherData1)。将尝试通过Internet发送一些密码now@Thilo:解密后的数据应该与原始数据相同。好的…我只是在查看变量名,听起来像是密文…新的String()命令正确处理二进制数据。“hello hello”.getBytes()
您应该在此处指定字符编码,否则双方可能会使用不兼容的内容。好的评论是,这可能会产生问题。私钥可能会被任何android设备使用。是,这很重要。如果您要进行服务器-客户端通信,我建议使用双重加密,在客户端(android设备)使用公钥进行RSA加密,然后使用该加密发送加密密钥(对于第二种加密方法)这是由设备生成的,这意味着您可以在服务器端拥有非常安全的私钥,并且只有公钥可用。对于二次加密算法,我建议使用TEA或类似的算法。我为一个在旧手机上运行平稳的项目做了类似的操作,因此它在Android上应该可以很好地工作。你知道吗服务器和客户端都应该创建密钥有自己的密钥对-私钥/公钥。我不完全理解。现在我的服务器有RSA私钥,我的客户端使用公钥将登录密码/用户名发送到服务器。TEA如何使这更安全?如果您保持RSA的“登录”连接,并在这个主要连接中发送(TEA)客户端每次运行应用程序时生成的密钥,您最终允许客户端和服务器根据会话密钥对TEA进行解密。TEA并不比RSA更安全,但是,如果您在上面指定的方法中将其与RSA结合使用,则会在客户端和服务器之间进行更安全的数据事务,因为RSA私钥s只有服务器知道,每次会话都会生成TEA密钥。谢谢,我了解这一点的基础知识有限。我将从RSA开始,并从那里开始。
public class KeyPairsGenerator {
public static void main(String args[]){
KeyPairsGenerator testClass = new KeyPairsGenerator();
testClass.GenerateKeyPair();
testClass.testEncryptDecrypt();
}
public void testEncryptDecrypt(){
ObjectInputStream oinPublic = null;
ObjectInputStream oinPrivate = null;
try {
//****************
//ENCRYPT
oinPublic = new ObjectInputStream
(new BufferedInputStream(new FileInputStream("public.key")));
BigInteger m = (BigInteger) oinPublic.readObject();
BigInteger e = (BigInteger) oinPublic.readObject();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal("hello hello".getBytes());
System.out.println(cipherData.toString());
//****************
//DECRYPT
oinPrivate = new ObjectInputStream
(new BufferedInputStream(new FileInputStream("private.key")));
BigInteger m1 = (BigInteger) oinPrivate.readObject();
BigInteger e1 = (BigInteger) oinPrivate.readObject();
RSAPrivateKeySpec keySpecPrivate = new RSAPrivateKeySpec(m1, e1);
KeyFactory fact1 = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact1.generatePrivate(keySpecPrivate);
Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher1.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherData1 = cipher1.doFinal(cipherData);
System.out.println(cipherData1.toString());
} catch (Exception e) {
throw new RuntimeException("Spurious serialization error", e);
} finally {
try {
oinPrivate.close();
oinPublic.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void GenerateKeyPair()
{
try{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(
kp.getPublic(),RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec
(kp.getPrivate(),RSAPrivateKeySpec.class);
saveToFile("public.key", pub.getModulus(),pub.getPublicExponent());
saveToFile("private.key", priv.getModulus(),priv.getPrivateExponent());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public void saveToFile(String fileName,BigInteger mod,
BigInteger exp) throws Exception {
ObjectOutputStream oout = new ObjectOutputStream
(new BufferedOutputStream(new FileOutputStream(fileName)));
try {
oout.writeObject(mod);
oout.writeObject(exp);
} catch (Exception e) {
throw new Exception("error", e);
} finally {
oout.close();
}
}
}