在R中加密密码 – 使用RODBC连接到Oracle DB
我使用以下代码连接Oracle数据库:
> library(RODBC) > channel <- odbcConnect("R",uid="xxx",pwd="catch@123") > sqlSave(channel,resultsclassifiedfinal_MC_TC_P1,tablename="table1",rownames=FALSE,append=TRUE,fast = FALSE,nastring = NULL) > odbcClose(channel) 但是,我不能公开使用明文密码. 我看到要使用的pki和digest库,这是一种方法吗? 更新 > require(PKI) > key <- PKI.genRSAkey(2048) > x <- charToRaw("catch@123") > e <- PKI.encrypt(x,key) > y <- PKI.decrypt(e,key) > stopifnot(identical(x,y)) > print(rawToChar(y)) [1] "catch@123" > e [1] 85 8e 6b 38 da 69 8a 4c 20 ea 24 4e 6d cb 47 3b e6 d5 48 b4 57 93 31 d9 0c 20 70 89 [29] fa 3c 94 bf b6 09 82 29 6f 15 c5 ab 75 e6 e7 3a 4f 9a ec cb 37 a0 0d 19 58 db a3 1f [57] 65 ef f2 bd a1 c8 7e 2a f0 f2 a9 bc 19 59 4e 36 64 19 3f 00 a5 bb dc d1 1b d7 bf c5 [85] cf 60 83 88 17 fe cc e1 b6 ee 5b dc 11 cf b1 8f 8f e0 07 99 8e 2c 1f 4f 46 7e 1d 73 [113] 69 12 44 b6 0a 4c 41 2a 62 df bf 48 e3 11 15 ed fb c4 06 85 c9 fc c3 7d 1b a8 93 7d [141] 58 72 71 8b 0b bb fc 3d 1c fe 88 28 4c 43 ef 95 c1 8f 95 cd 59 66 81 c5 c9 6f 46 81 [169] 8b 53 8e cb 3e 45 2b c6 ea 86 47 97 a3 09 60 73 36 d4 76 76 a0 84 7b 42 07 f8 32 c2 [197] 19 55 93 39 9c a4 fe 3b a9 1a 26 fa c6 bd 77 50 ac 41 92 a2 b5 c4 1d a6 0e 30 00 d8 [225] ab 1e 79 13 23 be a7 89 fe d1 3c d2 ea b0 35 f0 69 7d 06 77 d1 03 a7 55 f2 d3 ca 1d [253] 66 fb c7 26 > y [1] 63 61 74 63 68 40 31 32 33 这是加密的方式吗? channel <- odbcConnect("R",pwd=rawToChar(y))编辑:以下功能现在可以在我的R包 keyringr中使用.Keyringr包也具有类似的功能来访问Gnome Keyring和macOS Keychain. — 如果您使用的是Windows,则可以使用PowerShell执行此操作.请参阅下面的博文. http://www.gilfillan.space/2016/04/21/Using-PowerShell-and-DPAPI-to-securely-mask-passwords-in-R-scripts/ 从本质上讲… >确保你有enabled PowerShell execution. # Create directory user profile if it doesn't already exist. $passwordDir = "$($env:USERPROFILE)DPAPIpasswords$($env:computername)" New-Item -ItemType Directory -Force -Path $passwordDir # Prompt for password to encrypt $account = Read-Host "Please enter a label for the text to encrypt. This will be how you refer to the password in R. eg. MYDB_MYUSER $SecurePassword = Read-Host -AsSecureString "Enter password" | convertfrom-securestring | out-file "$($passwordDir)$($account).txt" # Check output and press any key to exit Write-Host "Press any key to continue..." $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") >执行上面的脚本(右键单击>使用PowerShell运行),为密码提供有意义的名称,然后输入密码.您现在可以通过检查%USERPROFILE%/ DPAPI / passwords / [PC NAME] / [PASSWORD IDENTIFIER.txt]中的文件来验证密码是否已加密 getEncryptedPassword <- function(credential_label,credential_path) { # if path not supplied,use %USER_PROFILE%DPAPIpasswordscomputernamecredential_label.txt as default if (missing(credential_path)) { credential_path <- paste(Sys.getenv("USERPROFILE"),'DPAPIpasswords',Sys.info()["nodename"],'',credential_label,'.txt',sep="") } # construct command command <- paste('powershell -command "$PlainPassword = Get-Content ',credential_path,'; $SecurePassword = ConvertTo-SecureString $PlainPassword; $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword); $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR); echo $UnsecurePassword"',sep='') # execute powershell and return command return(system(command,intern=TRUE)) } >现在,当您需要在R中提供密码时,可以运行以下命令,而不是硬编码/提示输入密码: getEncryptedPassword("[PASSWORD IDENTIFIER]") 例如,而不是运行ROracle命令: dbConnect(driver,"MYUSER","MY PASSWORD",dbname="MYDB") 你可以改为运行它(我在步骤3中提供的标识符是“MYUSER_MYDB”: dbConnect(driver,getEncryptedPassword("MYUSER_MYDB"),dbname="MYDB") >您可以根据需要重复步骤3以获取尽可能多的密码,并在步骤5中使用正确的标识符进行呼叫. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- c – 在类的begin()方法上编译错误
- ruby-on-rails – 让渲染器识别自定义路由路径
- 正则表达式基础知识
- c – 使用OS X Yosemite上的gcc编译器编译OpenMP程序
- 浅谈PL/SQL批处理语句:BULK COLLECT与FORALL对优化做出的贡
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号十七
- ruby-on-rails – Rolify和CanCan有什么意义?
- nosql – Cassandra或MySQL / PostgreSQL?
- Flex中利用flexpaper查看文件
- ruby-on-rails – Heroku / Postgres错误:整数超出范围