我想将加密数据从 iPhone 发送到服务器(java)。但服务器抛出以下异常。堆栈跟踪是:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at com.justshare.util.Util.getDecryptedString(Util.java:59)
at com.justshare.util.Util.getRequestString(Util.java:226)
at com.justshare.servlets.MobileRegistration.processRequest(MobileRegistration.java:63)
at com.justshare.servlets.MobileRegistration.doPost(MobileRegistration.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
我正在使用 AES 算法进行加密,我的 iPhone 代码是:
- (NSData *)AES128EncryptWithKey:(NSString *)key{
char keyPtr[kCCKeySizeAES128 + 1]; // room for terminator (unused)
bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128,kCCOptionECBMode,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted );
if( cryptStatus == kCCSuccess )
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free( buffer ); //free the buffer
return nil;
}
用于解密的服务器端代码是:
public static byte[] key_reg = new byte[] {1, 2, 3, 4, 5,6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7};//... secret sequence of bytes
public static String getDecryptedString(byte[] data,int i){
String str = null;
try {
Cipher ci = Cipher.getInstance("AES/ECB/PKCS5Padding","SunJCE");
SecretKeySpec sk = new SecretKeySpec(key_reg, "AES");
ci.init(Cipher.DECRYPT_MODE, sk);
byte[] dataD = ci.doFinal(data);
str = new String(dataD,"UTF-8");
return str;
} catch (Throwable e) {
logger.error("Error in getRequestString : "+e);
e.printStackTrace();
}
return str;
}