- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
public String encrypt(String str) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
ClassPathResource cpr = new ClassPathResource("beeline_cert.cer");
FileInputStream certFile = new FileInputStream(cpr.getFile());
java.security.cert.Certificate certificate = certificateFactory.generateCertificate(certFile);
certFile.close();
ecipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
ecipher.init(Cipher.ENCRYPT_MODE, certificate);
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
logger.debug("Base64.encodeBase64URLSafeString " + Base64.encodeBase64URLSafeString(enc));
return Base64.encodeBase64URLSafeString(enc);
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
} catch (CertificateException e) {
logger.error("CertificateException", e);
} catch (NoSuchAlgorithmException e) {
logger.error("NoSuchAlgorithmException", e);
} catch (NoSuchPaddingException e) {
logger.error("NoSuchPaddingException", e);
} catch (BadPaddingException e) {
logger.error("BadPaddingException", e);
} catch (IllegalBlockSizeException e) {
logger.error("IllegalBlockSizeException", e);
} catch (InvalidKeyException e) {
logger.error("InvalidKeyException", e);
} catch (IOException e) {
logger.error("IOException", e);
}
return null;
}
Нашел такой кладезь в одном из legacy-проектов. Книжку можно писать с таких примеров.
wvxvw 01.06.2016 00:46 # +2
3_14dar 01.06.2016 00:53 # 0
wvxvw 01.06.2016 08:32 # +4
dxd 01.06.2016 12:19 # 0
bormand 01.06.2016 07:15 # 0
wvxvw 01.06.2016 08:29 # 0
kegdan 01.06.2016 10:18 # +4