1
우선,이 질문은 중복되지 않습니다. 나는 아주 이상한 문제에 직면하고있다.javax.crypto.BadPaddingException : 데이터가 0으로 시작되어야합니다.
다음은 내가하는 일입니다.
사례 1 :
- 는
모든 것이 잘 작동 공개 키를 사용하여 개인 키
사례 2 :
- 로드 인증서 Mozila 파이어 폭스 Key에 저장
- 를 사용하여 인증서 인증서의 공개 Keu를 사용하여 인증서
- 해독의 개인 키를 사용하여 암호화
모두 정상적으로 작동합니다.
사례 3 :
- 로드 증명서 인터넷 익스플로러 키 저장소
- 를 사용하여 인증서 A로부터 인증서 (A)의 개인 키를 사용하여
- 암호화
- 해독하여 공공 Keu 인증서의
해독 시간에 BadPadding ex의 오류가 발생합니다. 전화 번호
다음은 각 코드의 스 니펫입니다.구성 파일의
생성 키 쌍
KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
로드 모질라 키 스토어
String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
Security.addProvider(p1);
keyStore = KeyStore.getInstance("PKCS11");
keyStore.load(null, "password".toCharArray());
내용
name=NSS
slot=2
library=C:/Program Files/Mozilla Firefox/softokn3.dll
nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix='' secmod='secmod.db' flags=readOnly"
로드 IE의 키 스토어
keyStore = KeyStore.getInstance("Windows-MY");
keyStore.load(null, null);
가 암호화
if (keyStore != null) {
Enumeration<String> enumaration = null;
try {
enumaration = keyStore.aliases();
} catch (KeyStoreException e1) {
e1.printStackTrace();
}
ArrayList<String> certiList;
while (enumaration.hasMoreElements()) {
String aliases = enumaration.nextElement();
certiList = new ArrayList<String>();
certiList.add(aliases);
try {
selectedCert = keyStore.getCertificate(aliases);
selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
selectedAlias = aliases;
selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
} catch (KeyStoreException e) {
e.printStackTrace();
}
}
키 스토어에서 공개 키와 개인 키를 가져옵니다
private static String publicEncrypt(String text, Key pubKey) throws Exception {
BASE64Encoder bASE64Encoder = new BASE64Encoder();
byte[] plainText = text.getBytes();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String encryptedText = bASE64Encoder.encode(cipher.doFinal(plainText));
return encryptedText;
}
암호 해독
private static String privateDecrypt(String text, Key priKey)throws Exception {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] encryptText = base64Decoder.decodeBuffer(text);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, priKey);
String decryptedString = new String(cipher.doFinal(encryptText));
return decryptedString;
}
예외 스택 추적
javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at test.testclass.privateDecrypt(testclass.java:198)
at test.testclass.test(testclass.java:137)
at test.testclass.main(testclass.java:120)
나는 오랫동안이 작업을하고있다. 이건 매우 중요합니다. 추가 정보가 필요한지 알려주십시오.
개인 키로 암호화하지 마십시오. 누군가의 공개 키로 메시지를 암호화하고 개인 키로 메시지를 해독합니다. 그렇게하면 privat 키가 비공개로 유지됩니다. 당신은 비밀 키로 메시지에 서명 할 수 있지만 코드가 다른 것처럼 보이지는 않습니다. 너 뭐하려고? 마지막으로, 케이스 3에서는 암호화를 위해 하나의 인증서를 사용하고 다른 하나는 해독하도록 말합니다. 이것이 정확한지, 그렇다면 그 이유는 무엇입니까? – imichaelmiers
암호화 기능의 출력이 어떻게 생겼는지 예를 들려 줄 수 있습니까? –
거의 동일한 쌍의 공개/비공개 키를 사용하고 있지 않습니다. –