· 8 years ago · Nov 26, 2017, 11:12 AM
1class StringCipher
2{
3 private const int Keysize = 256;
4 private const int DerivationIterations = 1000;
5
6 public static string Encrypt(string plainText, string passPhrase)
7 {
8
9 var saltStringBytes = Generate256BitsOfRandomEntropy();
10 var ivStringBytes = Generate256BitsOfRandomEntropy();
11 var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
12 using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
13 {
14 var keyBytes = password.GetBytes(Keysize / 8);
15 using (var symmetricKey = new RijndaelManaged())
16 {
17 symmetricKey.BlockSize = 256;
18 symmetricKey.Mode = CipherMode.CBC;
19 symmetricKey.Padding = PaddingMode.PKCS7;
20 using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
21 {
22 using (var memoryStream = new MemoryStream())
23 {
24 using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
25 {
26 cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
27 cryptoStream.FlushFinalBlock();
28 var cipherTextBytes = saltStringBytes;
29 cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
30 cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
31 memoryStream.Close();
32 cryptoStream.Close();
33 return Convert.ToBase64String(cipherTextBytes);
34 }
35 }
36 }
37 }
38 }
39 }
40
41 private static byte[] Generate256BitsOfRandomEntropy()
42 {
43 var randomBytes = new byte[32];
44 using (var rngCsp = new RNGCryptoServiceProvider())
45 {
46 rngCsp.GetBytes(randomBytes);
47 }
48 return randomBytes;
49 }
50}
51
52public class PasswordEncryption {
53 private final static int DerivationIterations = 1000;
54 private final int Keysize = 256;
55
56 private static byte[] getSalt() throws NoSuchAlgorithmException {
57 SecureRandom sr = new SecureRandom();
58 byte[] salt = new byte[32];
59 sr.nextBytes(salt);
60 return salt;
61 }
62
63 public static String Encrypt(String plainText, String passPhrase)
64 {
65 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
66 byte[] saltStringBytes = getSalt();
67 byte[] IvStringBytes=getSalt();
68 byte[] pwd = plainText.getBytes("UTF-8");
69 char[] chars = passPhrase.toCharArray();
70 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
71 KeySpec pbeKeySpec = new PBEKeySpec(chars, saltStringBytes, DerivationIterations, 256);
72 SecretKey secretKey = factory.generateSecret(pbeKeySpec);
73 System.out.println(secretKey.toString());
74 SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
75 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
76 cipher.init(Cipher.ENCRYPT_MODE, secret);
77 byte[] result = cipher.doFinal(pwd);
78 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
79 outputStream.write(saltStringBytes);
80 outputStream.write(IvStringBytes);
81 outputStream.write(result);
82
83 byte end[] = outputStream.toByteArray();
84
85 String base64Encoded = Base64.getEncoder().encodeToString(end);
86 System.out.println(base64Encoded);
87
88 return base64Encoded;
89
90 }
91
92 public static void main(String[] args) {
93 Encrypt("passwordhere", "ABC");
94 }
95
96}