1. 程式人生 > >SRA解密報錯:Data must start with zero

SRA解密報錯:Data must start with zero

專案背景:要對列印地址進行加密,用公鑰加密後會亂碼需要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”)

困擾了兩天的問題解決了,程式碼如下,希望有次問題的同學不必再走此彎路。


   
  1. package resources.util.encryption;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.security.Key;
  7. import
    java.security.KeyFactory;
  8. import java.security.KeyPair;
  9. import java.security.KeyPairGenerator;
  10. import java.security.PrivateKey;
  11. import java.security.PublicKey;
  12. import java.security.spec.PKCS8EncodedKeySpec;
  13. import java.security.spec.X509EncodedKeySpec;
  14. import javax.crypto.Cipher;
  15. import org.junit.Test;
  16. public class EncryptionUtil {
  17. private static final String RSA = "RSA";
  18. private static final String RSANOPADDING = "RSA/ECB/NoPadding";
  19. private static final String PUBLIC_KEY_PATH = "public.key";
  20. private static final String PRIVATE_KEY_PATH = "private.key";
  21. private static final String path = Thread.currentThread().getContextClassLoader().getResource( "/").getPath();
  22. // private static final String path = "";
  23. @Test
  24. public void generateKey() throws Exception {
  25. //KeyPairGenerator引擎類用於產生金鑰對,JDK(7)預設支援的演算法有,DiffieHellman、DSA、RSA、EC
  26. KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
  27. generator.initialize( 512);
  28. //產生金鑰對
  29. KeyPair keyPair = generator.generateKeyPair();
  30. //獲取公鑰
  31. PublicKey publicKey = keyPair.getPublic();
  32. //獲取私鑰
  33. PrivateKey privateKey = keyPair.getPrivate();
  34. //將公鑰與私鑰寫入檔案,以備後用
  35. writeKey(PUBLIC_KEY_PATH, publicKey);
  36. writeKey(PRIVATE_KEY_PATH, privateKey);
  37. }
  38. //公鑰加密
  39. public byte[] SRAEncrypt(String src) throws Exception {
  40. PublicKey publicKey= (PublicKey)readKey(path + PUBLIC_KEY_PATH);
  41. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
  42. KeyFactory keyFactory = KeyFactory.getInstance(RSA);
  43. publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
  44. Cipher cipher = Cipher.getInstance(RSANOPADDING);
  45. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  46. byte[] data = src.getBytes();
  47. int blockSize = 53; //根據異常提示設的53
  48. //根據塊大小分塊,不足一塊的部分為一塊
  49. int blocksNum = ( int)Math.ceil(( 1.0*data.length)/blockSize);
  50. //加密
  51. for ( int i = 0; i < blocksNum; i++) {
  52. if (i < blocksNum - 1) {
  53. cipher.doFinal(data, i * blockSize, blockSize);
  54. } else {
  55. cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
  56. }
  57. }
  58. return data;
  59. }
  60. //私鑰解密
  61. public String SRADecrypt(byte[] data) throws Exception{
  62. PrivateKey privateKey= (PrivateKey)readKey(path + PRIVATE_KEY_PATH);
  63. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
  64. KeyFactory keyFactory = KeyFactory.getInstance(RSA);
  65. privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
  66. Cipher cipher = Cipher.getInstance(RSANOPADDING);
  67. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  68. int blockSize = 64; //根據異常提示設的64
  69. //根據塊大小分塊,不足一塊的部分為一塊
  70. int blocksNum = ( int)Math.ceil(( 1.0*data.length)/blockSize);
  71. //解密
  72. for ( int i = 0; i < blocksNum; i++) {
  73. if (i < blocksNum - 1) {
  74. cipher.doFinal(data, i * blockSize, blockSize);
  75. } else {
  76. cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
  77. }
  78. }
  79. return new String(data);
  80. }
  81. public void writeKey(String path, Key key) throws Exception {
  82. FileOutputStream fos = new FileOutputStream(path);
  83. ObjectOutputStream oos = new ObjectOutputStream(fos);
  84. oos.writeObject(key);
  85. oos.close();
  86. }
  87. public Key readKey(String path) throws Exception {
  88. FileInputStream fis = new FileInputStream(path);
  89. ObjectInputStream bis = new ObjectInputStream(fis);
  90. Object object = bis.readObject();
  91. bis.close();
  92. return (Key)object;
  93. }
  94. @Test
  95. public void testEncryptAndDecrypt() throws Exception {
  96. Cipher cipher = Cipher.getInstance(RSA);
  97. //讀取公鑰,進行加密
  98. PublicKey publicKey= (PublicKey) readKey( "component/"+PUBLIC_KEY_PATH);
  99. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  100. //加密
  101. String sendInfo = "我的明文";
  102. byte[] results = cipher.doFinal(sendInfo.getBytes());
  103. //讀取私鑰,進行解密
  104. PrivateKey privateKey = (PrivateKey) readKey( "component/"+PRIVATE_KEY_PATH);
  105. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  106. //解密
  107. byte[] deciphered = cipher.doFinal(results);
  108. //得到明文
  109. String recvInfo = new String(deciphered);
  110. System.out.println(recvInfo);
  111. }
  112. @Test
  113. public void testSRA() throws Exception{
  114. PublicKey publicKey= (PublicKey)readKey( "component/" + PUBLIC_KEY_PATH);
  115. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
  116. KeyFactory keyFactory = KeyFactory.getInstance(RSA);
  117. publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
  118. Cipher cipher = Cipher.getInstance(RSA);
  119. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  120. byte[] result = cipher.doFinal( "yuanyuan".getBytes());
  121. PrivateKey privateKey= (PrivateKey)readKey( "component/" + PRIVATE_KEY_PATH);
  122. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
  123. privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
  124. cipher = Cipher.getInstance(RSANOPADDING);
  125. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  126. result = cipher.doFinal(result);
  127. System.out.println( new String(result));
  128. }
  129. }