Java 直接编码/解码不会产生原始数据
我想创建一个RSA密钥对,并使用它对数据进行编码/解码。我的代码很短,但我找不到任何错误 有人能帮我找到我的问题吗 谢谢你的提示Java 直接编码/解码不会产生原始数据,java,encryption,rsa,public-key-encryption,Java,Encryption,Rsa,Public Key Encryption,我想创建一个RSA密钥对,并使用它对数据进行编码/解码。我的代码很短,但我找不到任何错误 有人能帮我找到我的问题吗 谢谢你的提示 // Generate key pair. KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024, new SecureRandom()); KeyPair keyPair = kpg.genKeyPair(); PublicKey publicKey = key
// Generate key pair.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024, new SecureRandom());
KeyPair keyPair = kpg.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// Data to encode/decode.
byte[] original = "The quick brown fox jumps over the lazy dog.".getBytes("UTF8");
// Encode data with public key.
Cipher cipherEncoder = Cipher.getInstance("RSA/ECB/NoPadding");
cipherEncoder.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encodedData = cipherEncoder.doFinal(original);
// Decode data with private key.
Cipher cipherDecoder = Cipher.getInstance("RSA/ECB/NoPadding");
cipherDecoder.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedData = cipherEncoder.doFinal(encodedData);
// Output.
System.out.println(new String("Original data: " + new String(original, "UTF8")));
System.out.println(new String("Encoded/decoded: " + new String(decodedData, "UTF8")));
最后的输出似乎很奇怪。首先,您使用的是
cipherEncoder
对数据进行解码。您可能打算使用密码解码器
。其次,在不使用填充的情况下使用RSA会出现问题(即,数据在开始时将有0
字节的负载)。我建议您至少使用PKCS1填充。下面是这些更改后的代码
// Generate key pair.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024, new SecureRandom());
KeyPair keyPair = kpg.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// Data to encode/decode.
byte[] original = "The quick brown fox jumps over the lazy dog.".getBytes("UTF8");
// Encode data with public key.
Cipher cipherEncoder = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipherEncoder.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encodedData = cipherEncoder.doFinal(original);
// Decode data with private key.
Cipher cipherDecoder = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipherDecoder.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedData = cipherDecoder.doFinal(encodedData);
// Output.
System.out.println(new String("Original data: " + new String(original, "UTF8")));
System.out.println(new String("Encoded/decoded: " + new String(decodedData, "UTF8")));
首先,您正在使用
cipherEncoder
对数据进行解码。您可能打算使用密码解码器
。其次,在不使用填充的情况下使用RSA会出现问题(即,数据在开始时将有0
字节的负载)。我建议您至少使用PKCS1填充。下面是这些更改后的代码
// Generate key pair.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024, new SecureRandom());
KeyPair keyPair = kpg.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// Data to encode/decode.
byte[] original = "The quick brown fox jumps over the lazy dog.".getBytes("UTF8");
// Encode data with public key.
Cipher cipherEncoder = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipherEncoder.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encodedData = cipherEncoder.doFinal(original);
// Decode data with private key.
Cipher cipherDecoder = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipherDecoder.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedData = cipherDecoder.doFinal(encodedData);
// Output.
System.out.println(new String("Original data: " + new String(original, "UTF8")));
System.out.println(new String("Encoded/decoded: " + new String(decodedData, "UTF8")));
教科书RSA(没有填充)是非常不安全的。PKCS#1 v1.5填充(开销为11字节)也不应使用。现在,建议使用OAEP(SHA1的开销为42字节)。@ArtjomB。谢谢你的提示!你有OAEP代码的例子给我吗?你是什么意思?只要更改密码字符串中的填充,就可以了。你不需要改变其他任何事情。可以在Cipher类文档中找到可用的填充字符串。好的。在大多数网站上,我都能找到使用BouncyCastle的例子。但当我正确理解你的时候,我就不需要它了?对不起,我的新手问题!一切都找到了。谢谢!教科书RSA(没有填充)是非常不安全的。PKCS#1 v1.5填充(开销为11字节)也不应使用。现在,建议使用OAEP(SHA1的开销为42字节)。@ArtjomB。谢谢你的提示!你有OAEP代码的例子给我吗?你是什么意思?只要更改密码字符串中的填充,就可以了。你不需要改变其他任何事情。可以在Cipher类文档中找到可用的填充字符串。好的。在大多数网站上,我都能找到使用BouncyCastle的例子。但当我正确理解你的时候,我就不需要它了?对不起,我的新手问题!一切都找到了。谢谢!非常感谢您的回答和发现我的错误。干得好!非常感谢您的回答和发现我的错误。干得好!