C#
RSACryptoServiceProvider 암호화
캡틴노랑이
2016. 1. 8. 15:47
반응형
RSACryptoServiceProvider 암호화
public static string Encrypt(this string target, string key) { if (string.IsNullOrEmpty(target)) throw new ArgumentException("target null"); if (string.IsNullOrEmpty(key)) throw new ArgumentException("key null"); return BitConverter.ToString( new RSACryptoServiceProvider( new CspParameters() { KeyContainerName = key }) { PersistKeyInCsp = true }.Encrypt(Encoding.UTF8.GetBytes(target), true ) ); } public static string Decrypt(this string source, string key) { if (string.IsNullOrEmpty(source)) throw new ArgumentException("sourcea null"); if (string.IsNullOrEmpty(key)) throw new ArgumentException("key null"); RSACryptoServiceProvider cryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters(){KeyContainerName = key}); cryptoServiceProvider.PersistKeyInCsp = true; byte[] rgb = Array.ConvertAll( source.Split( new string[1]{"-"}, StringSplitOptions.None), (Converter )(s => Convert.ToByte(byte.Parse(s, NumberStyles.HexNumber) ) ) ); return Encoding.UTF8.GetString(cryptoServiceProvider.Decrypt(rgb, true)); }
반응형