我正在加密java中的文件,并将加密的文件和私钥发送到Android设备.但是在解密AndroID中的文件时,它会给pad块损坏错误.
顺便说一句,相同的解密代码在PC上工作
这是加密:
public voID encrypt(file inf,file outf,file publicKey,int userID,int resourceID) throws ArServerconnectionexception { // ENCRYPTION BEGIN try { pkCipher = Cipher.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (NoSuchpaddingException e) { // Todo auto-generated catch block e.printstacktrace(); } // create AES shared key cipher try { aesCipher = Cipher.getInstance("AES"); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (NoSuchpaddingException e) { // Todo auto-generated catch block e.printstacktrace(); } try { makeKey(); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } // file operation try { saveKey(new file(System.getProperty("user.home") + "/" + userID + "/keyfile"),publicKey); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (GeneralSecurityException e) { // Todo auto-generated catch block e.printstacktrace(); } // file operation try { encryptfiles(inf,outf); } catch (InvalIDKeyException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } // /ENCRYPTION END} public voID saveKey(file out,file publicKeyfile) throws IOException,GeneralSecurityException { // read public key to be used to encrypt the AES key byte[] encodedKey = new byte[(int) publicKeyfile.length()]; new fileinputStream(publicKeyfile).read(encodedKey); // create public key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey pk = kf.generatePublic(publicKeySpec); // write AES key pkCipher.init(Cipher.ENCRYPT_MODE,pk); CipherOutputStream os = new CipherOutputStream( new fileOutputStream(out),pkCipher); os.write(aesKey); os.close();} public voID makeKey() throws NoSuchAlgorithmException { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(AES_Key_Size); SecretKey key = kgen.generateKey(); aesKey = key.getEncoded(); aeskeySpec = new SecretKeySpec(aesKey,"AES");}
这是解密部分:
public class fileDecrypt { public static final int AES_Key_Size = 256; Cipher pkCipher,aesCipher; byte[] aesKey; SecretKeySpec aeskeySpec; private String pubKeyPath=null; private String prvKeyPath=null; private String keyfilePath=null; private String encfilePath=null; private String unencfilePath=null; public String getEncfilePath() { return encfilePath; } public voID setEncfilePath(String encfilePath) { this.encfilePath = encfilePath; } public String getUnencfilePath() { return unencfilePath; } public voID setUnencfilePath(String unencfilePath) { this.unencfilePath = unencfilePath; } public String getPubKeyPath() { return pubKeyPath; } public voID setPubKeyPath(String pubKeyPath) { this.pubKeyPath = pubKeyPath; } public String getPrvKeyPath() { return prvKeyPath; } public voID setPrvKeyPath(String prvKeyPath) { this.prvKeyPath = prvKeyPath; } public String getKeyfilePath() { return keyfilePath; } public voID setKeyfilePath(String keyfilePath) { this.keyfilePath = keyfilePath; } public voID decrypt() { Log.i("DECRYPT","**************************************************DECRYPT&*******************"); Log.i("encfilePath",encfilePath); Log.i("pubKeyPath",pubKeyPath); Log.i("prvKeyPath",prvKeyPath); Log.i("keyfilePath",keyfilePath); Log.i("unencfilePath",unencfilePath); Log.i("DECRYPT","********************************************DECRYPT&*******************"); try { pkCipher = Cipher.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (NoSuchpaddingException e) { // Todo auto-generated catch block e.printstacktrace(); } try { aesCipher = Cipher.getInstance("AES"); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (NoSuchpaddingException e) { // Todo auto-generated catch block e.printstacktrace(); } //DECRYPTION BEGIN file pkf=new file(pubKeyPath); byte[] encodedKey = new byte[(int) pkf.length()]; try { new fileinputStream(pkf).read(encodedKey); // create public key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey pk = kf.generatePublic(publicKeySpec); // write AES key pkCipher.init(Cipher.ENCRYPT_MODE,pk); } catch (fileNotFoundException e1) { // Todo auto-generated catch block e1.printstacktrace(); } catch (IOException e1) { // Todo auto-generated catch block e1.printstacktrace(); } catch (NoSuchAlgorithmException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (InvalIDKeyException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (InvalIDKeySpecException e) { // Todo auto-generated catch block e.printstacktrace(); } try { loadKey(new file(keyfilePath),new file(prvKeyPath)); } catch (GeneralSecurityException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } try { decrypt(new file(encfilePath),new file(unencfilePath)); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } //DECRYPTION END } /** * Decrypts an AES key from a file using an RSA private key */ public voID loadKey(file in,file privateKeyfile) throws GeneralSecurityException,IOException { // read private key to be used to decrypt the AES key byte[] encodedKey = new byte[(int) privateKeyfile.length()]; new fileinputStream(privateKeyfile).read(encodedKey); // create private key PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey pk = kf.generatePrivate(privateKeySpec); // read AES key pkCipher.init(Cipher.DECRYPT_MODE,pk); aesKey = new byte[AES_Key_Size / 8]; CipherinputStream is = new CipherinputStream(new fileinputStream(in),pkCipher); is.read(aesKey); aeskeySpec = new SecretKeySpec(aesKey,"AES"); } /** * Decrypts and then copIEs the contents of a given file. */ public voID decrypt(file in,file out) throws IOException { try { aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec); } catch (InvalIDKeyException e) { Log.i("EXCEPTION","INVALID KEY EXCEPTION"); e.printstacktrace(); } CipherinputStream is = new CipherinputStream(new fileinputStream(in),aesCipher); fileOutputStream os = new fileOutputStream(out); copy(is,os); is.close(); os.close(); } /** * copIEs a stream. */ private voID copy(inputStream is,OutputStream os) throws IOException { int i; byte[] b = new byte[2048]; while ((i = is.read(b)) != -1) { os.write(b,i); } }}
最佳答案我们也遇到了同样的问题,它只在androID端(使用androID SDK)进行加密,然后在设备上解密时才解决.没有找到其他正确的理由来解决这个问题. 总结 以上是内存溢出为你收集整理的java – 解密过程中损坏的pad块(Android)全部内容,希望文章能够帮你解决java – 解密过程中损坏的pad块(Android)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)