Rui Patronillo Posted August 31, 2020 at 03:57 PM Report Share #619240 Posted August 31, 2020 at 03:57 PM (edited) Boas voltei. Para quem precisar assinatura de documentos em java (Android Studio). Citação private String Assinar_Documento(String Assinatura) { String Hash=""; char LineFeed=10; String Ass; PrivateKey privateKey; //String privateKeyString = ""+ // "-----BEGIN RSA PRIVATE KEY-----"+ String privateKeyString = "MIIC------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "----------------------------------------------------------------"+ "--------------------------------------------"; //"-----END RSA PRIVATE KEY-----"; try { privateKey=getPrivateKey(privateKeyString); Signature sig = Signature.getInstance("SHA1WithRSA"); sig.initSign(privateKey); byte[] data = Assinatura.getBytes(); sig.initSign(privateKey); sig.update(data); byte[] signatureBytes = sig.sign(); Ass=Base64.encodeToString(signatureBytes,Base64.DEFAULT); char chrs[] = Ass.toCharArray(); int i = 0; while (i < chrs.length) { if (chrs = LineFeed) { Hash=Hash+chrs ; } i++; } //System.out.println(Hash); return Hash; } catch (Exception Erro){ System.out.println(Erro.getMessage()); } return Hash; } public PrivateKey getPrivateKey(String key) { PrivateKey privateKey = null; try { String privateString = key; if(privateString!=null){ byte[] binCpk = Base64.decode(privateString,Base64.DEFAULT); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binCpk); privateKey = keyFactory.generatePrivate(privateKeySpec); } } catch(Exception e){ } return privateKey; } Edited August 31, 2020 at 03:59 PM by Rui Patronillo Link to comment Share on other sites More sharing options...
NigelLong Posted April 24, 2021 at 12:50 AM Report Share #622107 Posted April 24, 2021 at 12:50 AM I'm a newbie here. I also want to know how to make a signature invoice. Please guide me a little. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now