SRA解密報錯:Data must start with zero
阿新 • • 發佈:2018-11-29
專案背景:要對列印地址進行加密,用公鑰加密後會亂碼需要base64 decode一下,但是在解密時報錯:javax.crypto.BadPaddingException: Data must start with zero
解決辦法:
1.加解密時KeyFactory keyFactory = KeyFactory.getInstance("RSA");
2.將加解密的Cipher cipher = Cipher.getInstance(“RSA”)改為Cipher cipher = Cipher.getInstance(“RSA/ECB/NoPadding”)
困擾了兩天的問題解決了,程式碼如下,希望有次問題的同學不必再走此彎路。
-
package resources.util.encryption;
-
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.ObjectInputStream;
-
import java.io.ObjectOutputStream;
-
import java.security.Key;
-
import
java.security.KeyFactory;
-
import java.security.KeyPair;
-
import java.security.KeyPairGenerator;
-
import java.security.PrivateKey;
-
import java.security.PublicKey;
-
import java.security.spec.PKCS8EncodedKeySpec;
-
import java.security.spec.X509EncodedKeySpec;
-
-
import javax.crypto.Cipher;
-
-
import org.junit.Test;
-
-
public
class EncryptionUtil {
-
private
static
final String RSA =
"RSA";
-
private
static
final String RSANOPADDING =
"RSA/ECB/NoPadding";
-
private
static
final String PUBLIC_KEY_PATH =
"public.key";
-
private
static
final String PRIVATE_KEY_PATH =
"private.key";
-
private
static
final String path = Thread.currentThread().getContextClassLoader().getResource(
"/").getPath();
-
// private static final String path = "";
-
@Test
-
public void generateKey() throws Exception {
-
//KeyPairGenerator引擎類用於產生金鑰對,JDK(7)預設支援的演算法有,DiffieHellman、DSA、RSA、EC
-
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
-
generator.initialize(
512);
-
//產生金鑰對
-
KeyPair keyPair = generator.generateKeyPair();
-
//獲取公鑰
-
PublicKey publicKey = keyPair.getPublic();
-
//獲取私鑰
-
PrivateKey privateKey = keyPair.getPrivate();
-
-
//將公鑰與私鑰寫入檔案,以備後用
-
writeKey(PUBLIC_KEY_PATH, publicKey);
-
writeKey(PRIVATE_KEY_PATH, privateKey);
-
}
-
-
//公鑰加密
-
public
byte[] SRAEncrypt(String src)
throws Exception {
-
PublicKey publicKey= (PublicKey)readKey(path + PUBLIC_KEY_PATH);
-
X509EncodedKeySpec x509EncodedKeySpec =
new X509EncodedKeySpec(publicKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
byte[] data = src.getBytes();
-
int blockSize =
53;
//根據異常提示設的53
-
//根據塊大小分塊,不足一塊的部分為一塊
-
int blocksNum = (
int)Math.ceil((
1.0*data.length)/blockSize);
-
//加密
-
for (
int i =
0; i < blocksNum; i++) {
-
if (i < blocksNum -
1) {
-
cipher.doFinal(data, i * blockSize, blockSize);
-
}
else {
-
cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
-
}
-
}
-
return data;
-
}
-
-
//私鑰解密
-
public String SRADecrypt(byte[] data) throws Exception{
-
PrivateKey privateKey= (PrivateKey)readKey(path + PRIVATE_KEY_PATH);
-
PKCS8EncodedKeySpec pkcs8EncodedKeySpec =
new PKCS8EncodedKeySpec(privateKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
int blockSize =
64;
//根據異常提示設的64
-
//根據塊大小分塊,不足一塊的部分為一塊
-
int blocksNum = (
int)Math.ceil((
1.0*data.length)/blockSize);
-
//解密
-
for (
int i =
0; i < blocksNum; i++) {
-
if (i < blocksNum -
1) {
-
cipher.doFinal(data, i * blockSize, blockSize);
-
}
else {
-
cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
-
}
-
}
-
return
new String(data);
-
}
-
-
public void writeKey(String path, Key key) throws Exception {
-
FileOutputStream fos =
new FileOutputStream(path);
-
ObjectOutputStream oos =
new ObjectOutputStream(fos);
-
oos.writeObject(key);
-
oos.close();
-
}
-
-
public Key readKey(String path) throws Exception {
-
FileInputStream fis =
new FileInputStream(path);
-
ObjectInputStream bis =
new ObjectInputStream(fis);
-
Object object = bis.readObject();
-
bis.close();
-
return (Key)object;
-
}
-
-
@Test
-
public void testEncryptAndDecrypt() throws Exception {
-
Cipher cipher = Cipher.getInstance(RSA);
-
//讀取公鑰,進行加密
-
PublicKey publicKey= (PublicKey) readKey(
"component/"+PUBLIC_KEY_PATH);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
//加密
-
String sendInfo =
"我的明文";
-
byte[] results = cipher.doFinal(sendInfo.getBytes());
-
-
//讀取私鑰,進行解密
-
PrivateKey privateKey = (PrivateKey) readKey(
"component/"+PRIVATE_KEY_PATH);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
//解密
-
byte[] deciphered = cipher.doFinal(results);
-
//得到明文
-
String recvInfo =
new String(deciphered);
-
System.out.println(recvInfo);
-
}
-
-
@Test
-
public void testSRA() throws Exception{
-
PublicKey publicKey= (PublicKey)readKey(
"component/" + PUBLIC_KEY_PATH);
-
X509EncodedKeySpec x509EncodedKeySpec =
new X509EncodedKeySpec(publicKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSA);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
byte[] result = cipher.doFinal(
"yuanyuan".getBytes());
-
-
PrivateKey privateKey= (PrivateKey)readKey(
"component/" + PRIVATE_KEY_PATH);
-
PKCS8EncodedKeySpec pkcs8EncodedKeySpec =
new PKCS8EncodedKeySpec(privateKey.getEncoded());
-
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
-
cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
result = cipher.doFinal(result);
-
System.out.println(
new String(result));
-
}
-
}