JAVA encription and password protection

tonywang0613

  1. Cryptography
  2. Base64 encoding
  3. Password protection with MD5

1. Cryptography

AES. Symmetric cryptographyuses the same secret (private) key to encrypt and decrypt its data

,including DES,AES.

Asymmetric Cryptographyuses both a public and private key.  Asymmetric allows for distribution of your public key to anyone. Using the key they can encrypt the data they want to send securely and then it can only be decoded by the person having the private key

importjava.security.MessageDigest;

importjavax.crypto.KeyGenerator;

importjavax.crypto.SecretKey;

importjavax.crypto.spec.SecretKeySpec;

importjavax.crypto.Cipher;

importjavax.crypto.spec.SecretKeySpec;

Encryption

Cipher cipher = Cipher.getInstance(« AES/ECB/PKCS5Padding »);

//the key be assigned by hand: byte[] key={0x13,…}; or generated by following code:

//KeyGenerator keyGen = KeyGenerator.getInstance(« AES »);

//keyGen.init(128);

//SecretKey secretKey = keyGen.generateKey();

SecretKeySpec secretKey = new SecretKeySpec(key, « AES »);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
return encryptedString;

Decryption

Cipher cipher = Cipher.getInstance(« AES/ECB/PKCS5PADDING »);
SecretKeySpec secretKey = new SecretKeySpec(key, « AES »);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
return decryptedString;

Example is from this link https://www.owasp.org/index.php/Using_the_Java_Cryptographic_Extensions

https://gist.github.com/05be883ffe042fbfb77d.git »>https://gist.github.com/05be883ffe042fbfb77d.git

2. Base64 encoding

C

View original post 246 mots de plus

Publicités
Par défaut